harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0319harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0319MEDIUM DIFFICULTY TASK
Overview
You will implement a small, self-contained serial-port discovery module. The tests import a function all_ports from a submodule and verify that the returned port names match platform-specific patterns. Do not add external dependencies; rely only on the Python standard library.
Goal
Create a Python package under /app with a subpackage com_server that exposes a function all_ports via com_server.tools. The tests will import com_server.tools and call all_ports(). The function should return a list of 3-tuples: (port, description, hardware_id).
What to implement
- File layout (place all in /app/):
- /app/com_server/__init__.py
- /app/com_server/tools.py
- Function signature and behavior
- In com_server/tools.py, implement:
def all_ports() -> list[tuple[str, str, str]]:
- Returns a list of 3-tuples: (port, description, hardware_id)
- port: string with the serial port name (e.g., "/dev/ttyUSB0", "/dev/ttyACM0", "COM3")
- description: short human-friendly description (can be an empty string if unknown)
- hardware_id: hardware identifier string (can be an empty string if unknown)
- If no ports are found, return an empty list (do not raise).
- Port enumeration logic (no external dependencies)
- OS detection via sys.platform
- Linux (sys.platform starts with "linux"):
- Look for /dev/ttyUSB[0-9]* and /dev/ttyACM[0-9]*
- macOS (sys.platform starts with "darwin"):
- Look for /dev/cu.usbserial* and /dev/cu.usbmodem*
- Windows (sys.platform starts with "win"):
- Consider ports COM1 through COM256
- Other platforms:
- Return an empty list
- Only include ports that exist on the filesystem (note: for Windows you can still generate the candidate list; tests will filter with a regex).
- Return ports in a stable, deterministic order (e.g., sorted ascending by port name).
- Compatibility with tests
- The tests iterate: for a, _, _ in all_ports() and compare a against a platform-specific regex.
- Therefore:
- a must be a string containing the port name (e.g., "/dev/ttyUSB0" or "COM3")
- The second and third elements (description, hardware_id) can be empty strings or simple placeholders; tests ignore them.
- Implementation notes
- Use only standard library modules (os, glob, sys, re if needed).
- Include a brief module docstring and a function docstring explaining behavior.
- The code should be importable as com_server.tools (i.e., the package under /app/ is discoverable by the tests).
Edge cases and moderately challenging aspects
- Edge case 1: Linux/macOS paths exist but are not actually serial devices (e.g., stale symlinks). At minimum, check existence; adding a secondary check (such as file type) is optional but allowed to improve robustness.
- Edge case 2: On Windows, you may generate COM1..COM256 even if some are already in use. Tests will filter by regex; ensure the function does not crash in this scenario.
- Edge case 3: Duplicate port names from multiple patterns; ensure the final list contains unique port names (deduplicate while preserving deterministic order).
- Edge case 4: Very large results on Windows; keep the upper bound to 256.
Input validation and robustness
- Validate that the returned port strings are non-empty.
- If an unexpected platform is detected, return an empty list rather than raising.
- Ensure the function does not crash on missing directories or files.
Hints to approach (moderate guidance)
- For Linux/macOS, use glob.glob with patterns for the platform, then filter by os.path.exists and sort the results.
- For Windows, generate the candidate list programmatically (1..256) and format as "COM{n}".
- Keep the implementation small, readable, and well-documented.
Deliverables
- A Python package /app/com_server with:
- __init__.py (can be minimal)
- tools.py implementing all_ports() as described
- The function should be fully documented and ready to be imported by the provided pytest tests.
Notes
- Language: Python (standard library only)
- Focus on clarity, determinism, and robust handling of platforms with no ports (return empty list)
- The tests will import com_server.tools and call all_ports(), then inspect the port string returned as the first element of each tuple.