harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0497harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0497Harder task: scalable, external-sort, concurrent list_files in /app/
Overview
Rewrite the small CLI utility with a focus on scalability, robustness, and performance. The program remains a Python 3 script named list_files.py located under /app/, and it must be invoked with exactly one argument: the path to the output file. It should still enumerate non-hidden regular files in the current working directory and write their names to the output file, one per line, in lexicographic order. However, the solution must be capable of handling very large directories and unusual edge cases efficiently, using concurrency and an external-sort approach to keep memory usage bounded.
What to build (harder version)
- A single executable script placed at /app/list_files.py (with an appropriate shebang and executable bit).
- Behavior
- Accept exactly one argument: the path to the output file.
- Enumerate entries in the current working directory and select only non-hidden regular files, where:
- A path is considered a regular file if it is a regular file or a symlink to a regular file (i.e., follow symlinks when determining file type).
- Ignore directories, hidden entries whose names start with '.', and any path that does not resolve to a regular file.
- If a symlink is broken or points to a non-regular target, treat it as non-regular.
- Output the names of the selected entries to the specified output file, one per line, in lexicographic (sorted) order.
- Create or overwrite the output file. If there are no matching files, the output file should be created and left empty.
- Exit with status 0 on success; exit non-zero on error (e.g., wrong number of arguments, I/O error while reading the directory or writing the output).
- Correctly handle file names containing spaces or unusual characters, preserving their exact byte sequence as seen by the filesystem where possible. Use UTF-8 for output, but avoid crashing on non-UTF-8 names by employing surrogate handling.
- Performance and resource constraints
- Time and space complexity
- Must scale to directories containing potentially millions of entries without exhausting memory (soft limit: total resident memory under ~64 MB during normal operation).
- Implement an external sort: no assumption that all names fit in memory simultaneously.
- Memory management
- Do not keep all names in memory at once. Use chunked processing, writing sorted chunks to temporary files on disk.
- Temporary data must be bounded by a configurable limit (default safe value, e.g., ≤ 256 MB total for all temporary data).
- Concurrency
- Use a small thread pool (e.g., up to 8 worker threads) to perform I/O-bound stat checks in parallel during enumeration, while preserving correctness.
- Ensure deterministic final output order regardless of the concurrency level.
- Sorting
- Use a robust, in-process external-sort approach:
- Phase 1: Enumerate entries in chunks, validate them, and produce sorted chunk files on disk.
- Phase 2: Merge all sorted chunk files using a k-way merge (priority queue) and stream the merged output to the final output file.
- Sorting must be lexicographic with Unicode-aware ordering. Preserve the original display name for each entry in the output, without normalization changing the ordering unless the platform requires it for lexicographic comparisons.
- I/O guarantees
- Output file must be overwritten if it already exists.
- If an unexpected I/O error occurs, the program must exit with a non-zero status and provide minimal error context to stderr.
- Inputs and outputs
- Input: A single command-line argument: the path to the output file (e.g., /tmp/output.txt).
- Output: A text file at the given path containing, line by line, the names of all non-hidden regular files in the current directory, sorted lexicographically. If no such files exist, the file is empty.
- File locations and naming
- Solution file must be placed in /app/.
- Recommended naming: list_files.py (Python 3), with a proper shebang (#!/usr/bin/env python3) and executable permissions.
- Example usage:
- python3 /app/list_files.py /path/to/output.txt
- Cross-platform considerations
- The solution should work on common POSIX and Windows environments where Python 3 is available.
- Scandir and filesystem semantics should be used to maximize performance and correctness across platforms.
- Design and architecture (required)
- Implement a clean, modular architecture that demonstrates the following patterns:
- Strategy pattern for sorting, with a pluggable LexSortStrategy as the default.
- A simple Producer-Consumer model for enumeration (using a thread pool) and a separate external sort/merger component.
- A small Context or Orchestrator that coordinates enumeration, chunking, sorting, merging, and writing.
- The code must be written with clear separation of concerns, testable components, and robust error handling.
- Edge cases and correctness requirements
- Empty directories, directories with only hidden files, and directories with large numbers of non-regular entries should yield an empty output file or a correctly populated sorted list as appropriate.
- File names containing spaces, newlines, or unusual characters must be preserved as they appear (subject to filesystem encoding), and must be correctly written to the output file with one per line.
- Broken symlinks or links that point to non-regular targets should be ignored gracefully (not treated as files, and should not crash).
- Inputs for validation (non-test-revealing)
- Do not reveal internal test logic. The implementation should adhere to the behavior described above so that the produced output file matches the verifier’s expectations for the current directory contents.