harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0199harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0199Harder Task: Performance-aware, Unicode-capable Click CLI with optional streaming and concurrency
Objective
Enhance the minimal Click-based CLI in /app/fip.py to be robust under large inputs and edge cases while preserving the original no-output default behavior. The module must export a single Click Command object named cli, created via a decorator, and remain importable as from fip import cli. The command must still be effectively a no-op when invoked with no arguments, satisfying the test expectations for default behavior and help output. Build in streaming-friendly, concurrency-safe data processing that scales in a controlled way and demonstrates performance-conscious design.
What to implement
- File: /app/fip.py
- Exposed symbol: cli (a Click Command object)
- Implementation pattern: Use the decorator form to create a Click command, e.g.,
- from click import command, option
- @command()
def cli(...): ...
- The command function by default should be a no-op (no output) when invoked with no arguments.
Enhanced behavior and constraints (harder requirements)
- Large-input-friendly processing:
- Introduce options to simulate processing of large data without building a single giant string in memory.
- Use a streaming approach (generator-based) to process data in chunks when requested, to keep memory usage bounded (O(chunk_size)).
- When streaming is disabled, implement a non-streaming path that may accumulate data in memory (to illustrate the contrast) but still adheres to a reasonable upper bound for test environments.
- Options (all optional, default values chosen to preserve default no-output behavior):
- --size INTEGER (default 0): total number of “units” to process. If 0, nothing is processed.
- --chunk-size INTEGER (default 1024): size of each streaming chunk.
- --streaming / --no-streaming (default True): enable/disable streaming path. Streaming must be memory-efficient; non-streaming path is allowed to be less memory-efficient to illustrate the difference.
- --unicode / --no-unicode (default True): when generating chunks, optionally include a small amount of non-ASCII characters (e.g., U+2603 snowman) to exercise Unicode handling.
- --concurrency INTEGER (default 1): number of worker threads used to “process” chunks. Must be >= 1.
- --validate / --no-validate (default False): if set, perform a validation pass that checks:
- The total number of processed units equals the requested --size.
- The sum of chunk lengths equals --size.
- The streaming/non-streaming paths behave consistently.
- Processing semantics (no output by default):
- The function should simulate work by counting or transforming chunks, but must not print or return output by default.
- If --validate is used, print a short, deterministic summary line (e.g., "validated 12345 units") to stdout. By default, no output should be produced.
- Robustness and edge cases:
- Accept empty input gracefully (size = 0).
- Handle very large sizes without causing unbounded memory growth when streaming is enabled.
- Handle Unicode data safely and without raising encoding errors.
- Ensure thread-safe behavior when using concurrency (no shared mutable state without synchronization).
- Documentation and style:
- Provide a concise module-level docstring describing purpose and behavior.
- Provide a docstring for cli explaining its options and behavior.
- Use standard library only (no external dependencies beyond Click).
- Tests compatibility hints (for maintainers):
- The module remains importable as from fip import cli.
- The tests will call runner.invoke(cli.main) with no args to verify exit code 0 and empty output, and runner.invoke(cli.main, ['--help']) to verify the help text includes the line:
- "-h, --help Show this message and exit.\n"
- The presence of the options above should not break these tests; ensure default behavior remains a no-op with no output.
Notes
- Keep the implementation self-contained in /app/fip.py.
- The tests rely on cli being a Click Command object and exposing cli.main for programmatic invocation, as provided by Click’s Command class.
- The approach should demonstrate a clear design pattern: a single, importable CLI object that can operate in both streaming and non-streaming modes, with optional Unicode data handling and a concurrency-controlled processing path.