-
Notifications
You must be signed in to change notification settings - Fork 46k
fix(backend): bound CountdownTimerBlock inputs and surface field-bound errors inline #13237
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
kcze
merged 7 commits into
dev
from
kpczerwinski/secrt-2319-fixblocks-cap-countdowntimerblock-duration-closes-5-ghsas
Jun 4, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34de0f9
fix(blocks): validate CountdownTimerBlock duration bounds
kcze 68ebfaa
Merge remote-tracking branch 'origin/dev' into kpczerwinski/secrt-231…
kcze 580d2d6
docs: document CountdownTimerBlock duration and repeat bounds
kcze ab357fc
fix(blocks): address CountdownTimerBlock review feedback
kcze 393efcb
fix(blocks): tighten CountdownTimerBlock repeat bounds and harden run()
kcze 8126679
fix(backend): surface block input-bound violations as inline node errors
kcze 30ae327
fix(backend): address autogpt-pr-reviewer feedback on CountdownTimerB…
kcze 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
167 changes: 167 additions & 0 deletions
167
autogpt_platform/backend/backend/blocks/time_blocks_test.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,167 @@ | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from backend.blocks.basic import StoreValueBlock | ||
| from backend.blocks.time_blocks import CountdownTimerBlock | ||
| from backend.data.graph import GraphModel, Link, Node | ||
|
|
||
|
|
||
| async def _run(block: CountdownTimerBlock, **input_kwargs): | ||
| outputs = [] | ||
| async for name, value in block.run(block.input_schema(**input_kwargs)): | ||
| outputs.append((name, value)) | ||
| return outputs | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_rejects_excessive_duration(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValueError, match="exceeds max"): | ||
| await _run(block, days=365, repeat=1) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_rejects_cumulative_duration_over_cap(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValueError, match="exceeds max"): | ||
| await _run(block, days=1, repeat=10) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_rejects_negative_duration(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValueError, match="non-negative"): | ||
| await _run(block, seconds="-1") | ||
|
|
||
|
|
||
| def test_countdown_timer_rejects_repeat_zero_at_schema(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValidationError): | ||
| block.input_schema(seconds=1, repeat=0) | ||
|
|
||
|
|
||
| def test_countdown_timer_rejects_repeat_over_max_at_schema(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValidationError): | ||
| block.input_schema(seconds=1, repeat=1001) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_run_rejects_repeat_zero_defense_in_depth(): | ||
| block = CountdownTimerBlock() | ||
| bypassed = block.input_schema.model_construct(seconds=1, repeat=0) | ||
| with pytest.raises(ValueError, match="Repeat must be between"): | ||
| async for _ in block.run(bypassed): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_run_rejects_repeat_over_max_defense_in_depth(): | ||
| block = CountdownTimerBlock() | ||
| bypassed = block.input_schema.model_construct(seconds=1, repeat=1001) | ||
| with pytest.raises(ValueError, match="Repeat must be between"): | ||
| async for _ in block.run(bypassed): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_allows_duration_at_cap(mocker): | ||
| sleep_mock = mocker.patch( | ||
| "backend.blocks.time_blocks.asyncio.sleep", new_callable=AsyncMock | ||
| ) | ||
| block = CountdownTimerBlock() | ||
| outputs = await _run(block, days=7, repeat=1) | ||
| assert outputs == [("output_message", "timer finished")] | ||
| sleep_mock.assert_awaited_once_with(7 * 86400) | ||
|
|
||
|
|
||
| def test_countdown_timer_execution_timeout_covers_max_duration(): | ||
| block = CountdownTimerBlock() | ||
| assert block.execution_timeout_seconds is not None | ||
| assert block.execution_timeout_seconds >= block.MAX_TOTAL_SECONDS | ||
|
|
||
|
|
||
| def test_countdown_timer_get_field_errors_reports_per_field_bound_violations(): | ||
| block = CountdownTimerBlock() | ||
| errors = block.input_schema.get_field_errors({"repeat": 1200}) | ||
| assert "repeat" in errors | ||
| assert "1200" in errors["repeat"] | ||
| assert "1000" in errors["repeat"] | ||
|
|
||
|
|
||
| def test_countdown_timer_get_field_errors_clean_when_within_bounds(): | ||
| block = CountdownTimerBlock() | ||
| assert block.input_schema.get_field_errors({"repeat": 5}) == {} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_rejects_non_numeric_string_duration(): | ||
| block = CountdownTimerBlock() | ||
| with pytest.raises(ValueError, match="seconds must be a valid integer"): | ||
| await _run(block, seconds="abc") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_countdown_timer_emits_one_message_per_repeat(mocker): | ||
| sleep_mock = mocker.patch( | ||
| "backend.blocks.time_blocks.asyncio.sleep", new_callable=AsyncMock | ||
| ) | ||
| block = CountdownTimerBlock() | ||
| outputs = await _run(block, seconds=1, repeat=3) | ||
| assert outputs == [("output_message", "timer finished")] * 3 | ||
| assert sleep_mock.await_count == 3 | ||
|
|
||
|
|
||
| def _countdown_node(node_id: str, **input_default) -> Node: | ||
| return Node( | ||
| id=node_id, | ||
| block_id=CountdownTimerBlock().id, | ||
| input_default=input_default, | ||
| ) | ||
|
|
||
|
|
||
| def _graph(nodes: list[Node], links: list[Link] | None = None) -> GraphModel: | ||
| # Bypass GraphModel field validators by constructing the structural fields | ||
| # directly — these tests only exercise the per-field jsonschema bound check | ||
| # in ``_validate_graph_get_errors`` and don't need DB-side metadata. | ||
| return GraphModel.model_construct( | ||
| id="g", | ||
| version=1, | ||
| name="t", | ||
| description="t", | ||
| nodes=nodes, | ||
| links=links or [], | ||
| sub_graphs=[], | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_graph_surfaces_bound_violation_inline_on_field(): | ||
| node = _countdown_node("n1", repeat=1200) | ||
| graph = _graph([node]) | ||
|
|
||
| node_errors = graph.validate_graph_get_errors(for_run=True) | ||
|
|
||
| assert "n1" in node_errors | ||
| assert "repeat" in node_errors["n1"] | ||
| assert "1000" in node_errors["n1"]["repeat"] | ||
|
|
||
|
|
||
| def test_validate_graph_skips_bound_check_when_field_is_linked(): | ||
| # ``repeat`` is linked from an upstream block — the runtime value isn't | ||
| # known at validation time, so the saved ``input_default`` placeholder | ||
| # (here, an out-of-range value) should NOT raise a spurious field error. | ||
| source = Node(id="src", block_id=StoreValueBlock().id, input_default={"input": 5}) | ||
| countdown = _countdown_node("n1", repeat=1200) | ||
| link = Link( | ||
| source_id="src", | ||
| sink_id="n1", | ||
| source_name="output", | ||
| sink_name="repeat", | ||
| ) | ||
| graph = _graph([source, countdown], [link]) | ||
|
|
||
| node_errors = graph.validate_graph_get_errors(for_run=True) | ||
|
|
||
| assert "repeat" not in node_errors.get("n1", {}) |
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.