-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Milestone 9 — exposure filtering layer (student_public / research_instructor) #14
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Per-mode bundle filter rules. | ||
|
|
||
| :data:`FILTERS` maps every :class:`~leadforge.core.enums.ExposureMode` to a | ||
| :class:`BundleFilter` that governs which artefacts are written when | ||
| :func:`~leadforge.api.bundle.write_bundle` produces an output bundle. | ||
|
|
||
| Adding a new mode: define its ``BundleFilter`` entry in ``FILTERS``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from leadforge.core.enums import ExposureMode | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class BundleFilter: | ||
| """Rules that govern bundle publication for one :class:`ExposureMode`. | ||
|
|
||
| Attributes: | ||
| write_metadata: Whether to create ``metadata/`` with hidden-truth | ||
| files (``graph.json``, ``graph.graphml``, ``world_spec.json``, | ||
| ``latent_registry.json``, ``mechanism_summary.json``). | ||
| """ | ||
|
|
||
| write_metadata: bool | ||
|
|
||
|
|
||
| #: Canonical filter rules for every supported exposure mode. | ||
| FILTERS: dict[ExposureMode, BundleFilter] = { | ||
| ExposureMode.student_public: BundleFilter(write_metadata=False), | ||
| ExposureMode.research_instructor: BundleFilter(write_metadata=True), | ||
| } | ||
|
|
||
|
|
||
| def get_filter(mode: ExposureMode) -> BundleFilter: | ||
| """Return the :class:`BundleFilter` for *mode*. | ||
|
|
||
| Raises: | ||
| KeyError: if *mode* has no registered filter (should never happen | ||
| with well-typed callers, but guards against future enum additions | ||
| that forget to update ``FILTERS``). | ||
| """ | ||
| return FILTERS[mode] | ||
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """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). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| 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.redaction import write_metadata_dir | ||
|
|
||
| if TYPE_CHECKING: | ||
| 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 the | ||
| directory is not created and no hidden truth is published. | ||
|
|
||
| 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. | ||
| """ | ||
| filt = get_filter(mode) | ||
| if filt.write_metadata: | ||
| write_metadata_dir(bundle, bundle_root) | ||
|
shaypal5 marked this conversation as resolved.
|
||
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """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 | ||
| """ | ||
|
|
||
| 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: | ||
|
shaypal5 marked this conversation as resolved.
|
||
| """Populate ``bundle_root/metadata/`` with all hidden-truth files. | ||
|
|
||
| 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.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 | ||
|
|
||
| meta_dir = bundle_root / "metadata" | ||
| meta_dir.mkdir(exist_ok=True) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # 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)) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # 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 | ||
| ) | ||
| 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) | ||
| ) | ||
Empty file.
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.