harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0336harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0336Harder task: Hardened minimal sinfactory package with thread-safety, performance guarantees, and robustness
Overview
You must implement a minimal sinfactory package under /app/sinfactory that provides a PFactoryGrid and a Line type. The tests will still import via:
- from sinfactory.pfactorygrid import PFactoryGrid
To increase difficulty, the implementation must be robust under concurrency, handle edge cases, and adhere to simple performance constraints while remaining a tiny, self-contained solution with no external dependencies.
What to implement
- Location and package structure
- Create a Python package at /app/sinfactory (i.e., a directory named sinfactory with an __init__.py) and add pfactorygrid.py inside it.
- Ensure the package is importable as sinfactory.pfactorygrid.
- Public API to implement (exact signatures and behaviors)
1) Class PFactoryGrid
- __init__(self, project_name: str) -> None
- Initialize the object with the given project_name.
- Create an attribute lines: dict[str, Line] that will hold line definitions.
- Pre-populate lines with at least a line named "Line12" having f_bus and t_bus values as required by tests.
- Maintain an internal thread-safe state (see concurrency requirements below).
- run_load_flow(self) -> None
- Simulate the action of running a load-flow calculation. It should be a no-op with respect to data values, but it must be thread-safe.
- The method should set an internal flag to indicate the load flow has “happened” (idempotent) without mutating Line data in a way that would break tests.
- Attribute
- lines: dict[str, Line]
- A dictionary mapping line names (e.g., "Line12") to Line objects.
- After initialization, lines must contain "Line12" with f_bus = "bus1" and t_bus = "bus2".
- Thread-safety and performance requirements
- All reads and writes to internal state (including lines and the load-flow state) must be safe for concurrent access from multiple threads.
- run_load_flow must be non-blocking beyond a minimal lock-protected section, and must complete quickly (no sleeps, no heavy computations).
- Repeated or concurrent calls to run_load_flow should not raise exceptions and should leave internal state consistent.
2) Class Line
- __init__(self, name: str, f_bus: str, t_bus: str) -> None
- Initialize a line with its name and its from-bus and to-bus identifiers.
- Attributes
- name: str
- f_bus: str
- t_bus: str
- The test will access test_line.f_bus and test_line.t_bus and expect "bus1" and "bus2" respectively.
Notes on edge cases and design decisions
- Unicode and unusual project names: __init__ must handle project_name containing non-ASCII characters and still initialize cleanly.
- Empty or malformed inputs: __init__ should not crash if project_name is an empty string; no assumptions beyond type hints.
- Concurrency: Even if run_load_flow is invoked from multiple threads simultaneously, no data races should occur. Use a lightweight synchronization primitive (e.g., threading.RLock) to guard internal state changes.
- Immutability and external access: Do not require external callers to mutate internal Line objects; tests only read f_bus and t_bus. You may allow mutation if carefully guarded, but ensure tests remain deterministic.
- No external dependencies: Only the standard library.
Implementation details and hints
- Package structure:
- /app/
- sinfactory/
- __init__.py
- pfactorygrid.py
- pfactorygrid.py should define PFactoryGrid and Line with the signatures specified above.
- Optional but recommended:
- Use a small, explicit docstring for each class and method describing intent and thread-safety considerations.
- Use typing hints for all public methods and attributes.
- Keep the implementation minimal and self-contained.
Deliverables
- A self-contained Python package located at /app/ containing:
- __init__.py (can be minimal)
- pfactorygrid.py implementing PFactoryGrid and Line as described
- The package should satisfy tests that perform:
- test_system = PFactoryGrid(project_name="test_grid_sinfactory")
- test_system.run_load_flow()
- test_line = test_system.lines["Line12"]
- Assert test_line.f_bus == "bus1"
- Assert test_line.t_bus == "bus2"
Notes
- The primary objective remains satisfying the tests with a clean, minimal, and well-documented implementation, but with the added hardness described above.