-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: scheme-agnostic WorldBundle + exposure metadata hook [LTV-Pn.2] #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
bf843dd
refactor: scheme-agnostic WorldBundle + exposure metadata hook [LTV-P…
shaypal5 fdf87f5
fix(scripts): migrate build_v*_snapshot to bundle.artifacts [LTV-Pn.2]
shaypal5 7216d6d
fix(exposure): clear metadata/ before rewrite; fix stale docstring [L…
shaypal5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,35 @@ | ||
| """Write hidden-truth metadata files for ``research_instructor`` mode. | ||
|
|
||
| :func:`write_metadata_dir` creates ``bundle_root/metadata/`` and populates | ||
| it with five files that expose the full hidden world: | ||
|
|
||
| - ``graph.json`` — world graph as JSON (nodes, edges, motif family) | ||
| - ``graph.graphml`` — world graph as GraphML for graph tools | ||
| - ``world_spec.json`` — generation config + narrative spec | ||
| - ``latent_registry.json`` — per-entity latent trait values | ||
| - ``mechanism_summary.json`` — mechanism assignment summary | ||
| """Scheme-agnostic hidden-truth metadata for ``research_instructor`` mode. | ||
|
|
||
| The bundle's ``metadata/`` directory mixes scheme-agnostic provenance | ||
| (``world_spec.json`` — config + narrative) with scheme-specific hidden truth | ||
| (the lead-scoring world graph, latent registry, and mechanism summary; the | ||
| lifecycle scheme will emit its own). Only the generic part lives here; | ||
| :func:`write_world_spec_json` writes it. Each scheme owns the rest via its | ||
| :meth:`~leadforge.schemes.base.GenerationScheme.write_metadata` hook, called by | ||
| :func:`leadforge.exposure.modes.apply_exposure`. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import dataclasses | ||
| import json | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from leadforge.core.models import WorldBundle | ||
|
|
||
|
|
||
| def write_metadata_dir(bundle: WorldBundle, bundle_root: Path) -> None: | ||
| """Populate ``bundle_root/metadata/`` with all hidden-truth files. | ||
| from pathlib import Path | ||
|
|
||
| Args: | ||
| bundle: Fully populated :class:`~leadforge.core.models.WorldBundle`. | ||
| bundle_root: Root directory of the written bundle. | ||
| """ | ||
| from leadforge.core.rng import RNGRoot | ||
| from leadforge.schemes.lead_scoring.mechanisms.policies import assign_mechanisms | ||
|
|
||
| # Callers must only invoke this after full bundle assembly; world_graph | ||
| # and population are guaranteed non-None at that point. | ||
| assert bundle.world_graph is not None # noqa: S101 | ||
| assert bundle.population is not None # noqa: S101 | ||
| from leadforge.core.models import WorldSpec | ||
|
|
||
| meta_dir = bundle_root / "metadata" | ||
| meta_dir.mkdir(exist_ok=True) | ||
| __all__ = ["write_world_spec_json"] | ||
|
|
||
| # graph.json + graph.graphml | ||
| (meta_dir / "graph.json").write_text(bundle.world_graph.to_json()) | ||
| (meta_dir / "graph.graphml").write_text(bundle.world_graph.to_graphml()) | ||
|
|
||
| # latent_registry.json | ||
| ls = bundle.population.latent_state | ||
| latent_registry: dict[str, object] = { | ||
| "account_latents": ls.account_latents, | ||
| "contact_latents": ls.contact_latents, | ||
| "lead_latents": ls.lead_latents, | ||
| } | ||
| (meta_dir / "latent_registry.json").write_text(json.dumps(latent_registry, indent=2)) | ||
| def write_world_spec_json(spec: WorldSpec, meta_dir: Path) -> None: | ||
| """Write ``meta_dir/world_spec.json`` — the resolved config + narrative. | ||
|
|
||
| # world_spec.json — config + narrative (if present) | ||
| config_dict = dataclasses.asdict(bundle.spec.config) | ||
| narrative_dict = ( | ||
| dataclasses.asdict(bundle.spec.narrative) if bundle.spec.narrative is not None else None | ||
| ) | ||
| Scheme-agnostic: depends only on the shared :class:`WorldSpec`, so it is | ||
| identical across generation schemes. | ||
| """ | ||
| config_dict = dataclasses.asdict(spec.config) | ||
| narrative_dict = dataclasses.asdict(spec.narrative) if spec.narrative is not None else None | ||
| world_spec_dict = {"config": config_dict, "narrative": narrative_dict} | ||
| (meta_dir / "world_spec.json").write_text(json.dumps(world_spec_dict, indent=2)) | ||
|
|
||
| # mechanism_summary.json | ||
| # Reconstruct the mechanism assignment with the same RNG substream that | ||
| # was used during simulation — produces the identical parameter values. | ||
| motif_family = bundle.world_graph.motif_family | ||
| mech_rng = RNGRoot(bundle.spec.config.seed).child("mechanisms") | ||
| assignment = assign_mechanisms(motif_family, mech_rng) | ||
| (meta_dir / "mechanism_summary.json").write_text( | ||
| json.dumps(assignment.summary().to_dict(), indent=2) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,55 @@ | ||
| """Exposure-mode dispatch for bundle publication. | ||
|
|
||
| :func:`apply_exposure` is the single entry point called by | ||
| :func:`~leadforge.api.bundle.write_bundle`. It reads the resolved | ||
| :class:`~leadforge.exposure.filters.BundleFilter` for the requested mode | ||
| and performs the corresponding writes (or skips them). | ||
| :func:`apply_exposure` is the single entry point called by each scheme's | ||
| ``write_bundle``. It reads the resolved | ||
| :class:`~leadforge.exposure.filters.BundleFilter` for the requested mode and, | ||
| when hidden truth should be published, writes the scheme-agnostic | ||
| ``world_spec.json`` and delegates the scheme-specific hidden-truth files to the | ||
| producing scheme's :meth:`~leadforge.schemes.base.GenerationScheme.write_metadata` | ||
| hook. This keeps the exposure layer free of any single scheme's types. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from leadforge.core.enums import ExposureMode | ||
| from leadforge.exposure.filters import get_filter | ||
| from leadforge.exposure.metadata import write_metadata_dir | ||
| from leadforge.exposure.metadata import write_world_spec_json | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| from leadforge.core.enums import ExposureMode | ||
| from leadforge.core.models import WorldBundle | ||
|
|
||
|
|
||
| def apply_exposure(bundle: WorldBundle, bundle_root: Path, mode: ExposureMode) -> None: | ||
| """Apply exposure filtering for *mode* to the bundle at *bundle_root*. | ||
|
|
||
| For ``research_instructor`` mode this writes the ``metadata/`` | ||
| directory with all hidden-truth files. For ``student_public`` mode any | ||
| pre-existing ``metadata/`` directory is removed so that hidden truth | ||
| is never accidentally published when reusing an output path. | ||
| For modes whose filter sets ``write_metadata`` (e.g. ``research_instructor``) | ||
| this creates ``metadata/``, writes the scheme-agnostic ``world_spec.json``, | ||
| and calls the producing scheme's ``write_metadata`` hook for its | ||
| hidden-truth files. For modes that must not publish hidden truth (e.g. | ||
| ``student_public``) any pre-existing ``metadata/`` directory is removed so | ||
| truth is never accidentally republished when reusing an output path. | ||
|
|
||
| Args: | ||
| bundle: Fully populated :class:`~leadforge.core.models.WorldBundle`. | ||
| bundle_root: Root directory of the written bundle (must already exist). | ||
| mode: Exposure mode that controls which artefacts are published. | ||
| """ | ||
| from leadforge.schemes import get_scheme | ||
|
|
||
| filt = get_filter(mode) | ||
| meta_dir = bundle_root / "metadata" | ||
| if filt.write_metadata: | ||
| write_metadata_dir(bundle, bundle_root) | ||
| elif meta_dir.exists(): | ||
| # Always start from a clean metadata/ so its contents exactly match the | ||
| # current bundle. Reusing an output path across runs — or across schemes, | ||
| # which emit different hidden-truth file sets — must not leave stale files | ||
| # behind (the non-writing branch below clears it for the same reason). | ||
| if meta_dir.exists(): | ||
| shutil.rmtree(meta_dir) | ||
| if filt.write_metadata: | ||
| meta_dir.mkdir(parents=True) | ||
| write_world_spec_json(bundle.spec, meta_dir) | ||
| get_scheme(bundle.spec.scheme).write_metadata(bundle, meta_dir) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.