Add recipe metadata helper
Summary
- add
set_recipe_meta(recipe, key, value)and export it fromnvflare.recipe - require the second argument to be a
JobMetaKeyenum 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-keymeta_propsvalues are replaced without conflict checks - reject
JobMetaKey.DEPLOY_MAPbecause it is generated dynamically from recipe deployment and must not be snapshotted intometa_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 -qsource ../.venv/bin/activate && ./runtest.sh -s --skip-installsource ../.venv/bin/activate && ./runtest.sh -u --skip-install
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_metawith enum-key enforcement, type validation (number/str/dict/list, rejecting bool), deep-copy safety, and explicit rejection ofJobMetaKey.DEPLOY_MAPsince deploy-map is generated dynamically at export time. - Lists are now accepted as values, enabling
JobMetaKey.MANDATORY_CLIENTSto be set correctly;meta_propsentries 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
#4849 build on top of this PR so we can safely close this