-
Notifications
You must be signed in to change notification settings - Fork 649
MAINT: Registry Metadata Refactor #1323
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
rlundeen2
merged 7 commits into
Azure:main
from
rlundeen2:users/rlundeen/2026_01_23_identifier
Jan 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80469d5
refactoring register metadata
rlundeen2 7b9118e
pre-commit
rlundeen2 650da3e
pr feedback
rlundeen2 85b0c97
pr feedback
rlundeen2 34f69e7
Merge branch 'main' into users/rlundeen/2026_01_23_identifier
rlundeen2 c512b64
pre-commit
rlundeen2 e951459
Merge branch 'users/rlundeen/2026_01_23_identifier' of https://github…
rlundeen2 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,13 +1,89 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import hashlib | ||
| import json | ||
| from abc import abstractmethod | ||
| from dataclasses import asdict, dataclass, field, fields, is_dataclass | ||
| from typing import Any, Literal | ||
|
|
||
| IdentifierType = Literal["class", "instance"] | ||
|
|
||
|
|
||
| class Identifiable: | ||
| """ | ||
| Abstract base class for objects that can provide an identifier dictionary. | ||
|
|
||
| This is a legacy interface that will eventually be replaced by Identifier dataclass. | ||
| Classes implementing this interface should return a dict describing their identity. | ||
| """ | ||
|
|
||
| class Identifier: | ||
| @abstractmethod | ||
| def get_identifier(self) -> dict[str, str]: | ||
| pass | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"{self.get_identifier}" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Identifier: | ||
| """ | ||
| Base dataclass for identifying PyRIT components. | ||
|
|
||
| This frozen dataclass provides a stable identifier for registry items, | ||
| targets, scorers, attacks, converters, and other components. The hash is computed at creation | ||
| time from the core fields and remains constant. | ||
|
|
||
| This class serves as: | ||
| 1. Base for registry metadata (replacing RegistryItemMetadata) | ||
| 2. Future replacement for get_identifier() dict patterns | ||
|
|
||
| All component-specific identifier types should extend this with additional fields. | ||
| """ | ||
|
|
||
| name: str # The snake_case identifier name (e.g., "self_ask_refusal") | ||
| class_name: str # The actual class name, equivalent to __type__ (e.g., "SelfAskRefusalScorer") | ||
| class_module: str # The module path, equivalent to __module__ (e.g., "pyrit.score.self_ask_refusal_scorer") | ||
|
|
||
| class_description: str = field(metadata={"exclude_from_storage": True}) | ||
|
|
||
| # Whether this identifies a "class" or "instance" | ||
| identifier_type: IdentifierType = field(metadata={"exclude_from_storage": True}) | ||
| hash: str = field(init=False, compare=False) | ||
|
|
||
| def __post_init__(self) -> None: | ||
| """Compute the identifier hash from core fields.""" | ||
| # Use object.__setattr__ since this is a frozen dataclass | ||
| object.__setattr__(self, "hash", self._compute_hash()) | ||
|
|
||
| def _compute_hash(self) -> str: | ||
| """ | ||
| Compute a stable SHA256 hash from storable identifier fields. | ||
|
|
||
| Fields marked with metadata={"exclude_from_storage": True} and 'hash' itself | ||
| are excluded from the hash computation. | ||
|
|
||
| Returns: | ||
| A hex string of the SHA256 hash. | ||
| """ | ||
| hashable_dict: dict[str, Any] = { | ||
| f.name: getattr(self, f.name) | ||
| for f in fields(self) | ||
| if f.name != "hash" and not f.metadata.get("exclude_from_storage", False) | ||
| } | ||
| config_json = json.dumps(hashable_dict, sort_keys=True, separators=(",", ":"), default=_dataclass_encoder) | ||
| return hashlib.sha256(config_json.encode("utf-8")).hexdigest() | ||
|
|
||
| def to_storage_dict(self) -> dict[str, Any]: | ||
| """Return only fields suitable for DB storage.""" | ||
| return { | ||
| f.name: getattr(self, f.name) for f in fields(self) if not f.metadata.get("exclude_from_storage", False) | ||
| } | ||
|
|
||
|
|
||
| def _dataclass_encoder(obj: Any) -> Any: | ||
| """JSON encoder that handles dataclasses by converting them to dicts.""" | ||
| if is_dataclass(obj) and not isinstance(obj, type): | ||
| return asdict(obj) | ||
| raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable") |
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
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
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.