NVFlare icon indicating copy to clipboard operation
NVFlare copied to clipboard

Add recipe metadata helper

Open IsaacYangSLA opened this issue 5 months ago • 1 comments

Summary

  • add set_recipe_meta(recipe, key, value) and export it from nvflare.recipe
  • require the second argument to be a JobMetaKey enum member; raw strings are rejected
  • accept number, string, dictionary, or list values, including list-backed metadata such as JobMetaKey.MANDATORY_CLIENTS
  • write helper-provided metadata through meta_props; same-key meta_props values are replaced without conflict checks
  • reject JobMetaKey.DEPLOY_MAP because it is generated dynamically from recipe deployment and must not be snapshotted into meta_props
  • document recipe metadata helper usage and update agent test-command guidance
  • add unit coverage for metadata export, deep-copying, enum/value validation, metadata replacement/override behavior, deploy-map rejection, list-valued metadata, and package export

Tests

  • source ../.venv/bin/activate && python3 -m pytest tests/unit_test/recipe/spec_test.py tests/unit_test/recipe/utils_test.py -q
  • source ../.venv/bin/activate && ./runtest.sh -s --skip-install
  • source ../.venv/bin/activate && ./runtest.sh -u --skip-install

IsaacYangSLA avatar Jun 10 '26 17:06 IsaacYangSLA

Greptile Summary

This PR adds a set_recipe_meta(recipe, key, value) helper that writes job metadata entries into meta_props using validated JobMetaKey enum keys. The function is exported from nvflare.recipe and accompanied by documentation and comprehensive unit tests.

  • Adds set_recipe_meta with enum-key enforcement, type validation (number/str/dict/list, rejecting bool), deep-copy safety, and explicit rejection of JobMetaKey.DEPLOY_MAP since deploy-map is generated dynamically at export time.
  • Lists are now accepted as values, enabling JobMetaKey.MANDATORY_CLIENTS to be set correctly; meta_props entries for the same key are overwritten on subsequent calls without conflict checks.
  • Documentation and test coverage span export behavior, deep-copy isolation, enum/value validation, deploy-map rejection, and package-level import.

Confidence Score: 5/5

Safe to merge; the helper is additive, all validation guards are in place, and the export path is exercised by tests that check the final meta.json.

The change is isolated to a new helper function and its exports. DEPLOY_MAP is correctly blocked before any write occurs, list values are accepted for MANDATORY_CLIENTS, deep-copy prevents caller mutation, and the meta_props override-wins contract at export time is explicitly verified by the test suite.

No files require special attention.

Important Files Changed

Filename Overview
nvflare/recipe/utils.py Adds set_recipe_meta and private helpers; DEPLOY_MAP is correctly blocked, list values are accepted, and deep-copy isolation is in place.
nvflare/recipe/init.py Exports set_recipe_meta from the top-level nvflare.recipe package via both the import and all list.
tests/unit_test/recipe/spec_test.py New TestRecipeMetaHelper class covers export round-trip, deep-copy, override, deploy-map rejection, and parametrized type validation; test fixtures are self-contained.
tests/unit_test/recipe/utils_test.py Adds package-import smoke test for set_recipe_meta; clean and minimal.
docs/user_guide/data_scientist_guide/job_recipe.rst Adds a Recipe Metadata section with working code examples, supported key list, and explicit call-outs for DEPLOY_MAP restriction and meta_props override semantics.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["set_recipe_meta(recipe, key, value)"] --> B["_normalize_recipe_meta_key(key)"]
    B --> C{key is JobMetaKey?}
    C -- No --> D["raise TypeError"]
    C -- Yes --> E{key in UNSUPPORTED?}
    E -- "Yes (DEPLOY_MAP)" --> F["raise ValueError"]
    E -- No --> G["return key.value as str"]
    G --> H["_validate_recipe_meta_value(key, value)"]
    H --> I{value is bool or not Number/str/dict/list?}
    I -- invalid --> J["raise TypeError"]
    I -- valid --> K["_get_recipe_job_config(recipe)"]
    K --> L{recipe.job.job is None?}
    L -- Yes --> M["raise TypeError"]
    L -- No --> N["deepcopy meta_props or empty dict"]
    N --> O["meta_props[key] = deepcopy(value)"]
    O --> P["job_config.meta_props = meta_props"]
    P --> Q["At export: FedJobConfig._generate_meta"]
    Q --> R["Build base meta_json with name, resource_spec, min_clients, deploy_map"]
    R --> S["meta_json.update(self.meta_props)"]
    S --> T["Write meta.json"]

Reviews (7): Last reviewed commit: "Add recipe metadata helper" | Re-trigger Greptile

greptile-apps avatar Jun 10 '26 17:06 greptile-apps

#4849 build on top of this PR so we can safely close this

YuanTingHsieh avatar Jul 01 '26 21:07 YuanTingHsieh