-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Python: [BREAKING] bound PowerFx state construction #8511
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
Open
Jose Alvarez (jpalvarezl)
wants to merge
5
commits into
main
Choose a base branch
from
jpalvarezl-publish-state-construction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f641bd2
Python: bound PowerFx state construction
jpalvarezl 3182665
Python: bound PowerFx state construction
jpalvarezl 4deda7f
Merge published state construction history
jpalvarezl 0975d46
Python: keep PowerFx limit documentation near implementation
jpalvarezl 7768d9f
Merge upstream main into PowerFx state bounds
jpalvarezl 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
76 changes: 76 additions & 0 deletions
76
python/packages/declarative/agent_framework_declarative/_workflows/_powerfx_limits.py
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,76 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Structural budgets for state copying and PowerFx symbol conversion. | ||
|
|
||
| Each traversal has its own budget and rejects cycles or excess with ValueError, | ||
| rather than truncating values. These limits do not bound expression execution | ||
| or application-defined Python copy/conversion hooks. | ||
| """ | ||
|
|
||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import Any, cast | ||
|
|
||
| _MAX_POWERFX_STATE_DEPTH = 64 | ||
| _MAX_POWERFX_STATE_NODES = 10_000 | ||
| _MAX_POWERFX_STATE_TEXT_SIZE = 1_048_576 | ||
|
|
||
|
|
||
| class _PowerFxStateLimitError(ValueError): | ||
| """State cannot be copied or marshalled within the PowerFx budget.""" | ||
|
|
||
|
|
||
| @dataclass | ||
| class _PowerFxStateBudget: | ||
| """Count values, containers, and mapping keys, including repeated aliases. | ||
|
|
||
| Depth starts at zero. Text size counts string characters and binary bytes, | ||
| not encoded size or total memory. | ||
| """ | ||
|
|
||
| nodes: int = 0 | ||
| text_size: int = 0 | ||
|
|
||
| def consume(self, item: Any, depth: int) -> None: | ||
| self.nodes += 1 | ||
| if self.nodes > _MAX_POWERFX_STATE_NODES: | ||
| raise _PowerFxStateLimitError("PowerFx state exceeds the node budget") | ||
| if depth > _MAX_POWERFX_STATE_DEPTH: | ||
| raise _PowerFxStateLimitError("PowerFx state exceeds the depth budget") | ||
| if isinstance(item, (str, bytes, bytearray)): | ||
| self.text_size += len(item) | ||
| if self.text_size > _MAX_POWERFX_STATE_TEXT_SIZE: | ||
| raise _PowerFxStateLimitError("PowerFx state exceeds the text size budget") | ||
|
|
||
|
|
||
| def _validate_powerfx_state(value: Any) -> None: # pyright: ignore[reportUnusedFunction] | ||
| """Reject cyclic or over-budget data before copying or conversion.""" | ||
| budget = _PowerFxStateBudget() | ||
| active: set[int] = set() | ||
|
|
||
| def visit(item: Any, depth: int) -> None: | ||
| budget.consume(item, depth) | ||
| if item is None or isinstance(item, (str, bytes, bytearray, bool, int, float)): | ||
| return | ||
|
|
||
| identity = id(item) | ||
| if identity in active: | ||
| raise _PowerFxStateLimitError("PowerFx state contains a cycle") | ||
| active.add(identity) | ||
| try: | ||
| if isinstance(item, Enum): | ||
| visit(item.value, depth + 1) | ||
| elif isinstance(item, Mapping): | ||
| for key, member in cast(Mapping[Any, Any], item).items(): | ||
| visit(key, depth + 1) | ||
| visit(member, depth + 1) | ||
| elif isinstance(item, (list, tuple, set, frozenset)): | ||
| for member in cast(list[Any] | tuple[Any, ...] | set[Any] | frozenset[Any], item): | ||
| visit(member, depth + 1) | ||
| elif hasattr(item, "__dict__"): | ||
| visit(vars(item), depth + 1) | ||
| finally: | ||
| active.remove(identity) | ||
|
|
||
| visit(value, 0) |
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.