@exercode/problem-utils 2.4.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/cli/checkAllProblems.cjs +1 -1
  2. package/dist/cli/checkAllProblems.cjs.map +1 -1
  3. package/dist/cli/checkAllProblems.js +1 -1
  4. package/dist/cli/checkAllProblems.js.map +1 -1
  5. package/dist/helpers/checkProblemDirIsolation.cjs +1 -1
  6. package/dist/helpers/checkProblemDirIsolation.js +1 -1
  7. package/dist/helpers/cleanWorkingDirectory.cjs +1 -1
  8. package/dist/helpers/cleanWorkingDirectory.cjs.map +1 -1
  9. package/dist/helpers/cleanWorkingDirectory.d.ts +2 -6
  10. package/dist/helpers/cleanWorkingDirectory.js +1 -1
  11. package/dist/helpers/cleanWorkingDirectory.js.map +1 -1
  12. package/dist/helpers/compareExpectedOutputFiles.cjs +1 -1
  13. package/dist/helpers/compareExpectedOutputFiles.cjs.map +1 -1
  14. package/dist/helpers/compareExpectedOutputFiles.js +1 -1
  15. package/dist/helpers/compareExpectedOutputFiles.js.map +1 -1
  16. package/dist/helpers/copyTestCaseFileInput.cjs +1 -1
  17. package/dist/helpers/copyTestCaseFileInput.cjs.map +1 -1
  18. package/dist/helpers/copyTestCaseFileInput.js +1 -1
  19. package/dist/helpers/copyTestCaseFileInput.js.map +1 -1
  20. package/dist/helpers/readOutputFiles.cjs +1 -1
  21. package/dist/helpers/readOutputFiles.cjs.map +1 -1
  22. package/dist/helpers/readOutputFiles.d.ts +0 -9
  23. package/dist/helpers/readOutputFiles.js +1 -1
  24. package/dist/helpers/readOutputFiles.js.map +1 -1
  25. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs +1 -1
  26. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs.map +1 -1
  27. package/dist/helpers/runCommandInTemporaryPackageManagerProject.d.ts +0 -3
  28. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js +1 -1
  29. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js.map +1 -1
  30. package/dist/helpers/spawnSyncWithTimeout.cjs +1 -1
  31. package/dist/helpers/spawnSyncWithTimeout.cjs.map +1 -1
  32. package/dist/helpers/spawnSyncWithTimeout.d.ts +0 -7
  33. package/dist/helpers/spawnSyncWithTimeout.js +1 -1
  34. package/dist/helpers/spawnSyncWithTimeout.js.map +1 -1
  35. package/dist/helpers/startHttpServer.cjs +1 -1
  36. package/dist/helpers/startHttpServer.cjs.map +1 -1
  37. package/dist/helpers/startHttpServer.js +1 -1
  38. package/dist/helpers/startHttpServer.js.map +1 -1
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.d.ts +0 -2
  41. package/dist/index.js +1 -1
  42. package/dist/presets/command.cjs +1 -1
  43. package/dist/presets/command.cjs.map +1 -1
  44. package/dist/presets/command.d.ts +0 -16
  45. package/dist/presets/command.js +1 -1
  46. package/dist/presets/command.js.map +1 -1
  47. package/dist/presets/evaluation.cjs +4 -4
  48. package/dist/presets/evaluation.cjs.map +1 -1
  49. package/dist/presets/evaluation.js +4 -4
  50. package/dist/presets/evaluation.js.map +1 -1
  51. package/dist/presets/guiCommand.cjs +3 -3
  52. package/dist/presets/guiCommand.cjs.map +1 -1
  53. package/dist/presets/guiCommand.d.ts +0 -24
  54. package/dist/presets/guiCommand.js +3 -3
  55. package/dist/presets/guiCommand.js.map +1 -1
  56. package/dist/presets/stdio.cjs +1 -1
  57. package/dist/presets/stdio.cjs.map +1 -1
  58. package/dist/presets/stdio.js +1 -1
  59. package/dist/presets/stdio.js.map +1 -1
  60. package/dist/types/language.cjs +2 -2
  61. package/dist/types/language.cjs.map +1 -1
  62. package/dist/types/language.js +2 -2
  63. package/dist/types/language.js.map +1 -1
  64. package/package.json +2 -2
  65. package/dist/helpers/runCustomRunner.cjs +0 -2
  66. package/dist/helpers/runCustomRunner.cjs.map +0 -1
  67. package/dist/helpers/runCustomRunner.d.ts +0 -8
  68. package/dist/helpers/runCustomRunner.js +0 -2
  69. package/dist/helpers/runCustomRunner.js.map +0 -1
  70. package/dist/helpers/safeFs.cjs +0 -2
  71. package/dist/helpers/safeFs.cjs.map +0 -1
  72. package/dist/helpers/safeFs.d.ts +0 -29
  73. package/dist/helpers/safeFs.js +0 -2
  74. package/dist/helpers/safeFs.js.map +0 -1
  75. package/dist/helpers/sandboxUser.cjs +0 -3
  76. package/dist/helpers/sandboxUser.cjs.map +0 -1
  77. package/dist/helpers/sandboxUser.d.ts +0 -146
  78. package/dist/helpers/sandboxUser.js +0 -3
  79. package/dist/helpers/sandboxUser.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"startHttpServer.cjs","names":["http","path","fs","sandboxUserName","isContainedPath"],"sources":["../../src/helpers/startHttpServer.ts"],"sourcesContent":["import fs from 'node:fs';\nimport http from 'node:http';\nimport path from 'node:path';\n\nimport { isContainedPath } from './safeFs.js';\nimport { sandboxUserName } from './sandboxUser.js';\n\nexport interface HttpServer {\n [Symbol.asyncDispose](): Promise<void>;\n url: string;\n port: number | undefined;\n}\n\n/**\n * Start a HTTP server for testing web pages.\n */\nexport function startHttpServer(dir: string): HttpServer {\n const server = http.createServer((request, response) => {\n let pathname = new URL(request.url ?? '/', 'http://127.0.0.1').pathname;\n\n if (pathname === '/') {\n pathname = '/index.html';\n }\n\n const pathnameWithIndexHtml = pathname.endsWith('/') ? path.join(pathname, 'index.html') : pathname;\n\n const filePath = path.join(dir, pathnameWithIndexHtml);\n\n // Under delegation this read runs as the trusted harness user while the served directory\n // belongs to the sandboxed submission, so a planted symlink (or symlinked parent) pointing at\n // e.g. the problem's expected outputs would otherwise be served straight back to the page.\n if (!isServableSubmissionPath(dir, filePath)) {\n response.statusCode = 404;\n response.end();\n return;\n }\n\n if (fs.existsSync(filePath)) {\n try {\n const buffer = fs.readFileSync(filePath);\n response.writeHead(200, {\n 'Access-Control-Allow-Origin': '*',\n Pragma: 'no-cache',\n 'Cache-Control': 'no-cache',\n 'Content-Type': getContentType(pathnameWithIndexHtml),\n });\n response.end(buffer);\n } catch {\n response.statusCode = 500;\n response.end();\n }\n } else {\n response.statusCode = 404;\n response.end();\n }\n });\n\n server.listen();\n\n const address = server.address();\n if (!address) throw new Error('server has been unexpectedly closed');\n\n return {\n [Symbol.asyncDispose]: async () => {\n server.closeAllConnections();\n await new Promise<void>((resolve) => {\n server.close(() => {\n resolve();\n });\n });\n },\n url: typeof address === 'string' ? address : `http://127.0.0.1:${address.port}`,\n port: typeof address === 'object' ? address.port : undefined,\n };\n}\n\nconst CONTENT_TYPE_BY_SUFFIX: Record<string, string> = {\n '.css': 'text/css',\n '.html': 'text/html',\n '.js': 'text/javascript',\n '.gif': 'image/gif',\n '.jpg': 'image/jpeg',\n '.png': 'image/png',\n '.svg': 'image/svg+xml',\n};\n\nfunction isServableSubmissionPath(dir: string, filePath: string): boolean {\n // Without user separation the page's own code can read these files anyway, so keep the previous\n // behavior and stay usable for course authoring outside the judge image.\n if (!sandboxUserName) return true;\n try {\n return isContainedPath(fs.realpathSync(dir), fs.realpathSync(filePath));\n } catch {\n // A missing file is reported as 404 below; anything unresolvable is not servable either.\n return !fs.existsSync(filePath);\n }\n}\n\nfunction getContentType(pathname: string): string {\n for (const [suffix, contentType] of Object.entries(CONTENT_TYPE_BY_SUFFIX)) {\n if (pathname.endsWith(suffix)) return contentType;\n }\n return 'text/plain';\n}\n"],"mappings":"6PAgBA,SAAgB,EAAgB,EAAyB,CACvD,IAAM,EAASA,EAAAA,QAAK,cAAc,EAAS,IAAa,CACtD,IAAI,EAAW,IAAI,IAAI,EAAQ,KAAO,IAAK,kBAAkB,CAAC,CAAC,SAE3D,IAAa,MACf,EAAW,eAGb,IAAM,EAAwB,EAAS,SAAS,GAAG,EAAIC,EAAAA,QAAK,KAAK,EAAU,YAAY,EAAI,EAErF,EAAWA,EAAAA,QAAK,KAAK,EAAK,CAAqB,EAKrD,GAAI,CAAC,EAAyB,EAAK,CAAQ,EAAG,CAC5C,EAAS,WAAa,IACtB,EAAS,I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fs"],"sources":["../../src/helpers/startHttpServer.ts"],"sourcesContent":["import fs from 'node:fs';\nimport http from 'node:http';\nimport path from 'node:path';\n\nexport interface HttpServer {\n [Symbol.asyncDispose](): Promise<void>;\n url: string;\n port: number | undefined;\n}\n\n/**\n * Start a HTTP server for testing web pages.\n */\nexport function startHttpServer(dir: string): HttpServer {\n const server = http.createServer((request, response) => {\n let pathname = new URL(request.url ?? '/', 'http://127.0.0.1').pathname;\n\n if (pathname === '/') {\n pathname = '/index.html';\n }\n\n const pathnameWithIndexHtml = pathname.endsWith('/') ? path.join(pathname, 'index.html') : pathname;\n\n const filePath = path.join(dir, pathnameWithIndexHtml);\n\n if (fs.existsSync(filePath)) {\n try {\n const buffer = fs.readFileSync(filePath);\n response.writeHead(200, {\n 'Access-Control-Allow-Origin': '*',\n Pragma: 'no-cache',\n 'Cache-Control': 'no-cache',\n 'Content-Type': getContentType(pathnameWithIndexHtml),\n });\n response.end(buffer);\n } catch {\n response.statusCode = 500;\n response.end();\n }\n } else {\n response.statusCode = 404;\n response.end();\n }\n });\n\n server.listen();\n\n const address = server.address();\n if (!address) throw new Error('server has been unexpectedly closed');\n\n return {\n [Symbol.asyncDispose]: async () => {\n server.closeAllConnections();\n await new Promise<void>((resolve) => {\n server.close(() => {\n resolve();\n });\n });\n },\n url: typeof address === 'string' ? address : `http://127.0.0.1:${address.port}`,\n port: typeof address === 'object' ? address.port : undefined,\n };\n}\n\nconst CONTENT_TYPE_BY_SUFFIX: Record<string, string> = {\n '.css': 'text/css',\n '.html': 'text/html',\n '.js': 'text/javascript',\n '.gif': 'image/gif',\n '.jpg': 'image/jpeg',\n '.png': 'image/png',\n '.svg': 'image/svg+xml',\n};\n\nfunction getContentType(pathname: string): string {\n for (const [suffix, contentType] of Object.entries(CONTENT_TYPE_BY_SUFFIX)) {\n if (pathname.endsWith(suffix)) return contentType;\n }\n return 'text/plain';\n}\n"],"mappings":"oMAaA,SAAgB,EAAgB,EAAyB,CACvD,IAAM,EAASA,EA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import{isContainedPath as t}from"./safeFs.js";import n from"node:fs";import r from"node:path";import i from"node:http";function a(e){let t=i.createServer((t,i)=>{let a=new URL(t.url??`/`,`http://127.0.0.1`).pathname;a===`/`&&(a=`/index.html`);let o=a.endsWith(`/`)?r.join(a,`index.html`):a,l=r.join(e,o);if(!s(e,l)){i.statusCode=404,i.end();return}if(n.existsSync(l))try{let e=n.readFileSync(l);i.writeHead(200,{"Access-Control-Allow-Origin":`*`,Pragma:`no-cache`,"Cache-Control":`no-cache`,"Content-Type":c(o)}),i.end(e)}catch{i.statusCode=500,i.end()}else i.statusCode=404,i.end()});t.listen();let a=t.address();if(!a)throw Error(`server has been unexpectedly closed`);return{[Symbol.asyncDispose]:async()=>{t.closeAllConnections(),await new Promise(e=>{t.close(()=>{e()})})},url:typeof a==`string`?a:`http://127.0.0.1:${a.port}`,port:typeof a==`object`?a.port:void 0}}const o={".css":`text/css`,".html":`text/html`,".js":`text/javascript`,".gif":`image/gif`,".jpg":`image/jpeg`,".png":`image/png`,".svg":`image/svg+xml`};function s(r,i){if(!e)return!0;try{return t(n.realpathSync(r),n.realpathSync(i))}catch{return!n.existsSync(i)}}function c(e){for(let[t,n]of Object.entries(o))if(e.endsWith(t))return n;return`text/plain`}export{a as startHttpServer};
1
+ import e from"node:fs";import t from"node:path";import n from"node:http";function r(r){let i=n.createServer((n,i)=>{let o=new URL(n.url??`/`,`http://127.0.0.1`).pathname;o===`/`&&(o=`/index.html`);let s=o.endsWith(`/`)?t.join(o,`index.html`):o,c=t.join(r,s);if(e.existsSync(c))try{let t=e.readFileSync(c);i.writeHead(200,{"Access-Control-Allow-Origin":`*`,Pragma:`no-cache`,"Cache-Control":`no-cache`,"Content-Type":a(s)}),i.end(t)}catch{i.statusCode=500,i.end()}else i.statusCode=404,i.end()});i.listen();let o=i.address();if(!o)throw Error(`server has been unexpectedly closed`);return{[Symbol.asyncDispose]:async()=>{i.closeAllConnections(),await new Promise(e=>{i.close(()=>{e()})})},url:typeof o==`string`?o:`http://127.0.0.1:${o.port}`,port:typeof o==`object`?o.port:void 0}}const i={".css":`text/css`,".html":`text/html`,".js":`text/javascript`,".gif":`image/gif`,".jpg":`image/jpeg`,".png":`image/png`,".svg":`image/svg+xml`};function a(e){for(let[t,n]of Object.entries(i))if(e.endsWith(t))return n;return`text/plain`}export{r as startHttpServer};
2
2
  //# sourceMappingURL=startHttpServer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"startHttpServer.js","names":[],"sources":["../../src/helpers/startHttpServer.ts"],"sourcesContent":["import fs from 'node:fs';\nimport http from 'node:http';\nimport path from 'node:path';\n\nimport { isContainedPath } from './safeFs.js';\nimport { sandboxUserName } from './sandboxUser.js';\n\nexport interface HttpServer {\n [Symbol.asyncDispose](): Promise<void>;\n url: string;\n port: number | undefined;\n}\n\n/**\n * Start a HTTP server for testing web pages.\n */\nexport function startHttpServer(dir: string): HttpServer {\n const server = http.createServer((request, response) => {\n let pathname = new URL(request.url ?? '/', 'http://127.0.0.1').pathname;\n\n if (pathname === '/') {\n pathname = '/index.html';\n }\n\n const pathnameWithIndexHtml = pathname.endsWith('/') ? path.join(pathname, 'index.html') : pathname;\n\n const filePath = path.join(dir, pathnameWithIndexHtml);\n\n // Under delegation this read runs as the trusted harness user while the served directory\n // belongs to the sandboxed submission, so a planted symlink (or symlinked parent) pointing at\n // e.g. the problem's expected outputs would otherwise be served straight back to the page.\n if (!isServableSubmissionPath(dir, filePath)) {\n response.statusCode = 404;\n response.end();\n return;\n }\n\n if (fs.existsSync(filePath)) {\n try {\n const buffer = fs.readFileSync(filePath);\n response.writeHead(200, {\n 'Access-Control-Allow-Origin': '*',\n Pragma: 'no-cache',\n 'Cache-Control': 'no-cache',\n 'Content-Type': getContentType(pathnameWithIndexHtml),\n });\n response.end(buffer);\n } catch {\n response.statusCode = 500;\n response.end();\n }\n } else {\n response.statusCode = 404;\n response.end();\n }\n });\n\n server.listen();\n\n const address = server.address();\n if (!address) throw new Error('server has been unexpectedly closed');\n\n return {\n [Symbol.asyncDispose]: async () => {\n server.closeAllConnections();\n await new Promise<void>((resolve) => {\n server.close(() => {\n resolve();\n });\n });\n },\n url: typeof address === 'string' ? address : `http://127.0.0.1:${address.port}`,\n port: typeof address === 'object' ? address.port : undefined,\n };\n}\n\nconst CONTENT_TYPE_BY_SUFFIX: Record<string, string> = {\n '.css': 'text/css',\n '.html': 'text/html',\n '.js': 'text/javascript',\n '.gif': 'image/gif',\n '.jpg': 'image/jpeg',\n '.png': 'image/png',\n '.svg': 'image/svg+xml',\n};\n\nfunction isServableSubmissionPath(dir: string, filePath: string): boolean {\n // Without user separation the page's own code can read these files anyway, so keep the previous\n // behavior and stay usable for course authoring outside the judge image.\n if (!sandboxUserName) return true;\n try {\n return isContainedPath(fs.realpathSync(dir), fs.realpathSync(filePath));\n } catch {\n // A missing file is reported as 404 below; anything unresolvable is not servable either.\n return !fs.existsSync(filePath);\n }\n}\n\nfunction getContentType(pathname: string): string {\n for (const [suffix, contentType] of Object.entries(CONTENT_TYPE_BY_SUFFIX)) {\n if (pathname.endsWith(suffix)) return contentType;\n }\n return 'text/plain';\n}\n"],"mappings":"0KAgBA,SAAgB,EAAgB,EAAyB,CACvD,IAAM,EAAS,EAAK,cAAc,EAAS,IAAa,CACtD,IAAI,EAAW,IAAI,IAAI,EAAQ,KAAO,IAAK,kBAAkB,CAAC,CAAC,SAE3D,IAAa,MACf,EAAW,eAGb,IAAM,EAAwB,EAAS,SAAS,GAAG,EAAI,EAAK,KAAK,EAAU,YAAY,EAAI,EAErF,EAAW,EAAK,KAAK,EAAK,CAAqB,EAKrD,GAAI,CAAC,EAAyB,EAAK,CAAQ,EAAG,CAC5C,EAAS,WAAa,IACtB,EAAS,IAAI,EACb,MACF,CAEA,GAAI,EAAG,WAAW,CAAQ,EACxB,GAAI,CACF,IAAM,EAAS,EAAG,aAAa,CAAQ,EACvC,EAAS,UAAU,IAAK,CACtB,8BAA+B,IAC/B,OAAQ,WACR,gBAAiB,WACjB,eAAgB,EAAe,CAAqB,CACtD,CAAC,EACD,EAAS,IAAI,CAAM,CACrB,MAAQ,CACN,EAAS,WAAa,IACtB,EAAS,IAAI,CACf,KAEA,GAAS,WAAa,IACtB,EAAS,IAAI,CAEjB,CAAC,EAED,EAAO,OAAO,EAEd,IAAM,EAAU,EAAO,QAAQ,EAC/B,GAAI,CAAC,EAAS,MAAU,MAAM,qCAAqC,EAEnE,MAAO,EACJ,OAAO,cAAe,SAAY,CACjC,EAAO,oBAAoB,EAC3B,MAAM,IAAI,QAAe,GAAY,CACnC,EAAO,UAAY,CACjB,EAAQ,CACV,CAAC,CACH,CAAC,CACH,EACA,IAAK,OAAO,GAAY,SAAW,EAAU,oBAAoB,EAAQ,OACzE,KAAM,OAAO,GAAY,SAAW,EAAQ,KAAO,IAAA,EACrD,CACF,CAEA,MAAM,EAAiD,CACrD,OAAQ,WACR,QAAS,YACT,MAAO,kBACP,OAAQ,YACR,OAAQ,aACR,OAAQ,YACR,OAAQ,eACV,EAEA,SAAS,EAAyB,EAAa,EAA2B,CAGxE,GAAI,CAAC,EAAiB,MAAO,GAC7B,GAAI,CACF,OAAO,EAAgB,EAAG,aAAa,CAAG,EAAG,EAAG,aAAa,CAAQ,CAAC,CACxE,MAAQ,CAEN,MAAO,CAAC,EAAG,WAAW,CAAQ,CAChC,CACF,CAEA,SAAS,EAAe,EAA0B,CAChD,IAAK,GAAM,CAAC,EAAQ,KAAgB,OAAO,QAAQ,CAAsB,EACvE,GAAI,EAAS,SAAS,CAAM,EAAG,OAAO,EAExC,MAAO,YACT"}
1
+ {"version":3,"file":"startHttpServer.js","names":[],"sources":["../../src/helpers/startHttpServer.ts"],"sourcesContent":["import fs from 'node:fs';\nimport http from 'node:http';\nimport path from 'node:path';\n\nexport interface HttpServer {\n [Symbol.asyncDispose](): Promise<void>;\n url: string;\n port: number | undefined;\n}\n\n/**\n * Start a HTTP server for testing web pages.\n */\nexport function startHttpServer(dir: string): HttpServer {\n const server = http.createServer((request, response) => {\n let pathname = new URL(request.url ?? '/', 'http://127.0.0.1').pathname;\n\n if (pathname === '/') {\n pathname = '/index.html';\n }\n\n const pathnameWithIndexHtml = pathname.endsWith('/') ? path.join(pathname, 'index.html') : pathname;\n\n const filePath = path.join(dir, pathnameWithIndexHtml);\n\n if (fs.existsSync(filePath)) {\n try {\n const buffer = fs.readFileSync(filePath);\n response.writeHead(200, {\n 'Access-Control-Allow-Origin': '*',\n Pragma: 'no-cache',\n 'Cache-Control': 'no-cache',\n 'Content-Type': getContentType(pathnameWithIndexHtml),\n });\n response.end(buffer);\n } catch {\n response.statusCode = 500;\n response.end();\n }\n } else {\n response.statusCode = 404;\n response.end();\n }\n });\n\n server.listen();\n\n const address = server.address();\n if (!address) throw new Error('server has been unexpectedly closed');\n\n return {\n [Symbol.asyncDispose]: async () => {\n server.closeAllConnections();\n await new Promise<void>((resolve) => {\n server.close(() => {\n resolve();\n });\n });\n },\n url: typeof address === 'string' ? address : `http://127.0.0.1:${address.port}`,\n port: typeof address === 'object' ? address.port : undefined,\n };\n}\n\nconst CONTENT_TYPE_BY_SUFFIX: Record<string, string> = {\n '.css': 'text/css',\n '.html': 'text/html',\n '.js': 'text/javascript',\n '.gif': 'image/gif',\n '.jpg': 'image/jpeg',\n '.png': 'image/png',\n '.svg': 'image/svg+xml',\n};\n\nfunction getContentType(pathname: string): string {\n for (const [suffix, contentType] of Object.entries(CONTENT_TYPE_BY_SUFFIX)) {\n if (pathname.endsWith(suffix)) return contentType;\n }\n return 'text/plain';\n}\n"],"mappings":"yEAaA,SAAgB,EAAgB,EAAyB,CACvD,IAAM,EAAS,EAAK,cAAc,EAAS,IAAa,CACtD,IAAI,EAAW,IAAI,IAAI,EAAQ,KAAO,IAAK,kBAAkB,CAAC,CAAC,SAE3D,IAAa,MACf,EAAW,eAGb,IAAM,EAAwB,EAAS,SAAS,GAAG,EAAI,EAAK,KAAK,EAAU,YAAY,EAAI,EAErF,EAAW,EAAK,KAAK,EAAK,CAAqB,EAErD,GAAI,EAAG,WAAW,CAAQ,EACxB,GAAI,CACF,IAAM,EAAS,EAAG,aAAa,CAAQ,EACvC,EAAS,UAAU,IAAK,CACtB,8BAA+B,IAC/B,OAAQ,WACR,gBAAiB,WACjB,eAAgB,EAAe,CAAqB,CACtD,CAAC,EACD,EAAS,IAAI,CAAM,CACrB,MAAQ,CACN,EAAS,WAAa,IACtB,EAAS,IAAI,CACf,KAEA,GAAS,WAAa,IACtB,EAAS,IAAI,CAEjB,CAAC,EAED,EAAO,OAAO,EAEd,IAAM,EAAU,EAAO,QAAQ,EAC/B,GAAI,CAAC,EAAS,MAAU,MAAM,qCAAqC,EAEnE,MAAO,EACJ,OAAO,cAAe,SAAY,CACjC,EAAO,oBAAoB,EAC3B,MAAM,IAAI,QAAe,GAAY,CACnC,EAAO,UAAY,CACjB,EAAQ,CACV,CAAC,CACH,CAAC,CACH,EACA,IAAK,OAAO,GAAY,SAAW,EAAU,oBAAoB,EAAQ,OACzE,KAAM,OAAO,GAAY,SAAW,EAAQ,KAAO,IAAA,EACrD,CACF,CAEA,MAAM,EAAiD,CACrD,OAAQ,WACR,QAAS,YACT,MAAO,kBACP,OAAQ,YACR,OAAQ,aACR,OAAQ,YACR,OAAQ,eACV,EAEA,SAAS,EAAe,EAA0B,CAChD,IAAK,GAAM,CAAC,EAAQ,KAAgB,OAAO,QAAQ,CAAsB,EACvE,GAAI,EAAS,SAAS,CAAM,EAAG,OAAO,EAExC,MAAO,YACT"}
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./helpers/compareStdoutAsSpaceSeparatedTokens.cjs"),t=require("./types/testCaseResult.cjs"),n=require("./helpers/printTestCaseResult.cjs"),r=require("./helpers/sandboxUser.cjs"),i=require("./helpers/safeFs.cjs"),a=require("./helpers/compareExpectedOutputFiles.cjs"),o=require("./helpers/defaultStdioHarness.cjs"),s=require("./helpers/parseArgs.cjs"),c=require("./helpers/parseCsv.cjs"),l=require("./helpers/readTestCases.cjs"),u=require("./helpers/removeCommentsInSourceCode.cjs"),d=require("./helpers/runCommandInTemporaryPackageManagerProject.cjs"),f=require("./helpers/runPythonInTemporaryUvProject.cjs"),p=require("./helpers/sourceCodeGrammars.cjs"),m=require("./helpers/startHttpServer.cjs"),h=require("./types/decisionCode.cjs"),g=require("./types/problem.cjs");exports.CsvParseError=c.CsvParseError,exports.DecisionCode=h.DecisionCode,exports.HARNESS_FILE_PRESETS=o.HARNESS_FILE_PRESETS,exports.MAX_COMPARED_FILE_BYTES=a.MAX_COMPARED_FILE_BYTES,exports.MAX_REPORTED_FILE_BYTES=a.MAX_REPORTED_FILE_BYTES,exports.SANDBOX_ENV_PREFIX=r.SANDBOX_ENV_PREFIX,exports.SANDBOX_USER_ENV_NAME=r.SANDBOX_USER_ENV_NAME,exports.SANDBOX_WATCHDOG_GRACE_SECONDS=r.SANDBOX_WATCHDOG_GRACE_SECONDS,exports.TEST_CASE_RESULT_PREFIX=t.TEST_CASE_RESULT_PREFIX,exports.TIMEOUT_COMMAND=r.TIMEOUT_COMMAND,exports.codeRuleSchema=g.codeRuleSchema,exports.compareExpectedOutputFiles=a.compareExpectedOutputFiles,exports.compareStdoutAsSpaceSeparatedTokens=e.compareStdoutAsSpaceSeparatedTokens,exports.copyPackageManagerProjectFiles=d.copyPackageManagerProjectFiles,exports.copyWithoutFollowingSymlinks=i.copyWithoutFollowingSymlinks,exports.createDirectoryWithoutFollowingSymlinks=i.createDirectoryWithoutFollowingSymlinks,exports.encodeFileForTestCaseResult=n.encodeFileForTestCaseResult,exports.findDefaultStdioHarnessFiles=o.findDefaultStdioHarnessFiles,exports.forceRemove=r.forceRemove,exports.getSandboxUserEnvOverrides=r.getSandboxUserEnvOverrides,exports.getTrustedHelperEnv=r.getTrustedHelperEnv,exports.isContainedPath=i.isContainedPath,exports.isDefaultStdioHarnessSource=o.isDefaultStdioHarnessSource,exports.isSandboxWrappedCommand=r.isSandboxWrappedCommand,exports.judgeAgainstExpectations=a.judgeAgainstExpectations,exports.killSandboxUserProcesses=r.killSandboxUserProcesses,exports.languageIdToSourceCodeGrammar=p.languageIdToSourceCodeGrammar,exports.makeAccessibleToSandboxUser=r.makeAccessibleToSandboxUser,exports.makeTraversableBySandboxUser=r.makeTraversableBySandboxUser,exports.mergeComparisonOutputFiles=a.mergeComparisonOutputFiles,exports.normalizeCodeRule=g.normalizeCodeRule,exports.parseArgs=s.parseArgs,exports.parseCsv=c.parseCsv,exports.parseCsvRecords=c.parseCsvRecords,exports.prependInsideSandboxWrapper=r.prependInsideSandboxWrapper,exports.printTestCaseResult=n.printTestCaseResult,exports.problemMarkdownFrontMatterSchema=g.problemMarkdownFrontMatterSchema,exports.readTestCases=l.readTestCases,exports.relaxPermissionsAsSandboxUser=r.relaxPermissionsAsSandboxUser,exports.removeCommentsAndStringsInSourceCode=u.removeCommentsAndStringsInSourceCode,exports.removeCommentsInSourceCode=u.removeCommentsInSourceCode,exports.runCommandInTemporaryPackageManagerProject=d.runCommandInTemporaryPackageManagerProject,exports.runPythonInTemporaryUvProject=f.runPythonInTemporaryUvProject,exports.sandboxUserName=r.sandboxUserName,exports.startHttpServer=m.startHttpServer,exports.startSandboxTimeoutWatchdog=r.startSandboxTimeoutWatchdog,exports.testCaseResultSchema=t.testCaseResultSchema,exports.wrapCommandWithSandboxUser=r.wrapCommandWithSandboxUser,exports.writeFileWithoutFollowingSymlinks=i.writeFileWithoutFollowingSymlinks;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./helpers/compareStdoutAsSpaceSeparatedTokens.cjs"),t=require("./types/testCaseResult.cjs"),n=require("./helpers/printTestCaseResult.cjs"),r=require("./helpers/compareExpectedOutputFiles.cjs"),i=require("./helpers/defaultStdioHarness.cjs"),a=require("./helpers/parseArgs.cjs"),o=require("./helpers/parseCsv.cjs"),s=require("./helpers/readTestCases.cjs"),c=require("./helpers/removeCommentsInSourceCode.cjs"),l=require("./helpers/runCommandInTemporaryPackageManagerProject.cjs"),u=require("./helpers/runPythonInTemporaryUvProject.cjs"),d=require("./helpers/sourceCodeGrammars.cjs"),f=require("./helpers/startHttpServer.cjs"),p=require("./types/decisionCode.cjs"),m=require("./types/problem.cjs");exports.CsvParseError=o.CsvParseError,exports.DecisionCode=p.DecisionCode,exports.HARNESS_FILE_PRESETS=i.HARNESS_FILE_PRESETS,exports.MAX_COMPARED_FILE_BYTES=r.MAX_COMPARED_FILE_BYTES,exports.MAX_REPORTED_FILE_BYTES=r.MAX_REPORTED_FILE_BYTES,exports.TEST_CASE_RESULT_PREFIX=t.TEST_CASE_RESULT_PREFIX,exports.codeRuleSchema=m.codeRuleSchema,exports.compareExpectedOutputFiles=r.compareExpectedOutputFiles,exports.compareStdoutAsSpaceSeparatedTokens=e.compareStdoutAsSpaceSeparatedTokens,exports.copyPackageManagerProjectFiles=l.copyPackageManagerProjectFiles,exports.encodeFileForTestCaseResult=n.encodeFileForTestCaseResult,exports.findDefaultStdioHarnessFiles=i.findDefaultStdioHarnessFiles,exports.isDefaultStdioHarnessSource=i.isDefaultStdioHarnessSource,exports.judgeAgainstExpectations=r.judgeAgainstExpectations,exports.languageIdToSourceCodeGrammar=d.languageIdToSourceCodeGrammar,exports.mergeComparisonOutputFiles=r.mergeComparisonOutputFiles,exports.normalizeCodeRule=m.normalizeCodeRule,exports.parseArgs=a.parseArgs,exports.parseCsv=o.parseCsv,exports.parseCsvRecords=o.parseCsvRecords,exports.printTestCaseResult=n.printTestCaseResult,exports.problemMarkdownFrontMatterSchema=m.problemMarkdownFrontMatterSchema,exports.readTestCases=s.readTestCases,exports.removeCommentsAndStringsInSourceCode=c.removeCommentsAndStringsInSourceCode,exports.removeCommentsInSourceCode=c.removeCommentsInSourceCode,exports.runCommandInTemporaryPackageManagerProject=l.runCommandInTemporaryPackageManagerProject,exports.runPythonInTemporaryUvProject=u.runPythonInTemporaryUvProject,exports.startHttpServer=f.startHttpServer,exports.testCaseResultSchema=t.testCaseResultSchema;
package/dist/index.d.ts CHANGED
@@ -8,8 +8,6 @@ export * from './helpers/readTestCases.js';
8
8
  export * from './helpers/removeCommentsInSourceCode.js';
9
9
  export * from './helpers/runCommandInTemporaryPackageManagerProject.js';
10
10
  export * from './helpers/runPythonInTemporaryUvProject.js';
11
- export * from './helpers/safeFs.js';
12
- export * from './helpers/sandboxUser.js';
13
11
  export * from './helpers/sourceCodeGrammars.js';
14
12
  export * from './helpers/startHttpServer.js';
15
13
  export * from './types/decisionCode.js';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{compareStdoutAsSpaceSeparatedTokens as e}from"./helpers/compareStdoutAsSpaceSeparatedTokens.js";import{TEST_CASE_RESULT_PREFIX as t,testCaseResultSchema as n}from"./types/testCaseResult.js";import{encodeFileForTestCaseResult as r,printTestCaseResult as i}from"./helpers/printTestCaseResult.js";import{SANDBOX_ENV_PREFIX as a,SANDBOX_USER_ENV_NAME as o,SANDBOX_WATCHDOG_GRACE_SECONDS as s,TIMEOUT_COMMAND as c,forceRemove as l,getSandboxUserEnvOverrides as u,getTrustedHelperEnv as d,isSandboxWrappedCommand as f,killSandboxUserProcesses as p,makeAccessibleToSandboxUser as m,makeTraversableBySandboxUser as h,prependInsideSandboxWrapper as g,relaxPermissionsAsSandboxUser as _,sandboxUserName as v,startSandboxTimeoutWatchdog as y,wrapCommandWithSandboxUser as b}from"./helpers/sandboxUser.js";import{copyWithoutFollowingSymlinks as x,createDirectoryWithoutFollowingSymlinks as S,isContainedPath as C,writeFileWithoutFollowingSymlinks as w}from"./helpers/safeFs.js";import{MAX_COMPARED_FILE_BYTES as T,MAX_REPORTED_FILE_BYTES as E,compareExpectedOutputFiles as D,judgeAgainstExpectations as O,mergeComparisonOutputFiles as k}from"./helpers/compareExpectedOutputFiles.js";import{HARNESS_FILE_PRESETS as A,findDefaultStdioHarnessFiles as j,isDefaultStdioHarnessSource as M}from"./helpers/defaultStdioHarness.js";import{parseArgs as N}from"./helpers/parseArgs.js";import{CsvParseError as P,parseCsv as F,parseCsvRecords as I}from"./helpers/parseCsv.js";import{readTestCases as L}from"./helpers/readTestCases.js";import{removeCommentsAndStringsInSourceCode as R,removeCommentsInSourceCode as z}from"./helpers/removeCommentsInSourceCode.js";import{copyPackageManagerProjectFiles as B,runCommandInTemporaryPackageManagerProject as V}from"./helpers/runCommandInTemporaryPackageManagerProject.js";import{runPythonInTemporaryUvProject as H}from"./helpers/runPythonInTemporaryUvProject.js";import{languageIdToSourceCodeGrammar as U}from"./helpers/sourceCodeGrammars.js";import{startHttpServer as W}from"./helpers/startHttpServer.js";import{DecisionCode as G}from"./types/decisionCode.js";import{codeRuleSchema as K,normalizeCodeRule as q,problemMarkdownFrontMatterSchema as J}from"./types/problem.js";export{P as CsvParseError,G as DecisionCode,A as HARNESS_FILE_PRESETS,T as MAX_COMPARED_FILE_BYTES,E as MAX_REPORTED_FILE_BYTES,a as SANDBOX_ENV_PREFIX,o as SANDBOX_USER_ENV_NAME,s as SANDBOX_WATCHDOG_GRACE_SECONDS,t as TEST_CASE_RESULT_PREFIX,c as TIMEOUT_COMMAND,K as codeRuleSchema,D as compareExpectedOutputFiles,e as compareStdoutAsSpaceSeparatedTokens,B as copyPackageManagerProjectFiles,x as copyWithoutFollowingSymlinks,S as createDirectoryWithoutFollowingSymlinks,r as encodeFileForTestCaseResult,j as findDefaultStdioHarnessFiles,l as forceRemove,u as getSandboxUserEnvOverrides,d as getTrustedHelperEnv,C as isContainedPath,M as isDefaultStdioHarnessSource,f as isSandboxWrappedCommand,O as judgeAgainstExpectations,p as killSandboxUserProcesses,U as languageIdToSourceCodeGrammar,m as makeAccessibleToSandboxUser,h as makeTraversableBySandboxUser,k as mergeComparisonOutputFiles,q as normalizeCodeRule,N as parseArgs,F as parseCsv,I as parseCsvRecords,g as prependInsideSandboxWrapper,i as printTestCaseResult,J as problemMarkdownFrontMatterSchema,L as readTestCases,_ as relaxPermissionsAsSandboxUser,R as removeCommentsAndStringsInSourceCode,z as removeCommentsInSourceCode,V as runCommandInTemporaryPackageManagerProject,H as runPythonInTemporaryUvProject,v as sandboxUserName,W as startHttpServer,y as startSandboxTimeoutWatchdog,n as testCaseResultSchema,b as wrapCommandWithSandboxUser,w as writeFileWithoutFollowingSymlinks};
1
+ import{compareStdoutAsSpaceSeparatedTokens as e}from"./helpers/compareStdoutAsSpaceSeparatedTokens.js";import{TEST_CASE_RESULT_PREFIX as t,testCaseResultSchema as n}from"./types/testCaseResult.js";import{encodeFileForTestCaseResult as r,printTestCaseResult as i}from"./helpers/printTestCaseResult.js";import{MAX_COMPARED_FILE_BYTES as a,MAX_REPORTED_FILE_BYTES as o,compareExpectedOutputFiles as s,judgeAgainstExpectations as c,mergeComparisonOutputFiles as l}from"./helpers/compareExpectedOutputFiles.js";import{HARNESS_FILE_PRESETS as u,findDefaultStdioHarnessFiles as d,isDefaultStdioHarnessSource as f}from"./helpers/defaultStdioHarness.js";import{parseArgs as p}from"./helpers/parseArgs.js";import{CsvParseError as m,parseCsv as h,parseCsvRecords as g}from"./helpers/parseCsv.js";import{readTestCases as _}from"./helpers/readTestCases.js";import{removeCommentsAndStringsInSourceCode as v,removeCommentsInSourceCode as y}from"./helpers/removeCommentsInSourceCode.js";import{copyPackageManagerProjectFiles as b,runCommandInTemporaryPackageManagerProject as x}from"./helpers/runCommandInTemporaryPackageManagerProject.js";import{runPythonInTemporaryUvProject as S}from"./helpers/runPythonInTemporaryUvProject.js";import{languageIdToSourceCodeGrammar as C}from"./helpers/sourceCodeGrammars.js";import{startHttpServer as w}from"./helpers/startHttpServer.js";import{DecisionCode as T}from"./types/decisionCode.js";import{codeRuleSchema as E,normalizeCodeRule as D,problemMarkdownFrontMatterSchema as O}from"./types/problem.js";export{m as CsvParseError,T as DecisionCode,u as HARNESS_FILE_PRESETS,a as MAX_COMPARED_FILE_BYTES,o as MAX_REPORTED_FILE_BYTES,t as TEST_CASE_RESULT_PREFIX,E as codeRuleSchema,s as compareExpectedOutputFiles,e as compareStdoutAsSpaceSeparatedTokens,b as copyPackageManagerProjectFiles,r as encodeFileForTestCaseResult,d as findDefaultStdioHarnessFiles,f as isDefaultStdioHarnessSource,c as judgeAgainstExpectations,C as languageIdToSourceCodeGrammar,l as mergeComparisonOutputFiles,D as normalizeCodeRule,p as parseArgs,h as parseCsv,g as parseCsvRecords,i as printTestCaseResult,O as problemMarkdownFrontMatterSchema,_ as readTestCases,v as removeCommentsAndStringsInSourceCode,y as removeCommentsInSourceCode,x as runCommandInTemporaryPackageManagerProject,S as runPythonInTemporaryUvProject,w as startHttpServer,n as testCaseResultSchema};
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/sandboxUser.cjs"),r=require("../helpers/readOutputFiles.cjs"),i=require("../helpers/compareExpectedOutputFiles.cjs"),a=require("../helpers/parseArgs.cjs"),o=require("../helpers/readTestCases.cjs"),s=require("../types/decisionCode.cjs"),c=require("../helpers/cleanWorkingDirectory.cjs"),l=require("../helpers/copyTestCaseFileInput.cjs"),u=require("../helpers/findEntryPointFile.cjs"),d=require("../helpers/findLanguageDefinitionByPath.cjs"),f=require("../helpers/judgeByStaticAnalysis.cjs"),p=require("../helpers/runCustomRunner.cjs"),m=require("../helpers/runIsolationCheckInDebugMode.cjs"),h=require("../helpers/readProblemMarkdownFrontMatter.cjs"),g=require("../helpers/resolveCwds.cjs"),_=require("../helpers/spawnSyncWithTimeout.cjs");let v=require("node:path");v=e.__toESM(v,1);let y=require("zod");const b=y.z.object({language:y.z.union([y.z.string(),y.z.array(y.z.string())]).optional()});async function x(e,t={}){let n=a.parseArgs(process.argv),r=b.parse(n.params),{cwds:i,isDebugMode:o}=await g.resolveCwds(e,n.cwd),s=await h.readProblemMarkdownFrontMatter(e),c=await(t.readTestCases??E)(e),l={buildTimeoutSeconds:t.limits?.buildTimeoutSeconds??10,maxOutputLength:t.limits?.maxOutputLength??5e4},u=typeof s.timeLimitMs==`number`?s.timeLimitMs/1e3:t.runTimeoutSeconds??2,d={problemMarkdownFrontMatter:s,testCases:c,limits:l,timeLimitSeconds:u};if(o){let t=(l.buildTimeoutSeconds+c.length*u)*1e3;if(!await m.passesIsolationCheckInDebugMode(e,i,r,{expectedMaxDurationMs:t})){process.exitCode=1;return}}for(let n of i){o&&g.printDebugCwdBanner(e,n);let i=await S(n.cwd,r,d,t);o&&!C(n,i)&&(process.exitCode=1,g.printDebugExpectationFailureBanner(e,n))}}async function S(e,i,a,o){n.makeAccessibleToSandboxUser(e);let{problemMarkdownFrontMatter:m,testCases:h,limits:g,timeLimitSeconds:_}=a,v=h[0]?.id??`prebuild`,y=await f.judgeByStaticAnalysis(e,m);if(y)return t.printTestCaseResult({testCaseId:v,...y}),{allAccepted:!1};let b=await u.findEntryPointFile(e,i.language);if(!b)return t.printTestCaseResult({testCaseId:v,decisionCode:s.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:`main file not found${i.language?`: language: ${i.language}`:``}`}),{allAccepted:!1};let x=d.findLanguageDefinitionByPath(b);if(!x)return t.printTestCaseResult({testCaseId:v,decisionCode:s.DecisionCode.WRONG_ANSWER,stderr:`unsupported language`}),{allAccepted:!1};let S={...process.env,CI:``,FORCE_COLOR:`0`},C=b;if(o.buildSubmission!==!1){if(x.prebuild)try{await x.prebuild(e);let t=await u.findEntryPointFile(e,i.language);t&&(C=t)}catch(e){return t.printTestCaseResult({testCaseId:v,decisionCode:s.DecisionCode.BUILD_ERROR,stderr:e instanceof Error?e.message:String(e)}),{allAccepted:!1}}let n=x.buildCommand?.(C);if(n){let r=w(n,{cwd:e,env:S,testCaseId:v,limits:g});if(r)return t.printTestCaseResult(r),{allAccepted:!1}}}let E=await c.snapshotWorkingDirectory(e);if(h.length===0)return t.printTestCaseResult({testCaseId:`default`,decisionCode:s.DecisionCode.ACCEPTED}),{allAccepted:!0};let A=h.shared?.fileInputPath;for(let i of h){A&&await l.copyTestCaseFileInput(A,e),i.fileInputPath&&await l.copyTestCaseFileInput(i.fileInputPath,e);let a=x.command(C),u=i.input??``,d;try{o.resolveInput&&(u=await o.resolveInput({testCase:i,cwd:e,env:S})),d=o.runCommand?await p.runCustomRunner(()=>o.runCommand?.({testCase:i,command:n.wrapCommandWithSandboxUser(a),stdin:u,cwd:e,env:{...S,...n.getSandboxUserEnvOverrides(S)},timeLimitSeconds:_})):O(a,{stdin:u,cwd:e,env:S,timeLimitSeconds:_})}catch(n){return t.printTestCaseResult({testCaseId:i.id,decisionCode:s.DecisionCode.RUNTIME_ERROR,stdin:u,stderr:T(n)}),await c.cleanWorkingDirectory(e,E),{allAccepted:!1}}let f=await r.readOutputFiles(e,m.requiredOutputFilePaths??[]),h={timeLimitSeconds:_,outputLimitLength:g.maxOutputLength,problemMarkdownFrontMatter:{memoryLimitByte:m.memoryLimitByte,requiredOutputFilePaths:m.requiredOutputFilePaths}},v=k({runResult:d,outputFiles:f,context:h}),y=v;if(v.decisionCode===s.DecisionCode.ACCEPTED)try{let t=o.test?await o.test({testCase:i,runResult:d,outputFiles:f,cwd:e,context:h}):await D({testCase:i,runResult:d,outputFiles:f,cwd:e});t&&(y={decisionCode:t.decisionCode??v.decisionCode,feedbackMarkdown:t.feedbackMarkdown,stderr:t.stderr})}catch(e){y={decisionCode:s.DecisionCode.RUNTIME_ERROR,stderr:T(e)}}if(t.printTestCaseResult({testCaseId:i.id,...y,exitStatus:d.status,stdin:d.stdin,stdout:d.stdout.slice(0,g.maxOutputLength)||void 0,stderr:(y.stderr??d.stderr).slice(0,g.maxOutputLength)||void 0,timeSeconds:d.timeSeconds,memoryBytes:d.memoryBytes,outputFiles:f.length>0?f:void 0}),await c.cleanWorkingDirectory(e,E),y.decisionCode!==s.DecisionCode.ACCEPTED)return{allAccepted:!1}}return{allAccepted:!0}}function C(e,t){return t.allAccepted===(e.expectedResult===`accepted`)}function w(e,t){let n=_.spawnSyncWithTimeout(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,env:t.env},t.limits.buildTimeoutSeconds),r=n.status??void 0;if(n.status!==0)return{testCaseId:t.testCaseId,decisionCode:s.DecisionCode.BUILD_ERROR,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.timeSeconds>t.limits.buildTimeoutSeconds)return{testCaseId:t.testCaseId,decisionCode:s.DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.stdout.length>t.limits.maxOutputLength||n.stderr.length>t.limits.maxOutputLength)return{testCaseId:t.testCaseId,decisionCode:s.DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function T(e){return e instanceof Error?e.message:String(e)}async function E(e){return await o.readTestCases(v.default.join(e,`test_cases`))}async function D(e){let{testCase:t,runResult:n,outputFiles:r,cwd:a}=e,o=await i.judgeAgainstExpectations({stdout:n.stdout,expectedStdout:typeof t.output==`string`?t.output:void 0,fileOutputPath:typeof t.fileOutputPath==`string`?t.fileOutputPath:void 0,cwd:a,outputFiles:r});return r.splice(0,r.length,...o.outputFiles),o.matches?{}:{decisionCode:s.DecisionCode.WRONG_ANSWER}}function O(e,t){let n=_.spawnSyncWithTimeout(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,input:t.stdin,env:t.env},t.timeLimitSeconds);return{stdin:t.stdin,stdout:n.stdout,stderr:n.stderr,status:n.status??void 0,timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function k(e){if(e.runResult.status!==0)return{decisionCode:s.DecisionCode.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.timeSeconds>e.context.timeLimitSeconds)return{decisionCode:s.DecisionCode.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:s.DecisionCode.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.outputLimitExceeded||e.runResult.stdout.length>e.context.outputLimitLength||e.runResult.stderr.length>e.context.outputLimitLength)return{decisionCode:s.DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:s.DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:s.DecisionCode.ACCEPTED}}exports.commandJudgePreset=x;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/compareExpectedOutputFiles.cjs"),r=require("../helpers/parseArgs.cjs"),i=require("../helpers/readTestCases.cjs"),a=require("../types/decisionCode.cjs"),o=require("../helpers/cleanWorkingDirectory.cjs"),s=require("../helpers/copyTestCaseFileInput.cjs"),c=require("../helpers/findEntryPointFile.cjs"),l=require("../helpers/findLanguageDefinitionByPath.cjs"),u=require("../helpers/judgeByStaticAnalysis.cjs"),d=require("../helpers/readOutputFiles.cjs"),f=require("../helpers/runIsolationCheckInDebugMode.cjs"),p=require("../helpers/readProblemMarkdownFrontMatter.cjs"),m=require("../helpers/resolveCwds.cjs"),h=require("../helpers/spawnSyncWithTimeout.cjs");let g=require("node:path");g=e.__toESM(g,1);let _=require("zod");const v=_.z.object({language:_.z.union([_.z.string(),_.z.array(_.z.string())]).optional()});async function y(e,t={}){let n=r.parseArgs(process.argv),i=v.parse(n.params),{cwds:a,isDebugMode:o}=await m.resolveCwds(e,n.cwd),s=await p.readProblemMarkdownFrontMatter(e),c=await(t.readTestCases??w)(e),l={buildTimeoutSeconds:t.limits?.buildTimeoutSeconds??10,maxOutputLength:t.limits?.maxOutputLength??5e4},u=typeof s.timeLimitMs==`number`?s.timeLimitMs/1e3:t.runTimeoutSeconds??2,d={problemMarkdownFrontMatter:s,testCases:c,limits:l,timeLimitSeconds:u};if(o){let t=(l.buildTimeoutSeconds+c.length*u)*1e3;if(!await f.passesIsolationCheckInDebugMode(e,a,i,{expectedMaxDurationMs:t})){process.exitCode=1;return}}for(let n of a){o&&m.printDebugCwdBanner(e,n);let r=await b(n.cwd,i,d,t);o&&!x(n,r)&&(process.exitCode=1,m.printDebugExpectationFailureBanner(e,n))}}async function b(e,n,r,i){let{problemMarkdownFrontMatter:f,testCases:p,limits:m,timeLimitSeconds:h}=r,g=p[0]?.id??`prebuild`,_=await u.judgeByStaticAnalysis(e,f);if(_)return t.printTestCaseResult({testCaseId:g,..._}),{allAccepted:!1};let v=await c.findEntryPointFile(e,n.language);if(!v)return t.printTestCaseResult({testCaseId:g,decisionCode:a.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:`main file not found${n.language?`: language: ${n.language}`:``}`}),{allAccepted:!1};let y=l.findLanguageDefinitionByPath(v);if(!y)return t.printTestCaseResult({testCaseId:g,decisionCode:a.DecisionCode.WRONG_ANSWER,stderr:`unsupported language`}),{allAccepted:!1};let b={...process.env,CI:``,FORCE_COLOR:`0`},x=v;if(i.buildSubmission!==!1){if(y.prebuild)try{await y.prebuild(e);let t=await c.findEntryPointFile(e,n.language);t&&(x=t)}catch(e){return t.printTestCaseResult({testCaseId:g,decisionCode:a.DecisionCode.BUILD_ERROR,stderr:e instanceof Error?e.message:String(e)}),{allAccepted:!1}}let r=y.buildCommand?.(x);if(r){let n=S(r,{cwd:e,env:b,testCaseId:g,limits:m});if(n)return t.printTestCaseResult(n),{allAccepted:!1}}}let w=await o.snapshotWorkingDirectory(e);if(p.length===0)return t.printTestCaseResult({testCaseId:`default`,decisionCode:a.DecisionCode.ACCEPTED}),{allAccepted:!0};let O=p.shared?.fileInputPath;for(let n of p){O&&await s.copyTestCaseFileInput(O,e),n.fileInputPath&&await s.copyTestCaseFileInput(n.fileInputPath,e);let r=y.command(x),c=n.input??``,l;try{i.resolveInput&&(c=await i.resolveInput({testCase:n,cwd:e,env:b})),l=i.runCommand?await i.runCommand({testCase:n,command:r,stdin:c,cwd:e,env:b,timeLimitSeconds:h}):E(r,{stdin:c,cwd:e,env:b,timeLimitSeconds:h})}catch(r){return t.printTestCaseResult({testCaseId:n.id,decisionCode:a.DecisionCode.RUNTIME_ERROR,stdin:c,stderr:C(r)}),await o.cleanWorkingDirectory(e,w),{allAccepted:!1}}let u=await d.readOutputFiles(e,f.requiredOutputFilePaths??[]),p={timeLimitSeconds:h,outputLimitLength:m.maxOutputLength,problemMarkdownFrontMatter:{memoryLimitByte:f.memoryLimitByte,requiredOutputFilePaths:f.requiredOutputFilePaths}},g=D({runResult:l,outputFiles:u,context:p}),_=g;if(g.decisionCode===a.DecisionCode.ACCEPTED)try{let t=i.test?await i.test({testCase:n,runResult:l,outputFiles:u,cwd:e,context:p}):await T({testCase:n,runResult:l,outputFiles:u,cwd:e});t&&(_={decisionCode:t.decisionCode??g.decisionCode,feedbackMarkdown:t.feedbackMarkdown,stderr:t.stderr})}catch(e){_={decisionCode:a.DecisionCode.RUNTIME_ERROR,stderr:C(e)}}if(t.printTestCaseResult({testCaseId:n.id,..._,exitStatus:l.status,stdin:l.stdin,stdout:l.stdout.slice(0,m.maxOutputLength)||void 0,stderr:(_.stderr??l.stderr).slice(0,m.maxOutputLength)||void 0,timeSeconds:l.timeSeconds,memoryBytes:l.memoryBytes,outputFiles:u.length>0?u:void 0}),await o.cleanWorkingDirectory(e,w),_.decisionCode!==a.DecisionCode.ACCEPTED)return{allAccepted:!1}}return{allAccepted:!0}}function x(e,t){return t.allAccepted===(e.expectedResult===`accepted`)}function S(e,t){let n=h.spawnSyncWithTimeout(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,env:t.env},t.limits.buildTimeoutSeconds),r=n.status??void 0;if(n.status!==0)return{testCaseId:t.testCaseId,decisionCode:a.DecisionCode.BUILD_ERROR,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.timeSeconds>t.limits.buildTimeoutSeconds)return{testCaseId:t.testCaseId,decisionCode:a.DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.stdout.length>t.limits.maxOutputLength||n.stderr.length>t.limits.maxOutputLength)return{testCaseId:t.testCaseId,decisionCode:a.DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function C(e){return e instanceof Error?e.message:String(e)}async function w(e){return await i.readTestCases(g.default.join(e,`test_cases`))}async function T(e){let{testCase:t,runResult:r,outputFiles:i,cwd:o}=e,s=await n.judgeAgainstExpectations({stdout:r.stdout,expectedStdout:typeof t.output==`string`?t.output:void 0,fileOutputPath:typeof t.fileOutputPath==`string`?t.fileOutputPath:void 0,cwd:o,outputFiles:i});return i.splice(0,i.length,...s.outputFiles),s.matches?{}:{decisionCode:a.DecisionCode.WRONG_ANSWER}}function E(e,t){let n=h.spawnSyncWithTimeout(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,input:t.stdin,env:t.env},t.timeLimitSeconds);return{stdin:t.stdin,stdout:n.stdout,stderr:n.stderr,status:n.status??void 0,timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function D(e){if(e.runResult.status!==0)return{decisionCode:a.DecisionCode.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.timeSeconds>e.context.timeLimitSeconds)return{decisionCode:a.DecisionCode.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:a.DecisionCode.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.outputLimitExceeded||e.runResult.stdout.length>e.context.outputLimitLength||e.runResult.stderr.length>e.context.outputLimitLength)return{decisionCode:a.DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:a.DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:a.DecisionCode.ACCEPTED}}exports.commandJudgePreset=y;
2
2
  //# sourceMappingURL=command.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"command.cjs","names":["z","parseArgs","resolveCwds","readProblemMarkdownFrontMatter","passesIsolationCheckInDebugMode","judgeByStaticAnalysis","findEntryPointFile","DecisionCode","findLanguageDefinitionByPath","snapshotWorkingDirectory","copyTestCaseFileInput","runCustomRunner","wrapCommandWithSandboxUser","getSandboxUserEnvOverrides","cleanWorkingDirectory","readOutputFiles","spawnSyncWithTimeout","readFileTestCases","path","judgeAgainstExpectations"],"sources":["../../src/presets/command.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { judgeAgainstExpectations } from '../helpers/compareExpectedOutputFiles.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { runCustomRunner } from '../helpers/runCustomRunner.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n getSandboxUserEnvOverrides,\n makeAccessibleToSandboxUser,\n wrapCommandWithSandboxUser,\n} from '../helpers/sandboxUser.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 2;\nconst MAX_STDOUT_LENGTH = 50_000;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\ntype JudgeParams = z.infer<typeof judgeParamsSchema>;\n\n/** What every command test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseCommandTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface CommandTestCase extends BaseCommandTestCase {\n /** Expected standard output (`test_cases/<id>.out`). */\n output?: string;\n /** Directory of expected output files compared after the run (`test_cases/<id>.fout/`). */\n fileOutputPath?: string;\n}\n\ntype CommandJudgeCaseResult = Pick<TestCaseResult, 'decisionCode' | 'feedbackMarkdown' | 'stderr'>;\n\nexport interface CommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n outputLimitExceeded?: boolean;\n}\n\ninterface CommandJudgeContext {\n timeLimitSeconds: number;\n outputLimitLength: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\nexport interface CommandJudgeLimits {\n buildTimeoutSeconds: number;\n maxOutputLength: number;\n}\n\ninterface ResolvedCommandProblem<TTestCase extends BaseCommandTestCase> {\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n testCases: readonly TTestCase[];\n limits: CommandJudgeLimits;\n timeLimitSeconds: number;\n}\n\nexport interface CommandJudgePresetOptions<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n> {\n /**\n * Set to false when the custom runner builds the submitted source itself.\n * Defaults to true.\n */\n buildSubmission?: boolean;\n limits?: CommandJudgeLimits;\n /** The per-case time limit used only when `problem.md` declares no `timeLimitMs`. */\n runTimeoutSeconds?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n /**\n * Runs as the trusted harness user with the submission's `cwd`. Fixture files it creates there\n * must be written with `createDirectoryWithoutFollowingSymlinks`/`writeFileWithoutFollowingSymlinks`\n * (both exported): a submission can plant a symlink at a fixture path, and a plain `fs.writeFile`\n * would follow it into a file only the harness can write.\n */\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n runCommand?: (context: {\n testCase: TTestCase;\n /**\n * The command to run. Under `EXERCODE_SANDBOX_USER` delegation it is already wrapped so it\n * executes as the sandbox user; spawn it as given (with the supplied `env`) instead of\n * reconstructing it, or the submission runs as the trusted harness user.\n *\n * Its direct child is then a root-owned `sudo` whose descendants belong to the sandbox user, so\n * the handler cannot signal them: enforce `timeLimitSeconds` with `startSandboxTimeoutWatchdog`\n * and `killSandboxUserProcesses` (both exported) rather than `child.kill()` or an outer\n * `timeout`. The preset terminates leftover sandbox processes after the handler returns.\n */\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }) => Promise<TRunResult> | TRunResult;\n /**\n * Decides the verdict of a run that passed the limit checks. It replaces the default comparison\n * of `testCase.output` (space-separated tokens) and `testCase.fileOutputPath` (see\n * `compareStdoutAsSpaceSeparatedTokens` and `compareExpectedOutputFiles`, both exported), so\n * call those yourself when the test case ships `.out` / `.fout` expectations to keep.\n * `outputFiles` is the array printed with the result and may be edited in place.\n */\n test?: (context: {\n testCase: TTestCase;\n runResult: TRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: CommandJudgeContext;\n }) => Promise<Partial<CommandJudgeCaseResult>> | Partial<CommandJudgeCaseResult> | undefined;\n}\n\n/**\n * A preset function for judging by executable command.\n *\n * Without options, test cases come from `test_cases/` (`.in`, `.out`, `.fin/`, `.fout/`) and a run\n * within the limits is accepted when its stdout matches `.out` and its files match `.fout/`; a\n * case without either expectation only has to run within the limits.\n * Keep problem-specific logic in `resolveInput` and `test`.\n *\n * @example\n * Create `judge.ts` that judges `test_cases/` like the default stdio harness, with the command\n * preset's debug mode:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n *\n * await commandJudgePreset(import.meta.dirname);\n * ```\n *\n * @example\n * Create `judge.ts` with an own verdict:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n * import { DecisionCode } from '@exercode/problem-utils';\n *\n * await commandJudgePreset(import.meta.dirname, {\n * readTestCases: async () => [\n * { id: '01', input: '1 2' },\n * ],\n * test: ({ runResult }) => {\n * return runResult.stdout.trim() === '3'\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n *\n * Run with the required parameters:\n * ```bash\n * bun judge.ts model_answers/python '{ \"language\": \"python\" }'\n * ```\n *\n * Run without a cwd argument to judge each `<problemDir>/model_answers/*` directory\n * for debugging. A prominent banner is printed for each run.\n * ```bash\n * bun judge.ts\n * ```\n */\nexport async function commandJudgePreset<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(problemDir: string, options: CommandJudgePresetOptions<TTestCase, TRunResult> = {}): Promise<void> {\n const args = parseArgs(process.argv);\n const params = judgeParamsSchema.parse(args.params);\n\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const testCases = await (options.readTestCases ?? readCommandTestCases)(problemDir);\n const limits = {\n buildTimeoutSeconds: options.limits?.buildTimeoutSeconds ?? BUILD_TIMEOUT_SECONDS,\n maxOutputLength: options.limits?.maxOutputLength ?? MAX_STDOUT_LENGTH,\n };\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n const problem: ResolvedCommandProblem<TTestCase> = {\n problemMarkdownFrontMatter,\n testCases,\n limits,\n timeLimitSeconds,\n };\n\n if (isDebugMode) {\n const expectedMaxDurationMs = (limits.buildTimeoutSeconds + testCases.length * timeLimitSeconds) * 1000;\n if (!(await passesIsolationCheckInDebugMode(problemDir, cwds, params, { expectedMaxDurationMs }))) {\n process.exitCode = 1;\n return;\n }\n }\n\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await runCommandJudgeForCwd<TTestCase, TRunResult>(resolvedCwd.cwd, params, problem, options);\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function runCommandJudgeForCwd<\n TTestCase extends BaseCommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(\n cwd: string,\n params: JudgeParams,\n problem: ResolvedCommandProblem<TTestCase>,\n options: CommandJudgePresetOptions<TTestCase, TRunResult>\n): Promise<{ allAccepted: boolean }> {\n // The sandboxed submission must read its sources and write build/run outputs in its directory.\n makeAccessibleToSandboxUser(cwd);\n\n const { problemMarkdownFrontMatter, testCases, limits, timeLimitSeconds } = problem;\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return { allAccepted: false };\n }\n\n const originalMainFilePath = await findEntryPointFile(cwd, params.language);\n if (!originalMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return { allAccepted: false };\n }\n\n const languageDefinition = findLanguageDefinitionByPath(originalMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return { allAccepted: false };\n }\n\n // `CI` changes affects Chainlit. `FORCE_COLOR` affects Bun.\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let mainFilePath = originalMainFilePath;\n if (options.buildSubmission !== false) {\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(cwd);\n const prebuiltMainFilePath = await findEntryPointFile(cwd, params.language);\n if (prebuiltMainFilePath) mainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: error instanceof Error ? error.message : String(error),\n });\n return { allAccepted: false };\n }\n }\n\n const buildCommand = languageDefinition.buildCommand?.(mainFilePath);\n if (buildCommand) {\n const buildResult = runBuild(buildCommand, {\n cwd,\n env,\n testCaseId: prebuildTestCaseId,\n limits,\n });\n if (buildResult) {\n printTestCaseResult(buildResult);\n return { allAccepted: false };\n }\n }\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(cwd);\n\n if (testCases.length === 0) {\n printTestCaseResult({ testCaseId: 'default', decisionCode: DecisionCode.ACCEPTED });\n return { allAccepted: true };\n }\n\n const sharedFileInputPath = (testCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n\n for (const testCase of testCases) {\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, cwd);\n\n const command = languageDefinition.command(mainFilePath);\n let stdin = testCase.input ?? '';\n let runResult: TRunResult;\n try {\n if (options.resolveInput) {\n stdin = await options.resolveInput({ testCase, cwd, env });\n }\n\n runResult = options.runCommand\n ? await runCustomRunner(\n () =>\n // Hand custom runners a command that is already sandbox-wrapped, and the matching\n // environment: they spawn it themselves, so an unwrapped command would run the\n // submission as the trusted harness user and defeat the delegation boundary.\n options.runCommand?.({\n testCase,\n command: wrapCommandWithSandboxUser(command),\n stdin,\n cwd,\n env: { ...env, ...getSandboxUserEnvOverrides(env) },\n timeLimitSeconds,\n }) as Promise<TRunResult>\n )\n : (runCommand(command, {\n stdin,\n cwd,\n env,\n timeLimitSeconds,\n }) as TRunResult);\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n return { allAccepted: false };\n }\n\n const outputFiles = await readOutputFiles(cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: CommandJudgeContext = {\n timeLimitSeconds,\n outputLimitLength: limits.maxOutputLength,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateByLimits({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = options.test\n ? await options.test({ testCase, runResult, outputFiles, cwd, context: judgeContext })\n : await compareWithExpectedOutputs({ testCase, runResult, outputFiles, cwd });\n if (extendedJudgeResult) {\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n };\n }\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n printTestCaseResult({\n testCaseId: testCase.id,\n ...judgeResult,\n exitStatus: runResult.status,\n stdin: runResult.stdin,\n stdout: runResult.stdout.slice(0, limits.maxOutputLength) || undefined,\n stderr: (judgeResult.stderr ?? runResult.stderr).slice(0, limits.maxOutputLength) || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n outputFiles: outputFiles.length > 0 ? outputFiles : undefined,\n });\n\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n if (judgeResult.decisionCode !== DecisionCode.ACCEPTED) return { allAccepted: false };\n }\n\n return { allAccepted: true };\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: { allAccepted: boolean }): boolean {\n return result.allAccepted === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction runBuild(\n buildCommand: readonly [string, ...string[]],\n context: { cwd: string; env: NodeJS.ProcessEnv; testCaseId: string; limits: CommandJudgeLimits }\n): (Omit<TestCaseResult, 'testCaseId'> & { testCaseId: string }) | undefined {\n const spawnResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n context.limits.buildTimeoutSeconds\n );\n const exitStatus = spawnResult.status ?? undefined;\n\n if (spawnResult.status !== 0) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (spawnResult.timeSeconds > context.limits.buildTimeoutSeconds) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (\n spawnResult.stdout.length > context.limits.maxOutputLength ||\n spawnResult.stderr.length > context.limits.maxOutputLength\n ) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n return;\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nasync function readCommandTestCases<TTestCase extends BaseCommandTestCase = CommandTestCase>(\n problemDir: string\n): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\n/** The default verdict: a case exposing a string `output` / `fileOutputPath` is judged against it. */\nasync function compareWithExpectedOutputs(context: {\n testCase: BaseCommandTestCase & Partial<CommandTestCase>;\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n cwd: string;\n}): Promise<Partial<CommandJudgeCaseResult>> {\n const { testCase, runResult, outputFiles, cwd } = context;\n const judgement = await judgeAgainstExpectations({\n stdout: runResult.stdout,\n expectedStdout: typeof testCase.output === 'string' ? testCase.output : undefined,\n fileOutputPath: typeof testCase.fileOutputPath === 'string' ? testCase.fileOutputPath : undefined,\n cwd,\n outputFiles,\n });\n outputFiles.splice(0, outputFiles.length, ...judgement.outputFiles);\n return judgement.matches ? {} : { decisionCode: DecisionCode.WRONG_ANSWER };\n}\n\nfunction runCommand(\n command: readonly [string, ...string[]],\n context: {\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }\n): CommandRunResult {\n const spawnResult = spawnSyncWithTimeout(\n command[0],\n command.slice(1),\n { cwd: context.cwd, encoding: 'utf8', input: context.stdin, env: context.env },\n context.timeLimitSeconds\n );\n\n return {\n stdin: context.stdin,\n stdout: spawnResult.stdout,\n stderr: spawnResult.stderr,\n status: spawnResult.status ?? undefined,\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n}\n\nfunction evaluateByLimits(context: {\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: CommandJudgeContext;\n}): CommandJudgeCaseResult {\n if (context.runResult.status !== 0) {\n return { decisionCode: DecisionCode.RUNTIME_ERROR, stderr: context.runResult.stderr };\n }\n\n if (context.runResult.timeSeconds > context.context.timeLimitSeconds) {\n return { decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return { decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.outputLimitExceeded ||\n context.runResult.stdout.length > context.context.outputLimitLength ||\n context.runResult.stderr.length > context.context.outputLimitLength\n ) {\n return { decisionCode: DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n const requiredOutputFileCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFileCount) {\n return { decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n"],"mappings":"++BAiCA,MAIM,EAAoBA,EAAAA,EAAE,OAAO,CACjC,SAAUA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,OAAO,EAAGA,EAAAA,EAAE,MAAMA,EAAAA,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAChE,CAAC,EAwJD,eAAsB,EAGpB,EAAoB,EAA4D,CAAC,EAAkB,CACnG,IAAM,EAAOC,EAAAA,UAAU,QAAQ,IAAI,EAC7B,EAAS,EAAkB,MAAM,EAAK,MAAM,EAE5C,CAAE,OAAM,eAAgB,MAAMC,EAAAA,YAAY,EAAY,EAAK,GAAG,EAC9D,EAA6B,MAAMC,EAAAA,+BAA+B,CAAU,EAC5E,EAAY,MAAO,EAAQ,eAAiB,EAAA,CAAsB,CAAU,EAC5E,EAAS,CACb,oBAAqB,EAAQ,QAAQ,qBAAuB,GAC5D,gBAAiB,EAAQ,QAAQ,iBAAmB,GACtD,EACM,EACJ,OAAO,EAA2B,aAAgB,SAC9C,EAA2B,YAAc,IACxC,EAAQ,mBAAqB,EAC9B,EAA6C,CACjD,6BACA,YACA,SACA,kBACF,EAEA,GAAI,EAAa,CACf,IAAM,GAAyB,EAAO,oBAAsB,EAAU,OAAS,GAAoB,IACnG,GAAI,CAAE,MAAMC,EAAAA,gCAAgC,EAAY,EAAM,EAAQ,CAAE,uBAAsB,CAAC,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,UAAU,qBAClB,EAAQ,UAAU,OAAO,OAAS,EAAQ,QAAQ,mBAClD,EAAQ,UAAU,OAAO,OAAS,EAAQ,QAAQ,kBAElD,MAAO,CAAE,aAAcA,EAAAA,aAAa,2BAA4B,OAAQ,EAAQ,UAAU,MAAO,EAGnG,IAAM,EAA0B,EAAQ,QAAQ,2BAA2B,yBAAyB,QAAU,EAK9G,OAJI,EAAQ,YAAY,OAAS,EACxB,CAAE,aAAcA,EAAAA,aAAa,kCAAmC,EAGlE,CAAE,aAAcA,EAAAA,aAAa,QAAS,CAC/C"}
1
+ {"version":3,"file":"command.cjs","names":["z","parseArgs","resolveCwds","readProblemMarkdownFrontMatter","passesIsolationCheckInDebugMode","judgeByStaticAnalysis","findEntryPointFile","DecisionCode","findLanguageDefinitionByPath","snapshotWorkingDirectory","copyTestCaseFileInput","cleanWorkingDirectory","readOutputFiles","spawnSyncWithTimeout","readFileTestCases","path","judgeAgainstExpectations"],"sources":["../../src/presets/command.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { judgeAgainstExpectations } from '../helpers/compareExpectedOutputFiles.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 2;\nconst MAX_STDOUT_LENGTH = 50_000;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\ntype JudgeParams = z.infer<typeof judgeParamsSchema>;\n\n/** What every command test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseCommandTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface CommandTestCase extends BaseCommandTestCase {\n /** Expected standard output (`test_cases/<id>.out`). */\n output?: string;\n /** Directory of expected output files compared after the run (`test_cases/<id>.fout/`). */\n fileOutputPath?: string;\n}\n\ntype CommandJudgeCaseResult = Pick<TestCaseResult, 'decisionCode' | 'feedbackMarkdown' | 'stderr'>;\n\nexport interface CommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n outputLimitExceeded?: boolean;\n}\n\ninterface CommandJudgeContext {\n timeLimitSeconds: number;\n outputLimitLength: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\nexport interface CommandJudgeLimits {\n buildTimeoutSeconds: number;\n maxOutputLength: number;\n}\n\ninterface ResolvedCommandProblem<TTestCase extends BaseCommandTestCase> {\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n testCases: readonly TTestCase[];\n limits: CommandJudgeLimits;\n timeLimitSeconds: number;\n}\n\nexport interface CommandJudgePresetOptions<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n> {\n /**\n * Set to false when the custom runner builds the submitted source itself.\n * Defaults to true.\n */\n buildSubmission?: boolean;\n limits?: CommandJudgeLimits;\n /** The per-case time limit used only when `problem.md` declares no `timeLimitMs`. */\n runTimeoutSeconds?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n runCommand?: (context: {\n testCase: TTestCase;\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }) => Promise<TRunResult> | TRunResult;\n /**\n * Decides the verdict of a run that passed the limit checks. It replaces the default comparison\n * of `testCase.output` (space-separated tokens) and `testCase.fileOutputPath` (see\n * `compareStdoutAsSpaceSeparatedTokens` and `compareExpectedOutputFiles`, both exported), so\n * call those yourself when the test case ships `.out` / `.fout` expectations to keep.\n * `outputFiles` is the array printed with the result and may be edited in place.\n */\n test?: (context: {\n testCase: TTestCase;\n runResult: TRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: CommandJudgeContext;\n }) => Promise<Partial<CommandJudgeCaseResult>> | Partial<CommandJudgeCaseResult> | undefined;\n}\n\n/**\n * A preset function for judging by executable command.\n *\n * Without options, test cases come from `test_cases/` (`.in`, `.out`, `.fin/`, `.fout/`) and a run\n * within the limits is accepted when its stdout matches `.out` and its files match `.fout/`; a\n * case without either expectation only has to run within the limits.\n * Keep problem-specific logic in `resolveInput` and `test`.\n *\n * @example\n * Create `judge.ts` that judges `test_cases/` like the default stdio harness, with the command\n * preset's debug mode:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n *\n * await commandJudgePreset(import.meta.dirname);\n * ```\n *\n * @example\n * Create `judge.ts` with an own verdict:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n * import { DecisionCode } from '@exercode/problem-utils';\n *\n * await commandJudgePreset(import.meta.dirname, {\n * readTestCases: async () => [\n * { id: '01', input: '1 2' },\n * ],\n * test: ({ runResult }) => {\n * return runResult.stdout.trim() === '3'\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n *\n * Run with the required parameters:\n * ```bash\n * bun judge.ts model_answers/python '{ \"language\": \"python\" }'\n * ```\n *\n * Run without a cwd argument to judge each `<problemDir>/model_answers/*` directory\n * for debugging. A prominent banner is printed for each run.\n * ```bash\n * bun judge.ts\n * ```\n */\nexport async function commandJudgePreset<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(problemDir: string, options: CommandJudgePresetOptions<TTestCase, TRunResult> = {}): Promise<void> {\n const args = parseArgs(process.argv);\n const params = judgeParamsSchema.parse(args.params);\n\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const testCases = await (options.readTestCases ?? readCommandTestCases)(problemDir);\n const limits = {\n buildTimeoutSeconds: options.limits?.buildTimeoutSeconds ?? BUILD_TIMEOUT_SECONDS,\n maxOutputLength: options.limits?.maxOutputLength ?? MAX_STDOUT_LENGTH,\n };\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n const problem: ResolvedCommandProblem<TTestCase> = {\n problemMarkdownFrontMatter,\n testCases,\n limits,\n timeLimitSeconds,\n };\n\n if (isDebugMode) {\n const expectedMaxDurationMs = (limits.buildTimeoutSeconds + testCases.length * timeLimitSeconds) * 1000;\n if (!(await passesIsolationCheckInDebugMode(problemDir, cwds, params, { expectedMaxDurationMs }))) {\n process.exitCode = 1;\n return;\n }\n }\n\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await runCommandJudgeForCwd<TTestCase, TRunResult>(resolvedCwd.cwd, params, problem, options);\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function runCommandJudgeForCwd<\n TTestCase extends BaseCommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(\n cwd: string,\n params: JudgeParams,\n problem: ResolvedCommandProblem<TTestCase>,\n options: CommandJudgePresetOptions<TTestCase, TRunResult>\n): Promise<{ allAccepted: boolean }> {\n const { problemMarkdownFrontMatter, testCases, limits, timeLimitSeconds } = problem;\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return { allAccepted: false };\n }\n\n const originalMainFilePath = await findEntryPointFile(cwd, params.language);\n if (!originalMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return { allAccepted: false };\n }\n\n const languageDefinition = findLanguageDefinitionByPath(originalMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return { allAccepted: false };\n }\n\n // `CI` changes affects Chainlit. `FORCE_COLOR` affects Bun.\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let mainFilePath = originalMainFilePath;\n if (options.buildSubmission !== false) {\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(cwd);\n const prebuiltMainFilePath = await findEntryPointFile(cwd, params.language);\n if (prebuiltMainFilePath) mainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: error instanceof Error ? error.message : String(error),\n });\n return { allAccepted: false };\n }\n }\n\n const buildCommand = languageDefinition.buildCommand?.(mainFilePath);\n if (buildCommand) {\n const buildResult = runBuild(buildCommand, {\n cwd,\n env,\n testCaseId: prebuildTestCaseId,\n limits,\n });\n if (buildResult) {\n printTestCaseResult(buildResult);\n return { allAccepted: false };\n }\n }\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(cwd);\n\n if (testCases.length === 0) {\n printTestCaseResult({ testCaseId: 'default', decisionCode: DecisionCode.ACCEPTED });\n return { allAccepted: true };\n }\n\n const sharedFileInputPath = (testCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n\n for (const testCase of testCases) {\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, cwd);\n\n const command = languageDefinition.command(mainFilePath);\n let stdin = testCase.input ?? '';\n let runResult: TRunResult;\n try {\n if (options.resolveInput) {\n stdin = await options.resolveInput({ testCase, cwd, env });\n }\n\n runResult = options.runCommand\n ? await options.runCommand({ testCase, command, stdin, cwd, env, timeLimitSeconds })\n : (runCommand(command, {\n stdin,\n cwd,\n env,\n timeLimitSeconds,\n }) as TRunResult);\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n return { allAccepted: false };\n }\n\n const outputFiles = await readOutputFiles(cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: CommandJudgeContext = {\n timeLimitSeconds,\n outputLimitLength: limits.maxOutputLength,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateByLimits({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = options.test\n ? await options.test({ testCase, runResult, outputFiles, cwd, context: judgeContext })\n : await compareWithExpectedOutputs({ testCase, runResult, outputFiles, cwd });\n if (extendedJudgeResult) {\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n };\n }\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n printTestCaseResult({\n testCaseId: testCase.id,\n ...judgeResult,\n exitStatus: runResult.status,\n stdin: runResult.stdin,\n stdout: runResult.stdout.slice(0, limits.maxOutputLength) || undefined,\n stderr: (judgeResult.stderr ?? runResult.stderr).slice(0, limits.maxOutputLength) || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n outputFiles: outputFiles.length > 0 ? outputFiles : undefined,\n });\n\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n if (judgeResult.decisionCode !== DecisionCode.ACCEPTED) return { allAccepted: false };\n }\n\n return { allAccepted: true };\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: { allAccepted: boolean }): boolean {\n return result.allAccepted === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction runBuild(\n buildCommand: readonly [string, ...string[]],\n context: { cwd: string; env: NodeJS.ProcessEnv; testCaseId: string; limits: CommandJudgeLimits }\n): (Omit<TestCaseResult, 'testCaseId'> & { testCaseId: string }) | undefined {\n const spawnResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n context.limits.buildTimeoutSeconds\n );\n const exitStatus = spawnResult.status ?? undefined;\n\n if (spawnResult.status !== 0) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (spawnResult.timeSeconds > context.limits.buildTimeoutSeconds) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (\n spawnResult.stdout.length > context.limits.maxOutputLength ||\n spawnResult.stderr.length > context.limits.maxOutputLength\n ) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n return;\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nasync function readCommandTestCases<TTestCase extends BaseCommandTestCase = CommandTestCase>(\n problemDir: string\n): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\n/** The default verdict: a case exposing a string `output` / `fileOutputPath` is judged against it. */\nasync function compareWithExpectedOutputs(context: {\n testCase: BaseCommandTestCase & Partial<CommandTestCase>;\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n cwd: string;\n}): Promise<Partial<CommandJudgeCaseResult>> {\n const { testCase, runResult, outputFiles, cwd } = context;\n const judgement = await judgeAgainstExpectations({\n stdout: runResult.stdout,\n expectedStdout: typeof testCase.output === 'string' ? testCase.output : undefined,\n fileOutputPath: typeof testCase.fileOutputPath === 'string' ? testCase.fileOutputPath : undefined,\n cwd,\n outputFiles,\n });\n outputFiles.splice(0, outputFiles.length, ...judgement.outputFiles);\n return judgement.matches ? {} : { decisionCode: DecisionCode.WRONG_ANSWER };\n}\n\nfunction runCommand(\n command: readonly [string, ...string[]],\n context: {\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }\n): CommandRunResult {\n const spawnResult = spawnSyncWithTimeout(\n command[0],\n command.slice(1),\n { cwd: context.cwd, encoding: 'utf8', input: context.stdin, env: context.env },\n context.timeLimitSeconds\n );\n\n return {\n stdin: context.stdin,\n stdout: spawnResult.stdout,\n stderr: spawnResult.stderr,\n status: spawnResult.status ?? undefined,\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n}\n\nfunction evaluateByLimits(context: {\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: CommandJudgeContext;\n}): CommandJudgeCaseResult {\n if (context.runResult.status !== 0) {\n return { decisionCode: DecisionCode.RUNTIME_ERROR, stderr: context.runResult.stderr };\n }\n\n if (context.runResult.timeSeconds > context.context.timeLimitSeconds) {\n return { decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return { decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.outputLimitExceeded ||\n context.runResult.stdout.length > context.context.outputLimitLength ||\n context.runResult.stderr.length > context.context.outputLimitLength\n ) {\n return { decisionCode: DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n const requiredOutputFileCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFileCount) {\n return { decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n"],"mappings":"25BA2BA,MAIM,EAAoBA,EAAAA,EAAE,OAAO,CACjC,SAAUA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,OAAO,EAAGA,EAAAA,EAAE,MAAMA,EAAAA,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAChE,CAAC,EAwID,eAAsB,EAGpB,EAAoB,EAA4D,CAAC,EAAkB,CACnG,IAAM,EAAOC,EAAAA,UAAU,QAAQ,IAAI,EAC7B,EAAS,EAAkB,MAAM,EAAK,MAAM,EAE5C,CAAE,OAAM,eAAgB,MAAMC,EAAAA,YAAY,EAAY,EAAK,GAAG,EAC9D,EAA6B,MAAMC,EAAAA,+BAA+B,CAAU,EAC5E,EAAY,MAAO,EAAQ,eAAiB,EAAA,CAAsB,CAAU,EAC5E,EAAS,CACb,oBAAqB,EAAQ,QAAQ,qBAAuB,GAC5D,gBAAiB,EAAQ,QAAQ,iBAAmB,GACtD,EACM,EACJ,OAAO,EAA2B,aAAgB,SAC9C,EAA2B,YAAc,IACxC,EAAQ,mBAAqB,EAC9B,EAA6C,CACjD,6BACA,YACA,SACA,kBACF,EAEA,GAAI,EAAa,CACf,IAAM,GAAyB,EAAO,oBAAsB,EAAU,OAAS,GAAoB,IACnG,GAAI,CAAE,MAAM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U,OAAO,OAAS,EAAQ,QAAQ,kBAElD,MAAO,CAAE,aAAcA,EAAAA,aAAa,2BAA4B,OAAQ,EAAQ,UAAU,MAAO,EAGnG,IAAM,EAA0B,EAAQ,QAAQ,2BAA2B,yBAAyB,QAAU,EAK9G,OAJI,EAAQ,YAAY,OAAS,EACxB,CAAE,aAAcA,EAAAA,aAAa,kCAAmC,EAGlE,CAAE,aAAcA,EAAAA,aAAa,QAAS,CAC/C"}
@@ -44,12 +44,6 @@ export interface CommandJudgePresetOptions<TTestCase extends BaseCommandTestCase
44
44
  /** The per-case time limit used only when `problem.md` declares no `timeLimitMs`. */
45
45
  runTimeoutSeconds?: number;
46
46
  readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;
47
- /**
48
- * Runs as the trusted harness user with the submission's `cwd`. Fixture files it creates there
49
- * must be written with `createDirectoryWithoutFollowingSymlinks`/`writeFileWithoutFollowingSymlinks`
50
- * (both exported): a submission can plant a symlink at a fixture path, and a plain `fs.writeFile`
51
- * would follow it into a file only the harness can write.
52
- */
53
47
  resolveInput?: (context: {
54
48
  testCase: TTestCase;
55
49
  cwd: string;
@@ -57,16 +51,6 @@ export interface CommandJudgePresetOptions<TTestCase extends BaseCommandTestCase
57
51
  }) => Promise<string> | string;
58
52
  runCommand?: (context: {
59
53
  testCase: TTestCase;
60
- /**
61
- * The command to run. Under `EXERCODE_SANDBOX_USER` delegation it is already wrapped so it
62
- * executes as the sandbox user; spawn it as given (with the supplied `env`) instead of
63
- * reconstructing it, or the submission runs as the trusted harness user.
64
- *
65
- * Its direct child is then a root-owned `sudo` whose descendants belong to the sandbox user, so
66
- * the handler cannot signal them: enforce `timeLimitSeconds` with `startSandboxTimeoutWatchdog`
67
- * and `killSandboxUserProcesses` (both exported) rather than `child.kill()` or an outer
68
- * `timeout`. The preset terminates leftover sandbox processes after the handler returns.
69
- */
70
54
  command: readonly [string, ...string[]];
71
55
  stdin: string;
72
56
  cwd: string;
@@ -1,2 +1,2 @@
1
- import{printTestCaseResult as e}from"../helpers/printTestCaseResult.js";import{getSandboxUserEnvOverrides as t,makeAccessibleToSandboxUser as n,wrapCommandWithSandboxUser as r}from"../helpers/sandboxUser.js";import{readOutputFiles as i}from"../helpers/readOutputFiles.js";import{judgeAgainstExpectations as a}from"../helpers/compareExpectedOutputFiles.js";import{parseArgs as o}from"../helpers/parseArgs.js";import{readTestCases as s}from"../helpers/readTestCases.js";import{DecisionCode as c}from"../types/decisionCode.js";import{cleanWorkingDirectory as l,snapshotWorkingDirectory as u}from"../helpers/cleanWorkingDirectory.js";import{copyTestCaseFileInput as d}from"../helpers/copyTestCaseFileInput.js";import{findEntryPointFile as f}from"../helpers/findEntryPointFile.js";import{findLanguageDefinitionByPath as p}from"../helpers/findLanguageDefinitionByPath.js";import{judgeByStaticAnalysis as m}from"../helpers/judgeByStaticAnalysis.js";import{runCustomRunner as h}from"../helpers/runCustomRunner.js";import{passesIsolationCheckInDebugMode as g}from"../helpers/runIsolationCheckInDebugMode.js";import{readProblemMarkdownFrontMatter as _}from"../helpers/readProblemMarkdownFrontMatter.js";import{printDebugCwdBanner as v,printDebugExpectationFailureBanner as y,resolveCwds as b}from"../helpers/resolveCwds.js";import{spawnSyncWithTimeout as x}from"../helpers/spawnSyncWithTimeout.js";import S from"node:path";import{z as C}from"zod";const w=C.object({language:C.union([C.string(),C.array(C.string())]).optional()});async function T(e,t={}){let n=o(process.argv),r=w.parse(n.params),{cwds:i,isDebugMode:a}=await b(e,n.cwd),s=await _(e),c=await(t.readTestCases??A)(e),l={buildTimeoutSeconds:t.limits?.buildTimeoutSeconds??10,maxOutputLength:t.limits?.maxOutputLength??5e4},u=typeof s.timeLimitMs==`number`?s.timeLimitMs/1e3:t.runTimeoutSeconds??2,d={problemMarkdownFrontMatter:s,testCases:c,limits:l,timeLimitSeconds:u};if(a){let t=(l.buildTimeoutSeconds+c.length*u)*1e3;if(!await g(e,i,r,{expectedMaxDurationMs:t})){process.exitCode=1;return}}for(let n of i){a&&v(e,n);let i=await E(n.cwd,r,d,t);a&&!D(n,i)&&(process.exitCode=1,y(e,n))}}async function E(a,o,s,g){n(a);let{problemMarkdownFrontMatter:_,testCases:v,limits:y,timeLimitSeconds:b}=s,x=v[0]?.id??`prebuild`,S=await m(a,_);if(S)return e({testCaseId:x,...S}),{allAccepted:!1};let C=await f(a,o.language);if(!C)return e({testCaseId:x,decisionCode:c.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:`main file not found${o.language?`: language: ${o.language}`:``}`}),{allAccepted:!1};let w=p(C);if(!w)return e({testCaseId:x,decisionCode:c.WRONG_ANSWER,stderr:`unsupported language`}),{allAccepted:!1};let T={...process.env,CI:``,FORCE_COLOR:`0`},E=C;if(g.buildSubmission!==!1){if(w.prebuild)try{await w.prebuild(a);let e=await f(a,o.language);e&&(E=e)}catch(t){return e({testCaseId:x,decisionCode:c.BUILD_ERROR,stderr:t instanceof Error?t.message:String(t)}),{allAccepted:!1}}let t=w.buildCommand?.(E);if(t){let n=O(t,{cwd:a,env:T,testCaseId:x,limits:y});if(n)return e(n),{allAccepted:!1}}}let D=await u(a);if(v.length===0)return e({testCaseId:`default`,decisionCode:c.ACCEPTED}),{allAccepted:!0};let A=v.shared?.fileInputPath;for(let n of v){A&&await d(A,a),n.fileInputPath&&await d(n.fileInputPath,a);let o=w.command(E),s=n.input??``,u;try{g.resolveInput&&(s=await g.resolveInput({testCase:n,cwd:a,env:T})),u=g.runCommand?await h(()=>g.runCommand?.({testCase:n,command:r(o),stdin:s,cwd:a,env:{...T,...t(T)},timeLimitSeconds:b})):M(o,{stdin:s,cwd:a,env:T,timeLimitSeconds:b})}catch(t){return e({testCaseId:n.id,decisionCode:c.RUNTIME_ERROR,stdin:s,stderr:k(t)}),await l(a,D),{allAccepted:!1}}let f=await i(a,_.requiredOutputFilePaths??[]),p={timeLimitSeconds:b,outputLimitLength:y.maxOutputLength,problemMarkdownFrontMatter:{memoryLimitByte:_.memoryLimitByte,requiredOutputFilePaths:_.requiredOutputFilePaths}},m=N({runResult:u,outputFiles:f,context:p}),v=m;if(m.decisionCode===c.ACCEPTED)try{let e=g.test?await g.test({testCase:n,runResult:u,outputFiles:f,cwd:a,context:p}):await j({testCase:n,runResult:u,outputFiles:f,cwd:a});e&&(v={decisionCode:e.decisionCode??m.decisionCode,feedbackMarkdown:e.feedbackMarkdown,stderr:e.stderr})}catch(e){v={decisionCode:c.RUNTIME_ERROR,stderr:k(e)}}if(e({testCaseId:n.id,...v,exitStatus:u.status,stdin:u.stdin,stdout:u.stdout.slice(0,y.maxOutputLength)||void 0,stderr:(v.stderr??u.stderr).slice(0,y.maxOutputLength)||void 0,timeSeconds:u.timeSeconds,memoryBytes:u.memoryBytes,outputFiles:f.length>0?f:void 0}),await l(a,D),v.decisionCode!==c.ACCEPTED)return{allAccepted:!1}}return{allAccepted:!0}}function D(e,t){return t.allAccepted===(e.expectedResult===`accepted`)}function O(e,t){let n=x(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,env:t.env},t.limits.buildTimeoutSeconds),r=n.status??void 0;if(n.status!==0)return{testCaseId:t.testCaseId,decisionCode:c.BUILD_ERROR,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.timeSeconds>t.limits.buildTimeoutSeconds)return{testCaseId:t.testCaseId,decisionCode:c.BUILD_TIME_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.stdout.length>t.limits.maxOutputLength||n.stderr.length>t.limits.maxOutputLength)return{testCaseId:t.testCaseId,decisionCode:c.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function k(e){return e instanceof Error?e.message:String(e)}async function A(e){return await s(S.join(e,`test_cases`))}async function j(e){let{testCase:t,runResult:n,outputFiles:r,cwd:i}=e,o=await a({stdout:n.stdout,expectedStdout:typeof t.output==`string`?t.output:void 0,fileOutputPath:typeof t.fileOutputPath==`string`?t.fileOutputPath:void 0,cwd:i,outputFiles:r});return r.splice(0,r.length,...o.outputFiles),o.matches?{}:{decisionCode:c.WRONG_ANSWER}}function M(e,t){let n=x(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,input:t.stdin,env:t.env},t.timeLimitSeconds);return{stdin:t.stdin,stdout:n.stdout,stderr:n.stderr,status:n.status??void 0,timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function N(e){if(e.runResult.status!==0)return{decisionCode:c.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.timeSeconds>e.context.timeLimitSeconds)return{decisionCode:c.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:c.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.outputLimitExceeded||e.runResult.stdout.length>e.context.outputLimitLength||e.runResult.stderr.length>e.context.outputLimitLength)return{decisionCode:c.OUTPUT_SIZE_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:c.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:c.ACCEPTED}}export{T as commandJudgePreset};
1
+ import{printTestCaseResult as e}from"../helpers/printTestCaseResult.js";import{judgeAgainstExpectations as t}from"../helpers/compareExpectedOutputFiles.js";import{parseArgs as n}from"../helpers/parseArgs.js";import{readTestCases as r}from"../helpers/readTestCases.js";import{DecisionCode as i}from"../types/decisionCode.js";import{cleanWorkingDirectory as a,snapshotWorkingDirectory as o}from"../helpers/cleanWorkingDirectory.js";import{copyTestCaseFileInput as s}from"../helpers/copyTestCaseFileInput.js";import{findEntryPointFile as c}from"../helpers/findEntryPointFile.js";import{findLanguageDefinitionByPath as l}from"../helpers/findLanguageDefinitionByPath.js";import{judgeByStaticAnalysis as u}from"../helpers/judgeByStaticAnalysis.js";import{readOutputFiles as d}from"../helpers/readOutputFiles.js";import{passesIsolationCheckInDebugMode as f}from"../helpers/runIsolationCheckInDebugMode.js";import{readProblemMarkdownFrontMatter as p}from"../helpers/readProblemMarkdownFrontMatter.js";import{printDebugCwdBanner as m,printDebugExpectationFailureBanner as h,resolveCwds as g}from"../helpers/resolveCwds.js";import{spawnSyncWithTimeout as _}from"../helpers/spawnSyncWithTimeout.js";import v from"node:path";import{z as y}from"zod";const b=y.object({language:y.union([y.string(),y.array(y.string())]).optional()});async function x(e,t={}){let r=n(process.argv),i=b.parse(r.params),{cwds:a,isDebugMode:o}=await g(e,r.cwd),s=await p(e),c=await(t.readTestCases??E)(e),l={buildTimeoutSeconds:t.limits?.buildTimeoutSeconds??10,maxOutputLength:t.limits?.maxOutputLength??5e4},u=typeof s.timeLimitMs==`number`?s.timeLimitMs/1e3:t.runTimeoutSeconds??2,d={problemMarkdownFrontMatter:s,testCases:c,limits:l,timeLimitSeconds:u};if(o){let t=(l.buildTimeoutSeconds+c.length*u)*1e3;if(!await f(e,a,i,{expectedMaxDurationMs:t})){process.exitCode=1;return}}for(let n of a){o&&m(e,n);let r=await S(n.cwd,i,d,t);o&&!C(n,r)&&(process.exitCode=1,h(e,n))}}async function S(t,n,r,f){let{problemMarkdownFrontMatter:p,testCases:m,limits:h,timeLimitSeconds:g}=r,_=m[0]?.id??`prebuild`,v=await u(t,p);if(v)return e({testCaseId:_,...v}),{allAccepted:!1};let y=await c(t,n.language);if(!y)return e({testCaseId:_,decisionCode:i.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:`main file not found${n.language?`: language: ${n.language}`:``}`}),{allAccepted:!1};let b=l(y);if(!b)return e({testCaseId:_,decisionCode:i.WRONG_ANSWER,stderr:`unsupported language`}),{allAccepted:!1};let x={...process.env,CI:``,FORCE_COLOR:`0`},S=y;if(f.buildSubmission!==!1){if(b.prebuild)try{await b.prebuild(t);let e=await c(t,n.language);e&&(S=e)}catch(t){return e({testCaseId:_,decisionCode:i.BUILD_ERROR,stderr:t instanceof Error?t.message:String(t)}),{allAccepted:!1}}let r=b.buildCommand?.(S);if(r){let n=w(r,{cwd:t,env:x,testCaseId:_,limits:h});if(n)return e(n),{allAccepted:!1}}}let C=await o(t);if(m.length===0)return e({testCaseId:`default`,decisionCode:i.ACCEPTED}),{allAccepted:!0};let E=m.shared?.fileInputPath;for(let n of m){E&&await s(E,t),n.fileInputPath&&await s(n.fileInputPath,t);let r=b.command(S),o=n.input??``,c;try{f.resolveInput&&(o=await f.resolveInput({testCase:n,cwd:t,env:x})),c=f.runCommand?await f.runCommand({testCase:n,command:r,stdin:o,cwd:t,env:x,timeLimitSeconds:g}):O(r,{stdin:o,cwd:t,env:x,timeLimitSeconds:g})}catch(r){return e({testCaseId:n.id,decisionCode:i.RUNTIME_ERROR,stdin:o,stderr:T(r)}),await a(t,C),{allAccepted:!1}}let l=await d(t,p.requiredOutputFilePaths??[]),u={timeLimitSeconds:g,outputLimitLength:h.maxOutputLength,problemMarkdownFrontMatter:{memoryLimitByte:p.memoryLimitByte,requiredOutputFilePaths:p.requiredOutputFilePaths}},m=k({runResult:c,outputFiles:l,context:u}),_=m;if(m.decisionCode===i.ACCEPTED)try{let e=f.test?await f.test({testCase:n,runResult:c,outputFiles:l,cwd:t,context:u}):await D({testCase:n,runResult:c,outputFiles:l,cwd:t});e&&(_={decisionCode:e.decisionCode??m.decisionCode,feedbackMarkdown:e.feedbackMarkdown,stderr:e.stderr})}catch(e){_={decisionCode:i.RUNTIME_ERROR,stderr:T(e)}}if(e({testCaseId:n.id,..._,exitStatus:c.status,stdin:c.stdin,stdout:c.stdout.slice(0,h.maxOutputLength)||void 0,stderr:(_.stderr??c.stderr).slice(0,h.maxOutputLength)||void 0,timeSeconds:c.timeSeconds,memoryBytes:c.memoryBytes,outputFiles:l.length>0?l:void 0}),await a(t,C),_.decisionCode!==i.ACCEPTED)return{allAccepted:!1}}return{allAccepted:!0}}function C(e,t){return t.allAccepted===(e.expectedResult===`accepted`)}function w(e,t){let n=_(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,env:t.env},t.limits.buildTimeoutSeconds),r=n.status??void 0;if(n.status!==0)return{testCaseId:t.testCaseId,decisionCode:i.BUILD_ERROR,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.timeSeconds>t.limits.buildTimeoutSeconds)return{testCaseId:t.testCaseId,decisionCode:i.BUILD_TIME_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes};if(n.stdout.length>t.limits.maxOutputLength||n.stderr.length>t.limits.maxOutputLength)return{testCaseId:t.testCaseId,decisionCode:i.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,exitStatus:r,stdout:n.stdout.slice(0,t.limits.maxOutputLength),stderr:n.stderr.slice(0,t.limits.maxOutputLength),timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function T(e){return e instanceof Error?e.message:String(e)}async function E(e){return await r(v.join(e,`test_cases`))}async function D(e){let{testCase:n,runResult:r,outputFiles:a,cwd:o}=e,s=await t({stdout:r.stdout,expectedStdout:typeof n.output==`string`?n.output:void 0,fileOutputPath:typeof n.fileOutputPath==`string`?n.fileOutputPath:void 0,cwd:o,outputFiles:a});return a.splice(0,a.length,...s.outputFiles),s.matches?{}:{decisionCode:i.WRONG_ANSWER}}function O(e,t){let n=_(e[0],e.slice(1),{cwd:t.cwd,encoding:`utf8`,input:t.stdin,env:t.env},t.timeLimitSeconds);return{stdin:t.stdin,stdout:n.stdout,stderr:n.stderr,status:n.status??void 0,timeSeconds:n.timeSeconds,memoryBytes:n.memoryBytes}}function k(e){if(e.runResult.status!==0)return{decisionCode:i.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.timeSeconds>e.context.timeLimitSeconds)return{decisionCode:i.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:i.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.outputLimitExceeded||e.runResult.stdout.length>e.context.outputLimitLength||e.runResult.stderr.length>e.context.outputLimitLength)return{decisionCode:i.OUTPUT_SIZE_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:i.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:i.ACCEPTED}}export{x as commandJudgePreset};
2
2
  //# sourceMappingURL=command.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"command.js","names":["readFileTestCases"],"sources":["../../src/presets/command.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { judgeAgainstExpectations } from '../helpers/compareExpectedOutputFiles.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { runCustomRunner } from '../helpers/runCustomRunner.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n getSandboxUserEnvOverrides,\n makeAccessibleToSandboxUser,\n wrapCommandWithSandboxUser,\n} from '../helpers/sandboxUser.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 2;\nconst MAX_STDOUT_LENGTH = 50_000;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\ntype JudgeParams = z.infer<typeof judgeParamsSchema>;\n\n/** What every command test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseCommandTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface CommandTestCase extends BaseCommandTestCase {\n /** Expected standard output (`test_cases/<id>.out`). */\n output?: string;\n /** Directory of expected output files compared after the run (`test_cases/<id>.fout/`). */\n fileOutputPath?: string;\n}\n\ntype CommandJudgeCaseResult = Pick<TestCaseResult, 'decisionCode' | 'feedbackMarkdown' | 'stderr'>;\n\nexport interface CommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n outputLimitExceeded?: boolean;\n}\n\ninterface CommandJudgeContext {\n timeLimitSeconds: number;\n outputLimitLength: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\nexport interface CommandJudgeLimits {\n buildTimeoutSeconds: number;\n maxOutputLength: number;\n}\n\ninterface ResolvedCommandProblem<TTestCase extends BaseCommandTestCase> {\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n testCases: readonly TTestCase[];\n limits: CommandJudgeLimits;\n timeLimitSeconds: number;\n}\n\nexport interface CommandJudgePresetOptions<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n> {\n /**\n * Set to false when the custom runner builds the submitted source itself.\n * Defaults to true.\n */\n buildSubmission?: boolean;\n limits?: CommandJudgeLimits;\n /** The per-case time limit used only when `problem.md` declares no `timeLimitMs`. */\n runTimeoutSeconds?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n /**\n * Runs as the trusted harness user with the submission's `cwd`. Fixture files it creates there\n * must be written with `createDirectoryWithoutFollowingSymlinks`/`writeFileWithoutFollowingSymlinks`\n * (both exported): a submission can plant a symlink at a fixture path, and a plain `fs.writeFile`\n * would follow it into a file only the harness can write.\n */\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n runCommand?: (context: {\n testCase: TTestCase;\n /**\n * The command to run. Under `EXERCODE_SANDBOX_USER` delegation it is already wrapped so it\n * executes as the sandbox user; spawn it as given (with the supplied `env`) instead of\n * reconstructing it, or the submission runs as the trusted harness user.\n *\n * Its direct child is then a root-owned `sudo` whose descendants belong to the sandbox user, so\n * the handler cannot signal them: enforce `timeLimitSeconds` with `startSandboxTimeoutWatchdog`\n * and `killSandboxUserProcesses` (both exported) rather than `child.kill()` or an outer\n * `timeout`. The preset terminates leftover sandbox processes after the handler returns.\n */\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }) => Promise<TRunResult> | TRunResult;\n /**\n * Decides the verdict of a run that passed the limit checks. It replaces the default comparison\n * of `testCase.output` (space-separated tokens) and `testCase.fileOutputPath` (see\n * `compareStdoutAsSpaceSeparatedTokens` and `compareExpectedOutputFiles`, both exported), so\n * call those yourself when the test case ships `.out` / `.fout` expectations to keep.\n * `outputFiles` is the array printed with the result and may be edited in place.\n */\n test?: (context: {\n testCase: TTestCase;\n runResult: TRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: CommandJudgeContext;\n }) => Promise<Partial<CommandJudgeCaseResult>> | Partial<CommandJudgeCaseResult> | undefined;\n}\n\n/**\n * A preset function for judging by executable command.\n *\n * Without options, test cases come from `test_cases/` (`.in`, `.out`, `.fin/`, `.fout/`) and a run\n * within the limits is accepted when its stdout matches `.out` and its files match `.fout/`; a\n * case without either expectation only has to run within the limits.\n * Keep problem-specific logic in `resolveInput` and `test`.\n *\n * @example\n * Create `judge.ts` that judges `test_cases/` like the default stdio harness, with the command\n * preset's debug mode:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n *\n * await commandJudgePreset(import.meta.dirname);\n * ```\n *\n * @example\n * Create `judge.ts` with an own verdict:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n * import { DecisionCode } from '@exercode/problem-utils';\n *\n * await commandJudgePreset(import.meta.dirname, {\n * readTestCases: async () => [\n * { id: '01', input: '1 2' },\n * ],\n * test: ({ runResult }) => {\n * return runResult.stdout.trim() === '3'\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n *\n * Run with the required parameters:\n * ```bash\n * bun judge.ts model_answers/python '{ \"language\": \"python\" }'\n * ```\n *\n * Run without a cwd argument to judge each `<problemDir>/model_answers/*` directory\n * for debugging. A prominent banner is printed for each run.\n * ```bash\n * bun judge.ts\n * ```\n */\nexport async function commandJudgePreset<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(problemDir: string, options: CommandJudgePresetOptions<TTestCase, TRunResult> = {}): Promise<void> {\n const args = parseArgs(process.argv);\n const params = judgeParamsSchema.parse(args.params);\n\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const testCases = await (options.readTestCases ?? readCommandTestCases)(problemDir);\n const limits = {\n buildTimeoutSeconds: options.limits?.buildTimeoutSeconds ?? BUILD_TIMEOUT_SECONDS,\n maxOutputLength: options.limits?.maxOutputLength ?? MAX_STDOUT_LENGTH,\n };\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n const problem: ResolvedCommandProblem<TTestCase> = {\n problemMarkdownFrontMatter,\n testCases,\n limits,\n timeLimitSeconds,\n };\n\n if (isDebugMode) {\n const expectedMaxDurationMs = (limits.buildTimeoutSeconds + testCases.length * timeLimitSeconds) * 1000;\n if (!(await passesIsolationCheckInDebugMode(problemDir, cwds, params, { expectedMaxDurationMs }))) {\n process.exitCode = 1;\n return;\n }\n }\n\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await runCommandJudgeForCwd<TTestCase, TRunResult>(resolvedCwd.cwd, params, problem, options);\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function runCommandJudgeForCwd<\n TTestCase extends BaseCommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(\n cwd: string,\n params: JudgeParams,\n problem: ResolvedCommandProblem<TTestCase>,\n options: CommandJudgePresetOptions<TTestCase, TRunResult>\n): Promise<{ allAccepted: boolean }> {\n // The sandboxed submission must read its sources and write build/run outputs in its directory.\n makeAccessibleToSandboxUser(cwd);\n\n const { problemMarkdownFrontMatter, testCases, limits, timeLimitSeconds } = problem;\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return { allAccepted: false };\n }\n\n const originalMainFilePath = await findEntryPointFile(cwd, params.language);\n if (!originalMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return { allAccepted: false };\n }\n\n const languageDefinition = findLanguageDefinitionByPath(originalMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return { allAccepted: false };\n }\n\n // `CI` changes affects Chainlit. `FORCE_COLOR` affects Bun.\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let mainFilePath = originalMainFilePath;\n if (options.buildSubmission !== false) {\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(cwd);\n const prebuiltMainFilePath = await findEntryPointFile(cwd, params.language);\n if (prebuiltMainFilePath) mainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: error instanceof Error ? error.message : String(error),\n });\n return { allAccepted: false };\n }\n }\n\n const buildCommand = languageDefinition.buildCommand?.(mainFilePath);\n if (buildCommand) {\n const buildResult = runBuild(buildCommand, {\n cwd,\n env,\n testCaseId: prebuildTestCaseId,\n limits,\n });\n if (buildResult) {\n printTestCaseResult(buildResult);\n return { allAccepted: false };\n }\n }\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(cwd);\n\n if (testCases.length === 0) {\n printTestCaseResult({ testCaseId: 'default', decisionCode: DecisionCode.ACCEPTED });\n return { allAccepted: true };\n }\n\n const sharedFileInputPath = (testCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n\n for (const testCase of testCases) {\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, cwd);\n\n const command = languageDefinition.command(mainFilePath);\n let stdin = testCase.input ?? '';\n let runResult: TRunResult;\n try {\n if (options.resolveInput) {\n stdin = await options.resolveInput({ testCase, cwd, env });\n }\n\n runResult = options.runCommand\n ? await runCustomRunner(\n () =>\n // Hand custom runners a command that is already sandbox-wrapped, and the matching\n // environment: they spawn it themselves, so an unwrapped command would run the\n // submission as the trusted harness user and defeat the delegation boundary.\n options.runCommand?.({\n testCase,\n command: wrapCommandWithSandboxUser(command),\n stdin,\n cwd,\n env: { ...env, ...getSandboxUserEnvOverrides(env) },\n timeLimitSeconds,\n }) as Promise<TRunResult>\n )\n : (runCommand(command, {\n stdin,\n cwd,\n env,\n timeLimitSeconds,\n }) as TRunResult);\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n return { allAccepted: false };\n }\n\n const outputFiles = await readOutputFiles(cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: CommandJudgeContext = {\n timeLimitSeconds,\n outputLimitLength: limits.maxOutputLength,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateByLimits({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = options.test\n ? await options.test({ testCase, runResult, outputFiles, cwd, context: judgeContext })\n : await compareWithExpectedOutputs({ testCase, runResult, outputFiles, cwd });\n if (extendedJudgeResult) {\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n };\n }\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n printTestCaseResult({\n testCaseId: testCase.id,\n ...judgeResult,\n exitStatus: runResult.status,\n stdin: runResult.stdin,\n stdout: runResult.stdout.slice(0, limits.maxOutputLength) || undefined,\n stderr: (judgeResult.stderr ?? runResult.stderr).slice(0, limits.maxOutputLength) || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n outputFiles: outputFiles.length > 0 ? outputFiles : undefined,\n });\n\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n if (judgeResult.decisionCode !== DecisionCode.ACCEPTED) return { allAccepted: false };\n }\n\n return { allAccepted: true };\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: { allAccepted: boolean }): boolean {\n return result.allAccepted === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction runBuild(\n buildCommand: readonly [string, ...string[]],\n context: { cwd: string; env: NodeJS.ProcessEnv; testCaseId: string; limits: CommandJudgeLimits }\n): (Omit<TestCaseResult, 'testCaseId'> & { testCaseId: string }) | undefined {\n const spawnResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n context.limits.buildTimeoutSeconds\n );\n const exitStatus = spawnResult.status ?? undefined;\n\n if (spawnResult.status !== 0) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (spawnResult.timeSeconds > context.limits.buildTimeoutSeconds) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (\n spawnResult.stdout.length > context.limits.maxOutputLength ||\n spawnResult.stderr.length > context.limits.maxOutputLength\n ) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n return;\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nasync function readCommandTestCases<TTestCase extends BaseCommandTestCase = CommandTestCase>(\n problemDir: string\n): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\n/** The default verdict: a case exposing a string `output` / `fileOutputPath` is judged against it. */\nasync function compareWithExpectedOutputs(context: {\n testCase: BaseCommandTestCase & Partial<CommandTestCase>;\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n cwd: string;\n}): Promise<Partial<CommandJudgeCaseResult>> {\n const { testCase, runResult, outputFiles, cwd } = context;\n const judgement = await judgeAgainstExpectations({\n stdout: runResult.stdout,\n expectedStdout: typeof testCase.output === 'string' ? testCase.output : undefined,\n fileOutputPath: typeof testCase.fileOutputPath === 'string' ? testCase.fileOutputPath : undefined,\n cwd,\n outputFiles,\n });\n outputFiles.splice(0, outputFiles.length, ...judgement.outputFiles);\n return judgement.matches ? {} : { decisionCode: DecisionCode.WRONG_ANSWER };\n}\n\nfunction runCommand(\n command: readonly [string, ...string[]],\n context: {\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }\n): CommandRunResult {\n const spawnResult = spawnSyncWithTimeout(\n command[0],\n command.slice(1),\n { cwd: context.cwd, encoding: 'utf8', input: context.stdin, env: context.env },\n context.timeLimitSeconds\n );\n\n return {\n stdin: context.stdin,\n stdout: spawnResult.stdout,\n stderr: spawnResult.stderr,\n status: spawnResult.status ?? undefined,\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n}\n\nfunction evaluateByLimits(context: {\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: CommandJudgeContext;\n}): CommandJudgeCaseResult {\n if (context.runResult.status !== 0) {\n return { decisionCode: DecisionCode.RUNTIME_ERROR, stderr: context.runResult.stderr };\n }\n\n if (context.runResult.timeSeconds > context.context.timeLimitSeconds) {\n return { decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return { decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.outputLimitExceeded ||\n context.runResult.stdout.length > context.context.outputLimitLength ||\n context.runResult.stderr.length > context.context.outputLimitLength\n ) {\n return { decisionCode: DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n const requiredOutputFileCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFileCount) {\n return { decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n"],"mappings":"65CAiCA,MAIM,EAAoB,EAAE,OAAO,CACjC,SAAU,EAAE,MAAM,CAAC,EAAE,OAAO,EAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAChE,CAAC,EAwJD,eAAsB,EAGpB,EAAoB,EAA4D,CAAC,EAAkB,CACnG,IAAM,EAAO,EAAU,QAAQ,IAAI,EAC7B,EAAS,EAAkB,MAAM,EAAK,MAAM,EAE5C,CAAE,OAAM,eAAgB,MAAM,EAAY,EAAY,EAAK,GAAG,EAC9D,EAA6B,MAAM,EAA+B,CAAU,EAC5E,EAAY,MAAO,EAAQ,eAAiB,EAAA,CAAsB,CAAU,EAC5E,EAAS,CACb,oBAAqB,EAAQ,QAAQ,qBAAuB,GAC5D,gBAAiB,EAAQ,QAAQ,iBAAmB,GACtD,EACM,EACJ,OAAO,EAA2B,aAAgB,SAC9C,EAA2B,YAAc,IACxC,EAAQ,mBAAqB,EAC9B,EAA6C,CACjD,6BACA,YACA,SACA,kBACF,EAEA,GAAI,EAAa,CACf,IAAM,GAAyB,EAAO,oBAAsB,EAAU,OAAS,GAAoB,IACnG,GAAI,CAAE,MAAM,EAAgC,EAAY,EAAM,EAAQ,CAAE,uBAAsB,CAAC,EAAI,CACjG,QAAQ,SAAW,EACnB,MACF,CACF,CAEA,IAAK,IAAM,KAAe,EAAM,CAC1B,GAAa,EAAoB,EAAY,CAAW,EAC5D,IAAM,EAAS,MAAM,EAA6C,EAAY,IAAK,EAAQ,EAAS,CAAO,EACvG,GAAe,CAAC,EAAsB,EAAa,CAAM,IAC3D,QAAQ,SA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CAAC,EACf,CAAE,IAAK,EAAQ,IAAK,SAAU,OAAQ,MAAO,EAAQ,MAAO,IAAK,EAAQ,GAAI,EAC7E,EAAQ,gBACV,EAEA,MAAO,CACL,MAAO,EAAQ,MACf,OAAQ,EAAY,OACpB,OAAQ,EAAY,OACpB,OAAQ,EAAY,QAAU,IAAA,GAC9B,YAAa,EAAY,YACzB,YAAa,EAAY,WAC3B,CACF,CAEA,SAAS,EAAiB,EAIC,CACzB,GAAI,EAAQ,UAAU,SAAW,EAC/B,MAAO,CAAE,aAAc,EAAa,cAAe,OAAQ,EAAQ,UAAU,MAAO,EAGtF,GAAI,EAAQ,UAAU,YAAc,EAAQ,QAAQ,iBAClD,MAAO,CAAE,aAAc,EAAa,oBAAqB,OAAQ,EAAQ,UAAU,MAAO,EAG5F,GACE,EAAQ,UAAU,aACjB,EAAQ,QAAQ,2BAA2B,iBAAmB,KAE/D,MAAO,CAAE,aAAc,EAAa,sBAAuB,OAAQ,EAAQ,UAAU,MAAO,EAG9F,GACE,EAAQ,UAAU,qBAClB,EAAQ,UAAU,OAAO,OAAS,EAAQ,QAAQ,mBAClD,EAAQ,UAAU,OAAO,OAAS,EAAQ,QAAQ,kBAElD,MAAO,CAAE,aAAc,EAAa,2BAA4B,OAAQ,EAAQ,UAAU,MAAO,EAGnG,IAAM,EAA0B,EAAQ,QAAQ,2BAA2B,yBAAyB,QAAU,EAK9G,OAJI,EAAQ,YAAY,OAAS,EACxB,CAAE,aAAc,EAAa,kCAAmC,EAGlE,CAAE,aAAc,EAAa,QAAS,CAC/C"}
1
+ {"version":3,"file":"command.js","names":["readFileTestCases"],"sources":["../../src/presets/command.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { judgeAgainstExpectations } from '../helpers/compareExpectedOutputFiles.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 2;\nconst MAX_STDOUT_LENGTH = 50_000;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\ntype JudgeParams = z.infer<typeof judgeParamsSchema>;\n\n/** What every command test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseCommandTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface CommandTestCase extends BaseCommandTestCase {\n /** Expected standard output (`test_cases/<id>.out`). */\n output?: string;\n /** Directory of expected output files compared after the run (`test_cases/<id>.fout/`). */\n fileOutputPath?: string;\n}\n\ntype CommandJudgeCaseResult = Pick<TestCaseResult, 'decisionCode' | 'feedbackMarkdown' | 'stderr'>;\n\nexport interface CommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n outputLimitExceeded?: boolean;\n}\n\ninterface CommandJudgeContext {\n timeLimitSeconds: number;\n outputLimitLength: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\nexport interface CommandJudgeLimits {\n buildTimeoutSeconds: number;\n maxOutputLength: number;\n}\n\ninterface ResolvedCommandProblem<TTestCase extends BaseCommandTestCase> {\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n testCases: readonly TTestCase[];\n limits: CommandJudgeLimits;\n timeLimitSeconds: number;\n}\n\nexport interface CommandJudgePresetOptions<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n> {\n /**\n * Set to false when the custom runner builds the submitted source itself.\n * Defaults to true.\n */\n buildSubmission?: boolean;\n limits?: CommandJudgeLimits;\n /** The per-case time limit used only when `problem.md` declares no `timeLimitMs`. */\n runTimeoutSeconds?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n runCommand?: (context: {\n testCase: TTestCase;\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }) => Promise<TRunResult> | TRunResult;\n /**\n * Decides the verdict of a run that passed the limit checks. It replaces the default comparison\n * of `testCase.output` (space-separated tokens) and `testCase.fileOutputPath` (see\n * `compareStdoutAsSpaceSeparatedTokens` and `compareExpectedOutputFiles`, both exported), so\n * call those yourself when the test case ships `.out` / `.fout` expectations to keep.\n * `outputFiles` is the array printed with the result and may be edited in place.\n */\n test?: (context: {\n testCase: TTestCase;\n runResult: TRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: CommandJudgeContext;\n }) => Promise<Partial<CommandJudgeCaseResult>> | Partial<CommandJudgeCaseResult> | undefined;\n}\n\n/**\n * A preset function for judging by executable command.\n *\n * Without options, test cases come from `test_cases/` (`.in`, `.out`, `.fin/`, `.fout/`) and a run\n * within the limits is accepted when its stdout matches `.out` and its files match `.fout/`; a\n * case without either expectation only has to run within the limits.\n * Keep problem-specific logic in `resolveInput` and `test`.\n *\n * @example\n * Create `judge.ts` that judges `test_cases/` like the default stdio harness, with the command\n * preset's debug mode:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n *\n * await commandJudgePreset(import.meta.dirname);\n * ```\n *\n * @example\n * Create `judge.ts` with an own verdict:\n * ```ts\n * import { commandJudgePreset } from '@exercode/problem-utils/presets/command';\n * import { DecisionCode } from '@exercode/problem-utils';\n *\n * await commandJudgePreset(import.meta.dirname, {\n * readTestCases: async () => [\n * { id: '01', input: '1 2' },\n * ],\n * test: ({ runResult }) => {\n * return runResult.stdout.trim() === '3'\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n *\n * Run with the required parameters:\n * ```bash\n * bun judge.ts model_answers/python '{ \"language\": \"python\" }'\n * ```\n *\n * Run without a cwd argument to judge each `<problemDir>/model_answers/*` directory\n * for debugging. A prominent banner is printed for each run.\n * ```bash\n * bun judge.ts\n * ```\n */\nexport async function commandJudgePreset<\n TTestCase extends BaseCommandTestCase = CommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(problemDir: string, options: CommandJudgePresetOptions<TTestCase, TRunResult> = {}): Promise<void> {\n const args = parseArgs(process.argv);\n const params = judgeParamsSchema.parse(args.params);\n\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const testCases = await (options.readTestCases ?? readCommandTestCases)(problemDir);\n const limits = {\n buildTimeoutSeconds: options.limits?.buildTimeoutSeconds ?? BUILD_TIMEOUT_SECONDS,\n maxOutputLength: options.limits?.maxOutputLength ?? MAX_STDOUT_LENGTH,\n };\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n const problem: ResolvedCommandProblem<TTestCase> = {\n problemMarkdownFrontMatter,\n testCases,\n limits,\n timeLimitSeconds,\n };\n\n if (isDebugMode) {\n const expectedMaxDurationMs = (limits.buildTimeoutSeconds + testCases.length * timeLimitSeconds) * 1000;\n if (!(await passesIsolationCheckInDebugMode(problemDir, cwds, params, { expectedMaxDurationMs }))) {\n process.exitCode = 1;\n return;\n }\n }\n\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await runCommandJudgeForCwd<TTestCase, TRunResult>(resolvedCwd.cwd, params, problem, options);\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function runCommandJudgeForCwd<\n TTestCase extends BaseCommandTestCase,\n TRunResult extends CommandRunResult = CommandRunResult,\n>(\n cwd: string,\n params: JudgeParams,\n problem: ResolvedCommandProblem<TTestCase>,\n options: CommandJudgePresetOptions<TTestCase, TRunResult>\n): Promise<{ allAccepted: boolean }> {\n const { problemMarkdownFrontMatter, testCases, limits, timeLimitSeconds } = problem;\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return { allAccepted: false };\n }\n\n const originalMainFilePath = await findEntryPointFile(cwd, params.language);\n if (!originalMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return { allAccepted: false };\n }\n\n const languageDefinition = findLanguageDefinitionByPath(originalMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return { allAccepted: false };\n }\n\n // `CI` changes affects Chainlit. `FORCE_COLOR` affects Bun.\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let mainFilePath = originalMainFilePath;\n if (options.buildSubmission !== false) {\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(cwd);\n const prebuiltMainFilePath = await findEntryPointFile(cwd, params.language);\n if (prebuiltMainFilePath) mainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: error instanceof Error ? error.message : String(error),\n });\n return { allAccepted: false };\n }\n }\n\n const buildCommand = languageDefinition.buildCommand?.(mainFilePath);\n if (buildCommand) {\n const buildResult = runBuild(buildCommand, {\n cwd,\n env,\n testCaseId: prebuildTestCaseId,\n limits,\n });\n if (buildResult) {\n printTestCaseResult(buildResult);\n return { allAccepted: false };\n }\n }\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(cwd);\n\n if (testCases.length === 0) {\n printTestCaseResult({ testCaseId: 'default', decisionCode: DecisionCode.ACCEPTED });\n return { allAccepted: true };\n }\n\n const sharedFileInputPath = (testCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n\n for (const testCase of testCases) {\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, cwd);\n\n const command = languageDefinition.command(mainFilePath);\n let stdin = testCase.input ?? '';\n let runResult: TRunResult;\n try {\n if (options.resolveInput) {\n stdin = await options.resolveInput({ testCase, cwd, env });\n }\n\n runResult = options.runCommand\n ? await options.runCommand({ testCase, command, stdin, cwd, env, timeLimitSeconds })\n : (runCommand(command, {\n stdin,\n cwd,\n env,\n timeLimitSeconds,\n }) as TRunResult);\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n return { allAccepted: false };\n }\n\n const outputFiles = await readOutputFiles(cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: CommandJudgeContext = {\n timeLimitSeconds,\n outputLimitLength: limits.maxOutputLength,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateByLimits({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = options.test\n ? await options.test({ testCase, runResult, outputFiles, cwd, context: judgeContext })\n : await compareWithExpectedOutputs({ testCase, runResult, outputFiles, cwd });\n if (extendedJudgeResult) {\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n };\n }\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n printTestCaseResult({\n testCaseId: testCase.id,\n ...judgeResult,\n exitStatus: runResult.status,\n stdin: runResult.stdin,\n stdout: runResult.stdout.slice(0, limits.maxOutputLength) || undefined,\n stderr: (judgeResult.stderr ?? runResult.stderr).slice(0, limits.maxOutputLength) || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n outputFiles: outputFiles.length > 0 ? outputFiles : undefined,\n });\n\n await cleanWorkingDirectory(cwd, cwdSnapshot);\n if (judgeResult.decisionCode !== DecisionCode.ACCEPTED) return { allAccepted: false };\n }\n\n return { allAccepted: true };\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: { allAccepted: boolean }): boolean {\n return result.allAccepted === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction runBuild(\n buildCommand: readonly [string, ...string[]],\n context: { cwd: string; env: NodeJS.ProcessEnv; testCaseId: string; limits: CommandJudgeLimits }\n): (Omit<TestCaseResult, 'testCaseId'> & { testCaseId: string }) | undefined {\n const spawnResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n context.limits.buildTimeoutSeconds\n );\n const exitStatus = spawnResult.status ?? undefined;\n\n if (spawnResult.status !== 0) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (spawnResult.timeSeconds > context.limits.buildTimeoutSeconds) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n if (\n spawnResult.stdout.length > context.limits.maxOutputLength ||\n spawnResult.stderr.length > context.limits.maxOutputLength\n ) {\n return {\n testCaseId: context.testCaseId,\n decisionCode: DecisionCode.BUILD_OUTPUT_SIZE_LIMIT_EXCEEDED,\n exitStatus,\n stdout: spawnResult.stdout.slice(0, context.limits.maxOutputLength),\n stderr: spawnResult.stderr.slice(0, context.limits.maxOutputLength),\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n }\n\n return;\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nasync function readCommandTestCases<TTestCase extends BaseCommandTestCase = CommandTestCase>(\n problemDir: string\n): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\n/** The default verdict: a case exposing a string `output` / `fileOutputPath` is judged against it. */\nasync function compareWithExpectedOutputs(context: {\n testCase: BaseCommandTestCase & Partial<CommandTestCase>;\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n cwd: string;\n}): Promise<Partial<CommandJudgeCaseResult>> {\n const { testCase, runResult, outputFiles, cwd } = context;\n const judgement = await judgeAgainstExpectations({\n stdout: runResult.stdout,\n expectedStdout: typeof testCase.output === 'string' ? testCase.output : undefined,\n fileOutputPath: typeof testCase.fileOutputPath === 'string' ? testCase.fileOutputPath : undefined,\n cwd,\n outputFiles,\n });\n outputFiles.splice(0, outputFiles.length, ...judgement.outputFiles);\n return judgement.matches ? {} : { decisionCode: DecisionCode.WRONG_ANSWER };\n}\n\nfunction runCommand(\n command: readonly [string, ...string[]],\n context: {\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n }\n): CommandRunResult {\n const spawnResult = spawnSyncWithTimeout(\n command[0],\n command.slice(1),\n { cwd: context.cwd, encoding: 'utf8', input: context.stdin, env: context.env },\n context.timeLimitSeconds\n );\n\n return {\n stdin: context.stdin,\n stdout: spawnResult.stdout,\n stderr: spawnResult.stderr,\n status: spawnResult.status ?? undefined,\n timeSeconds: spawnResult.timeSeconds,\n memoryBytes: spawnResult.memoryBytes,\n };\n}\n\nfunction evaluateByLimits(context: {\n runResult: CommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: CommandJudgeContext;\n}): CommandJudgeCaseResult {\n if (context.runResult.status !== 0) {\n return { decisionCode: DecisionCode.RUNTIME_ERROR, stderr: context.runResult.stderr };\n }\n\n if (context.runResult.timeSeconds > context.context.timeLimitSeconds) {\n return { decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return { decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n if (\n context.runResult.outputLimitExceeded ||\n context.runResult.stdout.length > context.context.outputLimitLength ||\n context.runResult.stderr.length > context.context.outputLimitLength\n ) {\n return { decisionCode: DecisionCode.OUTPUT_SIZE_LIMIT_EXCEEDED, stderr: context.runResult.stderr };\n }\n\n const requiredOutputFileCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFileCount) {\n return { decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n"],"mappings":"qtCA2BA,MAIM,EAAoB,EAAE,OAAO,CACjC,SAAU,EAAE,MAAM,CAAC,EAAE,OAAO,EAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAChE,CAAC,EAwID,eAAsB,EAGpB,EAAoB,EAA4D,CAAC,EAAkB,CACnG,IAAM,EAAO,EAAU,QAAQ,IAAI,EAC7B,EAAS,EAAkB,MAAM,EAAK,MAAM,EAE5C,CAAE,OAAM,eAAgB,MAAM,EAAY,EAAY,EAAK,GAAG,EAC9D,EAA6B,MAAM,EAA+B,CAAU,EAC5E,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,OAAO,OAAS,EAAQ,QAAQ,kBAElD,MAAO,CAAE,aAAc,EAAa,2BAA4B,OAAQ,EAAQ,UAAU,MAAO,EAGnG,IAAM,EAA0B,EAAQ,QAAQ,2BAA2B,yBAAyB,QAAU,EAK9G,OAJI,EAAQ,YAAY,OAAS,EACxB,CAAE,aAAc,EAAa,kCAAmC,EAGlE,CAAE,aAAc,EAAa,QAAS,CAC/C"}
@@ -1,11 +1,11 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/readOutputFiles.cjs"),r=require("../helpers/parseArgs.cjs"),i=require("../helpers/parseCsv.cjs"),a=require("../types/decisionCode.cjs"),o=require("../helpers/judgeByStaticAnalysis.cjs"),s=require("../helpers/runIsolationCheckInDebugMode.cjs"),c=require("../helpers/readProblemMarkdownFrontMatter.cjs"),l=require("../helpers/resolveCwds.cjs");let u=require("node:path");u=e.__toESM(u,1);let d=require("node:fs/promises");d=e.__toESM(d,1);const f={unterminated_quote:'引用符(`"`)が閉じられていません',unexpected_quote:'引用符(`"`)は値全体を囲む位置にしか書けません',text_after_quoted_field:`閉じ引用符の後に区切り文字以外の文字があります`},p={rmsle:{label:`RMSLE`,isHigherBetter:!1,validatePrediction:e=>E(e)??(Number(e)<0?`負の値です`:void 0),compute:(e,t)=>Math.sqrt(D(e.map((e,n)=>(Math.log1p(Number(e))-Math.log1p(Number(t[n])))**2)))},rmse:{label:`RMSE`,isHigherBetter:!1,validatePrediction:E,compute:(e,t)=>Math.sqrt(D(e.map((e,n)=>(Number(e)-Number(t[n]))**2)))},mae:{label:`MAE`,isHigherBetter:!1,validatePrediction:E,compute:(e,t)=>D(e.map((e,n)=>Math.abs(Number(e)-Number(t[n]))))},accuracy:{label:`Accuracy`,isHigherBetter:!0,validatePrediction:e=>e.trim()===``?`空です`:void 0,compute:(e,t)=>D(e.map((e,n)=>+(e.trim()===t[n]?.trim())))}};async function m(e,n){let i=r.parseArgs(process.argv),{cwds:a,isDebugMode:o}=await l.resolveCwds(e,i.cwd);if(o&&!await s.passesIsolationCheckInDebugMode(e,a,i.params)){process.exitCode=1;return}let c=await b(u.default.join(e,n.answerFilePath),n);for(let r of a){o&&l.printDebugCwdBanner(e,r);let i=await h(e,r.cwd,c,n);t.printTestCaseResult({testCaseId:`evaluation`,...i}),o&&!T(r,i)&&(process.exitCode=1,l.printDebugExpectationFailureBanner(e,r))}}async function h(e,t,r,i){let s=await o.judgeByStaticAnalysis(t,await c.readProblemMarkdownFrontMatter(e));if(s)return s;let l=i.submissionFilePath??`submission.csv`,f;try{let e=u.default.join(t,l);if(!await n.isSafeSubmissionOutputPath(t,e))throw Error(`unsafe path`);f=await d.default.readFile(e,`utf8`)}catch{return{decisionCode:a.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`\`${l}\`が見つかりません。予測結果のCSVファイルを\`${l}\`という名前で提出してください。`}}let p=y(f,l,r,i);if(typeof p==`string`)return{decisionCode:a.DecisionCode.WRONG_ANSWER,feedbackMarkdown:p};let m=[...r.values()],h=i.metric.compute([...r.keys()].map(e=>p.get(e)),m);if(!Number.isFinite(h))return{decisionCode:a.DecisionCode.WRONG_ANSWER,feedbackMarkdown:`予測値が極端なため、${i.metric.label}を計算できませんでした。予測値を見直してください。`};let _=i.acceptableScore===void 0||(i.metric.isHigherBetter?h>=i.acceptableScore:h<=i.acceptableScore);return{decisionCode:_?a.DecisionCode.ACCEPTED:a.DecisionCode.WRONG_ANSWER,score:h,scoreLabel:i.metric.label,feedbackMarkdown:g(h,m.length,_,i)}}function g(e,t,n,r){let{label:i,isHigherBetter:a}=r.metric,o=_(e,r.acceptableScore),s=r.acceptableScore===void 0?``:`\n合格基準: ${i} ${a?`≧`:`≦`} ${v(r.acceptableScore,o)}(${n?`達成`:`未達成`})`;return`| 指標 | スコア |
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/parseArgs.cjs"),r=require("../helpers/parseCsv.cjs"),i=require("../types/decisionCode.cjs"),a=require("../helpers/judgeByStaticAnalysis.cjs"),o=require("../helpers/runIsolationCheckInDebugMode.cjs"),s=require("../helpers/readProblemMarkdownFrontMatter.cjs"),c=require("../helpers/resolveCwds.cjs");let l=require("node:path");l=e.__toESM(l,1);let u=require("node:fs/promises");u=e.__toESM(u,1);const d={unterminated_quote:'引用符(`"`)が閉じられていません',unexpected_quote:'引用符(`"`)は値全体を囲む位置にしか書けません',text_after_quoted_field:`閉じ引用符の後に区切り文字以外の文字があります`},f={rmsle:{label:`RMSLE`,isHigherBetter:!1,validatePrediction:e=>T(e)??(Number(e)<0?`負の値です`:void 0),compute:(e,t)=>Math.sqrt(E(e.map((e,n)=>(Math.log1p(Number(e))-Math.log1p(Number(t[n])))**2)))},rmse:{label:`RMSE`,isHigherBetter:!1,validatePrediction:T,compute:(e,t)=>Math.sqrt(E(e.map((e,n)=>(Number(e)-Number(t[n]))**2)))},mae:{label:`MAE`,isHigherBetter:!1,validatePrediction:T,compute:(e,t)=>E(e.map((e,n)=>Math.abs(Number(e)-Number(t[n]))))},accuracy:{label:`Accuracy`,isHigherBetter:!0,validatePrediction:e=>e.trim()===``?`空です`:void 0,compute:(e,t)=>E(e.map((e,n)=>+(e.trim()===t[n]?.trim())))}};async function p(e,r){let i=n.parseArgs(process.argv),{cwds:a,isDebugMode:s}=await c.resolveCwds(e,i.cwd);if(s&&!await o.passesIsolationCheckInDebugMode(e,a,i.params)){process.exitCode=1;return}let u=await y(l.default.join(e,r.answerFilePath),r);for(let n of a){s&&c.printDebugCwdBanner(e,n);let i=await m(e,n.cwd,u,r);t.printTestCaseResult({testCaseId:`evaluation`,...i}),s&&!w(n,i)&&(process.exitCode=1,c.printDebugExpectationFailureBanner(e,n))}}async function m(e,t,n,r){let o=await a.judgeByStaticAnalysis(t,await s.readProblemMarkdownFrontMatter(e));if(o)return o;let c=r.submissionFilePath??`submission.csv`,d;try{let e=l.default.join(t,c);d=await u.default.readFile(e,`utf8`)}catch{return{decisionCode:i.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`\`${c}\`が見つかりません。予測結果のCSVファイルを\`${c}\`という名前で提出してください。`}}let f=v(d,c,n,r);if(typeof f==`string`)return{decisionCode:i.DecisionCode.WRONG_ANSWER,feedbackMarkdown:f};let p=[...n.values()],m=r.metric.compute([...n.keys()].map(e=>f.get(e)),p);if(!Number.isFinite(m))return{decisionCode:i.DecisionCode.WRONG_ANSWER,feedbackMarkdown:`予測値が極端なため、${r.metric.label}を計算できませんでした。予測値を見直してください。`};let g=r.acceptableScore===void 0||(r.metric.isHigherBetter?m>=r.acceptableScore:m<=r.acceptableScore);return{decisionCode:g?i.DecisionCode.ACCEPTED:i.DecisionCode.WRONG_ANSWER,score:m,scoreLabel:r.metric.label,feedbackMarkdown:h(m,p.length,g,r)}}function h(e,t,n,r){let{label:i,isHigherBetter:a}=r.metric,o=g(e,r.acceptableScore),s=r.acceptableScore===void 0?``:`\n合格基準: ${i} ${a?`≧`:`≦`} ${_(r.acceptableScore,o)}(${n?`達成`:`未達成`})`;return`| 指標 | スコア |
2
2
  | ---- | ------ |
3
- | ${i} | ${v(e,o)} |
3
+ | ${i} | ${_(e,o)} |
4
4
 
5
5
  評価件数: ${t.toLocaleString(`en-US`)}件${s}
6
- `}function _(e,t){if(t===void 0||e===t)return 6;for(let n=6;n<17;n++)if(v(e,n)!==v(t,n))return n;return 17}function v(e,t){return Number(e.toPrecision(t)).toString()}function y(e,t,n,r){let a;try{a=i.parseCsvRecords(e)}catch(e){return`\`${t}\`をCSVとして読み込めませんでした。${e instanceof i.CsvParseError?`${e.lineNumber}行目付近: ${f[e.reason]}`:``}`}let o=x(a[0]?.cells??[],r);if(!o)return`\`${t}\`の1行目には\`${r.idColumn}\`列と\`${r.targetColumn}\`列のヘッダーが必要です。`;let s=new Map,c=[],l=0,u=e=>{l++,c.length<10&&c.push(e)};for(let{cells:e,lineNumber:t}of a.slice(1)){if(S(e))continue;let i=e[o.idIndex]?.trim()??``,a=e[o.targetIndex]??``;if(!n.has(i))u(`${t}行目: \`${r.idColumn}\`が\`${w(i)}\`の行は評価対象ではありません`);else if(s.has(i))u(`${t}行目: \`${r.idColumn}\`が\`${w(i)}\`の行が重複しています`);else{let e=r.metric.validatePrediction?.(a);e&&u(`${t}行目: \`${r.targetColumn}\`の値\`${w(a)}\`は${e}`),s.set(i,a)}}let d=[...n.keys()].filter(e=>!s.has(e));return d.length>0&&(l++,c.unshift(`\`${r.idColumn}\`が${C(d)}の行がありません(不足 ${d.length.toLocaleString(`en-US`)}件)`)),l>0?`\`${t}\`の内容に問題があります。
6
+ `}function g(e,t){if(t===void 0||e===t)return 6;for(let n=6;n<17;n++)if(_(e,n)!==_(t,n))return n;return 17}function _(e,t){return Number(e.toPrecision(t)).toString()}function v(e,t,n,i){let a;try{a=r.parseCsvRecords(e)}catch(e){return`\`${t}\`をCSVとして読み込めませんでした。${e instanceof r.CsvParseError?`${e.lineNumber}行目付近: ${d[e.reason]}`:``}`}let o=b(a[0]?.cells??[],i);if(!o)return`\`${t}\`の1行目には\`${i.idColumn}\`列と\`${i.targetColumn}\`列のヘッダーが必要です。`;let s=new Map,c=[],l=0,u=e=>{l++,c.length<10&&c.push(e)};for(let{cells:e,lineNumber:t}of a.slice(1)){if(x(e))continue;let r=e[o.idIndex]?.trim()??``,a=e[o.targetIndex]??``;if(!n.has(r))u(`${t}行目: \`${i.idColumn}\`が\`${C(r)}\`の行は評価対象ではありません`);else if(s.has(r))u(`${t}行目: \`${i.idColumn}\`が\`${C(r)}\`の行が重複しています`);else{let e=i.metric.validatePrediction?.(a);e&&u(`${t}行目: \`${i.targetColumn}\`の値\`${C(a)}\`は${e}`),s.set(r,a)}}let f=[...n.keys()].filter(e=>!s.has(e));return f.length>0&&(l++,c.unshift(`\`${i.idColumn}\`が${S(f)}の行がありません(不足 ${f.length.toLocaleString(`en-US`)}件)`)),l>0?`\`${t}\`の内容に問題があります。
7
7
 
8
8
  ${c.map(e=>`- ${e}`).join(`
9
9
  `)}${l>c.length?`\n- ほか${(l-c.length).toLocaleString(`en-US`)}件`:``}
10
- `:s}async function b(e,t){let n=i.parseCsv(await d.default.readFile(e,`utf8`)),r=x(n[0]??[],t);if(!r)throw Error(`answer file must have ${t.idColumn} and ${t.targetColumn} columns: ${e}`);let a=new Map;for(let i of n.slice(1)){if(S(i))continue;let n=i[r.idIndex]?.trim()??``,o=i[r.targetIndex]??``;if(!n)throw Error(`empty ${t.idColumn} in answer file: ${e}`);if(a.has(n))throw Error(`duplicate ${t.idColumn} in answer file: ${n}`);let s=t.metric.validatePrediction?.(o);if(s)throw Error(`invalid ${t.targetColumn} in answer file for ${n}: ${s}`);a.set(n,o)}if(a.size===0)throw Error(`answer file has no rows: ${e}`);return a}function x(e,t){let n=e.map(e=>e.trim()),r=n.indexOf(t.idColumn),i=n.indexOf(t.targetColumn);return r===-1||i===-1?void 0:{idIndex:r,targetIndex:i}}function S(e){return e.every(e=>e.trim()===``)}function C(e){let t=e.slice(0,10).map(e=>`\`${w(e)}\``);return e.length>10?`${t.join(`, `)}, ...`:t.join(`, `)}function w(e){let t=e.replaceAll(/[\p{Cc}]/gu,` `).replaceAll("`",`'`);return t.trim()===``?`(空)`:t.length>50?`${t.slice(0,50)}…`:t}function T(e,t){return t.decisionCode===a.DecisionCode.ACCEPTED==(e.expectedResult===`accepted`)}function E(e){return Number.isFinite(Number(e))&&e.trim()!==``?void 0:`数値ではありません`}function D(e){return e.reduce((e,t)=>e+t,0)/e.length}exports.evaluationJudgePreset=m,exports.evaluationMetrics=p;
10
+ `:s}async function y(e,t){let n=r.parseCsv(await u.default.readFile(e,`utf8`)),i=b(n[0]??[],t);if(!i)throw Error(`answer file must have ${t.idColumn} and ${t.targetColumn} columns: ${e}`);let a=new Map;for(let r of n.slice(1)){if(x(r))continue;let n=r[i.idIndex]?.trim()??``,o=r[i.targetIndex]??``;if(!n)throw Error(`empty ${t.idColumn} in answer file: ${e}`);if(a.has(n))throw Error(`duplicate ${t.idColumn} in answer file: ${n}`);let s=t.metric.validatePrediction?.(o);if(s)throw Error(`invalid ${t.targetColumn} in answer file for ${n}: ${s}`);a.set(n,o)}if(a.size===0)throw Error(`answer file has no rows: ${e}`);return a}function b(e,t){let n=e.map(e=>e.trim()),r=n.indexOf(t.idColumn),i=n.indexOf(t.targetColumn);return r===-1||i===-1?void 0:{idIndex:r,targetIndex:i}}function x(e){return e.every(e=>e.trim()===``)}function S(e){let t=e.slice(0,10).map(e=>`\`${C(e)}\``);return e.length>10?`${t.join(`, `)}, ...`:t.join(`, `)}function C(e){let t=e.replaceAll(/[\p{Cc}]/gu,` `).replaceAll("`",`'`);return t.trim()===``?`(空)`:t.length>50?`${t.slice(0,50)}…`:t}function w(e,t){return t.decisionCode===i.DecisionCode.ACCEPTED==(e.expectedResult===`accepted`)}function T(e){return Number.isFinite(Number(e))&&e.trim()!==``?void 0:`数値ではありません`}function E(e){return e.reduce((e,t)=>e+t,0)/e.length}exports.evaluationJudgePreset=p,exports.evaluationMetrics=f;
11
11
  //# sourceMappingURL=evaluation.cjs.map