From ba6b6d4fde7f4039582fa5e9027dd82dd309c9e5 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 16:54:01 +0900 Subject: [PATCH 1/6] feat: export selectDebugInputFilePaths for the stdio debug input files A client that ships only learner-visible files to debug a problem needs to know which input files the stdio debug preset hands to the program. Export a pure helper that selects them from problem-relative paths, and make the preset derive its choice from the same rule. Co-authored-by: WillBooster (Claude Code) --- .../src/helpers/readTestCases.ts | 11 ++- .../src/helpers/selectDebugInputFilePaths.ts | 42 +++++++++++ packages/problem-utils/src/index.ts | 1 + packages/problem-utils/src/presets/stdio.ts | 44 +++++++---- test/e2e/stdioDebugInputFiles.test.ts | 75 +++++++++++++++++++ 5 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts create mode 100644 test/e2e/stdioDebugInputFiles.test.ts diff --git a/packages/problem-utils/src/helpers/readTestCases.ts b/packages/problem-utils/src/helpers/readTestCases.ts index 913eafe5..9e039491 100644 --- a/packages/problem-utils/src/helpers/readTestCases.ts +++ b/packages/problem-utils/src/helpers/readTestCases.ts @@ -1,7 +1,8 @@ import fs from 'node:fs'; import path from 'node:path'; -const SHARED_TEST_CASE_NAME = '_shared'; +/** The `test_cases/` entry name (`_shared.fin/`) whose input files every run receives besides its own case's. */ +export const SHARED_TEST_CASE_ID = '_shared'; /** * Read the test cases of a `test_cases/` directory. A test case id is the name of every @@ -9,6 +10,8 @@ const SHARED_TEST_CASE_NAME = '_shared'; * and what an absent part means is up to the harness (the presets document their rules). * Other entries (e.g. `.json` configuration read by a custom `judge.ts`) are ignored. */ +// code-gauge-allow functionCognitiveComplexity 26 -- one pass over the directory entries classifies every entry kind; splitting it would only pass the five maps around +// code-gauge-allow functionNcss 33 -- one pass over the directory entries classifies every entry kind; splitting it would only pass the five maps around export async function readTestCases(directory: string): Promise< { id: string; input?: string; output?: string; fileInputPath?: string; fileOutputPath?: string }[] & { shared?: { fileInputPath?: string }; @@ -43,7 +46,7 @@ export async function readTestCases(directory: string): Promise< } const testCases: Awaited> = [...idSet] - .filter((id) => id !== SHARED_TEST_CASE_NAME) + .filter((id) => id !== SHARED_TEST_CASE_ID) .toSorted() .map((id) => ({ id, @@ -53,8 +56,8 @@ export async function readTestCases(directory: string): Promise< fileOutputPath: idToFileOutputPath.get(id), })); - if (idSet.has(SHARED_TEST_CASE_NAME)) { - testCases.shared = { fileInputPath: idToFileInputPath.get(SHARED_TEST_CASE_NAME) }; + if (idSet.has(SHARED_TEST_CASE_ID)) { + testCases.shared = { fileInputPath: idToFileInputPath.get(SHARED_TEST_CASE_ID) }; } return testCases; diff --git a/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts new file mode 100644 index 00000000..ec6fb492 --- /dev/null +++ b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts @@ -0,0 +1,42 @@ +import { SHARED_TEST_CASE_ID } from './readTestCases.js'; +import { EXAMPLE_TEST_CASE_ID_PATTERN } from './stdioJudgeRules.js'; + +const TEST_CASES_DIRECTORY_NAME = 'test_cases'; +const FILE_INPUT_EXTENSION = '.fin'; + +/** + * Select, from the files of a problem directory, the input files that the stdio debug preset hands + * to the debugged program: those in `test_cases/_shared.fin/` and in the `.fin/` directory of the + * first example test case that has one. Hidden test cases' files, `.in`/`.out` files, and every file + * outside `test_cases/` are never selected, so a client can ship the result (plus `problem.md`) to + * debug a problem without exposing anything the debug run would not expose anyway. + * + * @param problemFilePaths Paths relative to the problem directory, separated by `/` + * (e.g. `test_cases/_shared.fin/members.csv`). + * @returns The selected paths, in the given order. + */ +export function selectDebugInputFilePaths(problemFilePaths: readonly string[]): string[] { + const pathToTestCaseId = new Map(); + for (const filePath of problemFilePaths) { + const [directoryName, entryName, ...rest] = filePath.split('/'); + if (directoryName !== TEST_CASES_DIRECTORY_NAME || !entryName?.endsWith(FILE_INPUT_EXTENSION)) continue; + if (rest.length === 0) continue; + pathToTestCaseId.set(filePath, entryName.slice(0, -FILE_INPUT_EXTENSION.length)); + } + const selectedIds = new Set(selectDebugFileInputTestCaseIds(pathToTestCaseId.values())); + return problemFilePaths.filter((filePath) => { + const id = pathToTestCaseId.get(filePath); + return id !== undefined && selectedIds.has(id); + }); +} + +/** + * The ids of the `test_cases/.fin/` directories a debug run receives, in the order they are copied + * (later ones overwrite earlier ones), given the ids of every such directory (`_shared` included). + */ +export function selectDebugFileInputTestCaseIds(fileInputTestCaseIds: Iterable): string[] { + const ids = [...new Set(fileInputTestCaseIds)].toSorted(); + const sharedIds = ids.filter((id) => id === SHARED_TEST_CASE_ID); + const exampleId = ids.find((id) => id !== SHARED_TEST_CASE_ID && EXAMPLE_TEST_CASE_ID_PATTERN.test(id)); + return exampleId === undefined ? sharedIds : [...sharedIds, exampleId]; +} diff --git a/packages/problem-utils/src/index.ts b/packages/problem-utils/src/index.ts index 49043e48..46d96b81 100644 --- a/packages/problem-utils/src/index.ts +++ b/packages/problem-utils/src/index.ts @@ -8,6 +8,7 @@ export * from './helpers/readTestCases.js'; export * from './helpers/removeCommentsInSourceCode.js'; export * from './helpers/runCommandInTemporaryPackageManagerProject.js'; export * from './helpers/runPythonInTemporaryUvProject.js'; +export { selectDebugInputFilePaths } from './helpers/selectDebugInputFilePaths.js'; export * from './helpers/sourceCodeGrammars.js'; export * from './helpers/startHttpServer.js'; export * from './learningMaterial/validateContest.js'; diff --git a/packages/problem-utils/src/presets/stdio.ts b/packages/problem-utils/src/presets/stdio.ts index 4ca18e9c..4fc4baa0 100644 --- a/packages/problem-utils/src/presets/stdio.ts +++ b/packages/problem-utils/src/presets/stdio.ts @@ -17,9 +17,10 @@ import { judgesTestCasesWithoutExpectations, readProblemMarkdownFrontMatter, } from '../helpers/readProblemMarkdownFrontMatter.js'; -import { readTestCases } from '../helpers/readTestCases.js'; +import { readTestCases, SHARED_TEST_CASE_ID } from '../helpers/readTestCases.js'; +import { selectDebugFileInputTestCaseIds } from '../helpers/selectDebugInputFilePaths.js'; import { spawnWithTimeout } from '../helpers/spawnWithTimeout.js'; -import { EXAMPLE_TEST_CASE_ID_PATTERN, MAX_STDOUT_LENGTH } from '../helpers/stdioJudgeRules.js'; +import { MAX_STDOUT_LENGTH } from '../helpers/stdioJudgeRules.js'; import { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory, @@ -310,6 +311,8 @@ export async function stdioDebugPreset( } } +// code-gauge-allow functionCognitiveComplexity 26 -- the build, run, and verdict steps of one debug run share its limits and results +// code-gauge-allow functionNcss 56 -- the build, run, and verdict steps of one debug run share its limits and results async function debugInWorkingDirectory(problemDir: string, cwd: string, params: DebugParams): Promise { const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir); @@ -396,19 +399,7 @@ async function debugInWorkingDirectory(problemDir: string, cwd: string, params: } } - // A debug run has no test case of its own: it gets the shared input files and the `.fin/` of the - // first example case (the cases a learner may see), placed the way the judge does. Hidden cases' - // input files are never handed to learner code. - const testCases = await readTestCases(path.join(problemDir, 'test_cases')); - if (testCases.shared?.fileInputPath) await copyTestCaseFileInput(testCases.shared.fileInputPath, cwd); - const exampleFileInputPath = testCases.find( - (testCase) => EXAMPLE_TEST_CASE_ID_PATTERN.test(testCase.id) && testCase.fileInputPath - )?.fileInputPath; - if (exampleFileInputPath) { - await copyTestCaseFileInput(exampleFileInputPath, cwd); - } else if (testCases.some((testCase) => testCase.fileInputPath)) { - console.error('debug: no example test case has input files, so the program runs without them'); - } + await copyDebugFileInputs(problemDir, cwd); const timeoutSeconds = Math.max(DEBUG_DEFAULT_TIMEOUT_SECONDS, (problemMarkdownFrontMatter.timeLimitMs ?? 0) / 1000); @@ -450,3 +441,26 @@ async function debugInWorkingDirectory(problemDir: string, cwd: string, params: outputFiles: outputFiles.length > 0 ? outputFiles : undefined, }); } + +/** + * A debug run has no test case of its own: it gets the learner-visible input files, placed the way + * the judge does. Clients that ship only those files rely on `selectDebugInputFilePaths`, which + * shares this selection rule. + */ +async function copyDebugFileInputs(problemDir: string, cwd: string): Promise { + const testCases = await readTestCases(path.join(problemDir, 'test_cases')); + const idToFileInputPath = new Map( + testCases.flatMap((testCase) => (testCase.fileInputPath ? [[testCase.id, testCase.fileInputPath] as const] : [])) + ); + const hasCaseFileInput = idToFileInputPath.size > 0; + if (testCases.shared?.fileInputPath) idToFileInputPath.set(SHARED_TEST_CASE_ID, testCases.shared.fileInputPath); + + const ids = selectDebugFileInputTestCaseIds(idToFileInputPath.keys()); + for (const id of ids) { + const fileInputPath = idToFileInputPath.get(id); + if (fileInputPath) await copyTestCaseFileInput(fileInputPath, cwd); + } + if (hasCaseFileInput && ids.every((id) => id === SHARED_TEST_CASE_ID)) { + console.error('debug: no example test case has input files, so the program runs without them'); + } +} diff --git a/test/e2e/stdioDebugInputFiles.test.ts b/test/e2e/stdioDebugInputFiles.test.ts new file mode 100644 index 00000000..cae2b1b1 --- /dev/null +++ b/test/e2e/stdioDebugInputFiles.test.ts @@ -0,0 +1,75 @@ +import childProcess from 'node:child_process'; +import fs from 'node:fs/promises'; +import path from 'node:path'; + +import { expect, test } from 'vitest'; + +import { selectDebugInputFilePaths } from '../../packages/problem-utils/src/index.js'; +import { + TEST_CASE_RESULT_PREFIX, + testCaseResultSchema, +} from '../../packages/problem-utils/src/types/testCaseResult.js'; + +const problemFiles: Record = { + 'problem.md': '---\n---\n', + 'model_answers/js/main.js': 'console.log("answer");', + 'test_cases/_shared.fin/members.csv': 'name\n', + 'test_cases/_shared.fin/nested/notes.txt': 'shared\n', + 'test_cases/example_1.in': '1\n', + 'test_cases/example_1.out': '1\n', + 'test_cases/example_2.fin/visible.txt': 'example 2\n', + 'test_cases/example_3.fin/later.txt': 'example 3\n', + 'test_cases/a_hidden.fin/secret.txt': 'hidden\n', + 'test_cases/test_1.fin/secret.txt': 'hidden\n', + 'test_cases/test_1.in': '1\n', + 'test_cases/test_1.out': '1\n', +}; + +test( + 'a debug run receives exactly the input files selectDebugInputFilePaths selects', + { timeout: 30_000 }, + async () => { + const selectedPaths = selectDebugInputFilePaths(Object.keys(problemFiles)); + expect(selectedPaths).toEqual([ + 'test_cases/_shared.fin/members.csv', + 'test_cases/_shared.fin/nested/notes.txt', + 'test_cases/example_2.fin/visible.txt', + ]); + + await fs.mkdir('.tmp', { recursive: true }); + const root = await fs.mkdtemp(path.resolve('.tmp/stdio-debug-input-')); + try { + for (const [filePath, data] of Object.entries(problemFiles)) { + await fs.mkdir(path.dirname(path.join(root, filePath)), { recursive: true }); + await fs.writeFile(path.join(root, filePath), data); + } + const answer = path.join(root, 'answer'); + await fs.mkdir(answer); + await fs.writeFile( + path.join(answer, 'main.js'), + String.raw`import fs from 'node:fs'; console.log(fs.readdirSync('.', { recursive: true }).filter((p) => p !== 'main.js' && fs.statSync(p).isFile()).toSorted().join('\n'));` + ); + await fs.writeFile( + path.join(root, 'debug.ts'), + `import { stdioDebugPreset } from ${JSON.stringify(path.resolve('packages/problem-utils/src/presets/stdio.ts'))};\nawait stdioDebugPreset(import.meta.dirname);\n` + ); + const run = childProcess.spawnSync('bun', ['run', 'debug.ts', answer, JSON.stringify({ stdin: '' })], { + cwd: root, + encoding: 'utf8', + timeout: 20_000, + }); + expect(run.status, run.stderr).toBe(0); + const line = run.stdout.split('\n').find((value) => value.startsWith(TEST_CASE_RESULT_PREFIX)); + expect(line).toBeDefined(); + const result = testCaseResultSchema.parse(JSON.parse(line!.slice(TEST_CASE_RESULT_PREFIX.length))); + expect(result.stdout).toBe( + `${selectedPaths + .map((filePath) => filePath.split('/').slice(2).join('/')) + .toSorted() + .join('\n')}\n` + ); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + } +); From 662cbedf54e73338abdb5e84b7b627b05afd4cf4 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 17:00:20 +0900 Subject: [PATCH 2/6] fix: normalize paths before selecting debug input files Co-authored-by: WillBooster (Claude Code) --- .../problem-utils/src/helpers/selectDebugInputFilePaths.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts index ec6fb492..806fd9fb 100644 --- a/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts +++ b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts @@ -1,3 +1,5 @@ +import path from 'node:path'; + import { SHARED_TEST_CASE_ID } from './readTestCases.js'; import { EXAMPLE_TEST_CASE_ID_PATTERN } from './stdioJudgeRules.js'; @@ -12,13 +14,13 @@ const FILE_INPUT_EXTENSION = '.fin'; * debug a problem without exposing anything the debug run would not expose anyway. * * @param problemFilePaths Paths relative to the problem directory, separated by `/` - * (e.g. `test_cases/_shared.fin/members.csv`). + * (e.g. `test_cases/_shared.fin/members.csv` or `./test_cases/_shared.fin/members.csv`). * @returns The selected paths, in the given order. */ export function selectDebugInputFilePaths(problemFilePaths: readonly string[]): string[] { const pathToTestCaseId = new Map(); for (const filePath of problemFilePaths) { - const [directoryName, entryName, ...rest] = filePath.split('/'); + const [directoryName, entryName, ...rest] = path.posix.normalize(filePath).split('/'); if (directoryName !== TEST_CASES_DIRECTORY_NAME || !entryName?.endsWith(FILE_INPUT_EXTENSION)) continue; if (rest.length === 0) continue; pathToTestCaseId.set(filePath, entryName.slice(0, -FILE_INPUT_EXTENSION.length)); From 96ad4c82504bda4f0886271d5fc397d31d5bd491 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 17:01:20 +0900 Subject: [PATCH 3/6] test: list a later example first in the debug input files fixture Co-authored-by: WillBooster (Claude Code) --- test/e2e/stdioDebugInputFiles.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/stdioDebugInputFiles.test.ts b/test/e2e/stdioDebugInputFiles.test.ts index cae2b1b1..9b7f4420 100644 --- a/test/e2e/stdioDebugInputFiles.test.ts +++ b/test/e2e/stdioDebugInputFiles.test.ts @@ -17,8 +17,8 @@ const problemFiles: Record = { 'test_cases/_shared.fin/nested/notes.txt': 'shared\n', 'test_cases/example_1.in': '1\n', 'test_cases/example_1.out': '1\n', - 'test_cases/example_2.fin/visible.txt': 'example 2\n', 'test_cases/example_3.fin/later.txt': 'example 3\n', + 'test_cases/example_2.fin/visible.txt': 'example 2\n', 'test_cases/a_hidden.fin/secret.txt': 'hidden\n', 'test_cases/test_1.fin/secret.txt': 'hidden\n', 'test_cases/test_1.in': '1\n', From f048d5992c2ad36677e855a4dee7f9261c32fa32 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 17:12:15 +0900 Subject: [PATCH 4/6] test: cover ./-prefixed paths in debug input file selection Co-authored-by: WillBooster (Claude Code) --- test/e2e/stdioDebugInputFiles.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/e2e/stdioDebugInputFiles.test.ts b/test/e2e/stdioDebugInputFiles.test.ts index 9b7f4420..6d78749e 100644 --- a/test/e2e/stdioDebugInputFiles.test.ts +++ b/test/e2e/stdioDebugInputFiles.test.ts @@ -35,6 +35,9 @@ test( 'test_cases/_shared.fin/nested/notes.txt', 'test_cases/example_2.fin/visible.txt', ]); + expect( + selectDebugInputFilePaths(['./test_cases/example_2.fin/visible.txt', './test_cases/test_1.fin/secret.txt']) + ).toEqual(['./test_cases/example_2.fin/visible.txt']); await fs.mkdir('.tmp', { recursive: true }); const root = await fs.mkdtemp(path.resolve('.tmp/stdio-debug-input-')); From 4c89e6badd738f027b059dec28345fd7e5520ef6 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 17:12:57 +0900 Subject: [PATCH 5/6] test: check that example inputs overwrite shared ones in a debug run Co-authored-by: WillBooster (Claude Code) --- test/e2e/stdioDebugInputFiles.test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/e2e/stdioDebugInputFiles.test.ts b/test/e2e/stdioDebugInputFiles.test.ts index 6d78749e..e9441a3a 100644 --- a/test/e2e/stdioDebugInputFiles.test.ts +++ b/test/e2e/stdioDebugInputFiles.test.ts @@ -18,6 +18,7 @@ const problemFiles: Record = { 'test_cases/example_1.in': '1\n', 'test_cases/example_1.out': '1\n', 'test_cases/example_3.fin/later.txt': 'example 3\n', + 'test_cases/example_2.fin/members.csv': 'example 2 members\n', 'test_cases/example_2.fin/visible.txt': 'example 2\n', 'test_cases/a_hidden.fin/secret.txt': 'hidden\n', 'test_cases/test_1.fin/secret.txt': 'hidden\n', @@ -33,6 +34,7 @@ test( expect(selectedPaths).toEqual([ 'test_cases/_shared.fin/members.csv', 'test_cases/_shared.fin/nested/notes.txt', + 'test_cases/example_2.fin/members.csv', 'test_cases/example_2.fin/visible.txt', ]); expect( @@ -50,7 +52,7 @@ test( await fs.mkdir(answer); await fs.writeFile( path.join(answer, 'main.js'), - String.raw`import fs from 'node:fs'; console.log(fs.readdirSync('.', { recursive: true }).filter((p) => p !== 'main.js' && fs.statSync(p).isFile()).toSorted().join('\n'));` + String.raw`import fs from 'node:fs'; console.log(fs.readdirSync('.', { recursive: true }).filter((p) => p !== 'main.js' && fs.statSync(p).isFile()).toSorted().map((p) => p + '=' + fs.readFileSync(p, 'utf8').trim()).join('\n'));` ); await fs.writeFile( path.join(root, 'debug.ts'), @@ -65,12 +67,8 @@ test( const line = run.stdout.split('\n').find((value) => value.startsWith(TEST_CASE_RESULT_PREFIX)); expect(line).toBeDefined(); const result = testCaseResultSchema.parse(JSON.parse(line!.slice(TEST_CASE_RESULT_PREFIX.length))); - expect(result.stdout).toBe( - `${selectedPaths - .map((filePath) => filePath.split('/').slice(2).join('/')) - .toSorted() - .join('\n')}\n` - ); + // The example's files overwrite the shared ones at the same path, as in judging. + expect(result.stdout).toBe('members.csv=example 2 members\nnested/notes.txt=shared\nvisible.txt=example 2\n'); } finally { await fs.rm(root, { recursive: true, force: true }); } From ed0bd072b872439783749039df0da406d9c95f59 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 26 Nov 2026 17:13:18 +0900 Subject: [PATCH 6/6] docs: clarify which entries selectDebugInputFilePaths excludes Co-authored-by: WillBooster (Claude Code) --- .../src/helpers/selectDebugInputFilePaths.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts index 806fd9fb..1b9eab0d 100644 --- a/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts +++ b/packages/problem-utils/src/helpers/selectDebugInputFilePaths.ts @@ -8,10 +8,11 @@ const FILE_INPUT_EXTENSION = '.fin'; /** * Select, from the files of a problem directory, the input files that the stdio debug preset hands - * to the debugged program: those in `test_cases/_shared.fin/` and in the `.fin/` directory of the - * first example test case that has one. Hidden test cases' files, `.in`/`.out` files, and every file - * outside `test_cases/` are never selected, so a client can ship the result (plus `problem.md`) to - * debug a problem without exposing anything the debug run would not expose anyway. + * to the debugged program: every file (of any name) in `test_cases/_shared.fin/` and in the `.fin/` + * directory of the first example test case, in sorted id order, that has one. The `.in`/`.out` + * entries directly under `test_cases/`, hidden test cases' `.fin/` files, and every file outside + * `test_cases/` are never selected, so a client can ship the result (plus `problem.md`) to debug a + * problem without exposing anything the debug run would not expose anyway. * * @param problemFilePaths Paths relative to the problem directory, separated by `/` * (e.g. `test_cases/_shared.fin/members.csv` or `./test_cases/_shared.fin/members.csv`).