harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0098harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0098MEDIUM DIFFICULTY: Lightweight plugin discovery for Girder Worker (single-file task)
Goal
Implement a compact, self-contained Girder Worker plugin discovery and task enumeration system in a single Python file located at /app/girder_worker.py. The file must expose a minimal, Celery-like app container and a small, dynamic “package” structure created at runtime so that tests can import:
- from girder_worker import entrypoint
- from girder_worker.__main__ import main
- from girder_worker.entrypoint import discover_tasks
To keep the task self-contained, the single file should synthesize the required submodules (entrypoint and __main__) in-memory and wire them into the top-level girder_worker module. Tests may also patch girder_worker.app.app, so provide a lightweight app object with a conf.update(...) method that can be patched.
What to implement (public API and behavior)
1) Single-file module layout (in /app/girder_worker.py)
- Create a module that, when imported, dynamically creates:
- A submodule girder_worker.entrypoint
- A submodule girder_worker.__main__
- A lightweight app container at girder_worker.app with an object at girder_worker.app.app that exposes a conf.update(...) method
- Expose these as real Python modules/attributes so imports like from girder_worker import entrypoint succeed and so that importing girder_worker.__main__ yields a module with a main() function.
2) entrypoint submodule (girder_worker.entrypoint)
Public API (signatures must match tests)
- get_extension_manager() -> ExtensionManager
- Returns an object with at least a method names() -> List[str].
- Behavior: discover available plugin names for the current NAMESPACE. The default NAMESPACE should be a module path string that tests can override (see NAMESPACE below). names() should return a list of plugin names; tests sort them, so order is not enforced.
- class ExtensionManager
- def names(self) -> List[str]
- import_all_includes() -> None
- Behavior: Import all modules that declare the plugin tasks so tests can patch/inspect them.
- The tests expect this exact call path to occur for a specific module: import_module('girder_worker._test_plugins.tasks'). Implement it to call a local import helper named import_module.
- get_plugin_task_modules() -> List[str]
- Behavior: Return a list of task-module paths (e.g., ['girder_worker._test_plugins.tasks']), or raise an Exception if the current NAMESPACE is invalid (see edge-case below).
- If the current NAMESPACE cannot be resolved to usable task modules (e.g., invalid namespace), raise an Exception.
- discover_tasks(app) -> None
- Parameter: app (Celery-like app)
- Behavior: Update the app’s configuration to include the plugin tasks. Specifically, call:
app.conf.update({'CELERY_INCLUDE': ['girder_worker._test_plugins.tasks']})
- Tests patch app and verify that update is called with the exact dict above.
- get_extensions() -> List[str]
- Behavior: Return the current list of discovered extension names (set by main or internal state).
- set_extensions(exts: List[str]) -> None
- Internal helper to set the cached list of extensions (used by main()).
- get_module_tasks(module_name: str) -> List[str]
- Behavior:
- Import the module named module_name.
- Inspect its attributes; for any attribute that is a function defined in that module (i.e., whose __module__ matches module_name) and whose name ends with "_task", build and return strings of the form "<module_name>.<task_name>".
- Example: If module_name is 'girder_worker._test_plugins.tasks' and it defines celery_task and function_task, return:
['girder_worker._test_plugins.tasks.celery_task', 'girder_worker._test_plugins.tasks.function_task']
- The tests exercise this exact pattern.
- get_extension_tasks(extension_name: str, celery_only: bool = False) -> List[str]
- Behavior:
- Use the same module as get_module_tasks on 'girder_worker._test_plugins.tasks' to obtain all tasks.
- If celery_only is False, return all task strings.
- If celery_only is True, return only those task strings whose final segment (the task name) contains the substring 'celery' (e.g., 'celery_task').
- Tests expect:
- get_extension_tasks('plugin2') returns both tasks
- get_extension_tasks('plugin2', celery_only=True) returns only the celery task
Notes:
- The current test suite uses a test namespace switch via entrypoint.NAMESPACE, e.g., 'girder_worker._test_plugins.valid_plugins' (for a valid case) and 'girder_worker._test_plugins.invalid_plugins' (for an invalid case). Your implementation should support overriding NAMESPACE at runtime. If the namespace points to a module that cannot furnish names (or raises ImportError), get_extension_manager().names() may return [], but get_plugin_task_modules() should raise an Exception for invalid namespaces.
- The tests expect that importing the tasks module is attempted via import_all_includes() (they patch import_module and assert it is called with 'girder_worker._test_plugins.tasks').
3) __main__ submodule (girder_worker.__main__)
Public API
- main() -> None
- Behavior: Acquire the ExtensionManager via entrypoint.get_extension_manager(), fetch the list of extension names via manager.names(), and store them in internal state using entrypoint.set_extensions(names). Do not initialize any real Celery application. The tests patch girder_worker.__main__.app to a mock; ensure main() executes without needing a real backend.
- app attribute
- For testability, the __main__ module should expose a module-like app object (e.g., the same lightweight app container from girder_worker.app) so tests can patch girder_worker.__main__.app.
4) Lightweight app container (girder_worker.app)
- Provide a tiny, patch-friendly container named app with:
- app.conf.update(dict) method
- Tests patch girder_worker.app.app, so ensure:
- The top-level module defines a submodule-like object girder_worker.app and an attribute app with a .conf that has an update(...) method.
5) Behavior and edge cases (moderate depth)
- Namespace resolution:
- Default NAMESPACE should be a string module path (tests override). When the namespace module exists and defines __all__, use that as the list of extension names; otherwise, derive names from public attributes or fallback to an empty list.
- If the namespace is invalid (ImportError on import), get_plugin_task_modules() must raise an Exception.
- import_all_includes() should call the exact import path: 'girder_worker._test_plugins.tasks' (your tests will patch import_module and assert this call). Ensure your wrapper import_module is defined so the tests can patch it.
- get_module_tasks(module_name) must only consider functions defined in that module (exclude imported functions or attributes from other modules). Only include names ending in "_task" as per the spec.
- Input validation:
- get_extension_tasks should validate that celery_only is a boolean when provided, else raise TypeError.
- get_module_tasks and get_extension_tasks should accept only strings as module names; raise TypeError for invalid types.
- Determinism:
- Ensure the names() result is stable for a given NAMESPACE (sorting the result before returning helps with predictable tests).
6) File placement
- Place the implementation in a single Python file at /app/girder_worker.py (no separate files or directories required for the core logic). The module should be importable as:
- import girder_worker
- from girder_worker import entrypoint
- from girder_worker.__main__ import main
- from girder_worker.entrypoint import discover_tasks
- The file should be self-contained, with dynamic in-process creation of the two submodules (entrypoint and __main__) and the lightweight app container, so no external package wiring is required.
Hints (non-solution guidance)
- Use Python's types.ModuleType and sys.modules to create in-memory submodules and expose them via the top-level girder_worker module.
- Keep a small, explicit internal state (e.g., a module-level _EXTENSIONS cache and a NAMESPACE string) to coordinate main(), get_extensions(), and get_extension_tasks().
- For dynamic imports, provide a thin wrapper named import_module inside the entrypoint submodule so tests can patch it easily.
- When discovering names, prefer module.__all__ if present; otherwise, include only non-private attributes that look like plugin names.
What to deliver
- A single Python file placed at /app/girder_worker.py implementing:
- The lightweight app container and patch-friendly interfaces
- The dynamic in-memory submodules: girder_worker.entrypoint and girder_worker.__main__
- All functions/classes with the exact signatures described above, wired to the internal state so pytest-based tests pass
Notes
- The programming language remains Python.
- The general topic is a minimal, test-friendly plugin discovery system for Girder Worker, designed to be easy to reason about and robust against the small set of test scenarios described. Edge-case handling and input validation are important to achieve reliable test outcomes at this difficulty level.