-
Notifications
You must be signed in to change notification settings - Fork 0
feat(browser): fail closed on oversized Agent Task observation #128
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
Draft
seonghobae
wants to merge
13
commits into
test/agent-task-structured-value-evidence
Choose a base branch
from
test/agent-task-observation-bound
base: test/agent-task-structured-value-evidence
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.
+178
−12
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
71e7878
test(browser): require bounded semantic observation evidence
seonghobae a18730b
fix(browser): bound Agent Task semantic observation evidence
seonghobae 58917b0
test(browser): bound semantic locator text
seonghobae 24446c9
test(browser): restore bounded observation contract
seonghobae 5791b65
test(browser): require fixed semantic observation schema
seonghobae 46efc3d
test(browser): bind controlled observation to reviewed schema
seonghobae d199a0d
docs: record controlled observation schema contract
seonghobae 2d79ef2
Merge current structured evidence into observation bound
seonghobae 52d59d0
Merge current observation bound into schema contract
seonghobae f60771f
docs: record bounded Agent Task observation evidence
seonghobae 01eab20
docs: inherit bounded Agent Task observation release note
seonghobae 97b17e7
merge: align bounded semantic observation prerequisite
seonghobae 06b8505
Merge pull request #129 from ContextualWisdomLab/test/agent-task-sema…
seonghobae 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,60 @@ | ||
| """Contract for bounded semantic-observation evidence in the controlled Agent Task.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pathlib | ||
| import runpy | ||
| import unittest | ||
|
|
||
| ROOT = pathlib.Path(__file__).resolve().parents[1] | ||
| RUNNER = ROOT / "scripts" / "ci" / "run_mv3_compatibility.py" | ||
|
|
||
|
|
||
| class AgentTaskObservationBoundContractTests(unittest.TestCase): | ||
| """Require the pinned-browser Agent Task to fail closed on oversized observations.""" | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls) -> None: | ||
| cls.namespace = runpy.run_path( | ||
| str(RUNNER), run_name="agent_task_observation_bound_contract" | ||
| ) | ||
|
|
||
| def test_semantic_observation_has_an_explicit_byte_limit(self) -> None: | ||
| """The runner must expose one finite semantic-observation byte ceiling.""" | ||
|
|
||
| self.assertIn("MAX_AGENT_TASK_SEMANTIC_OBSERVATION_BYTES", self.namespace) | ||
| maximum = self.namespace["MAX_AGENT_TASK_SEMANTIC_OBSERVATION_BYTES"] | ||
| self.assertIsInstance(maximum, int) | ||
| self.assertGreater(maximum, 0) | ||
| self.assertLessEqual(maximum, 64 * 1024) | ||
|
|
||
| def test_observation_measurement_accepts_exact_limit_and_rejects_overflow(self) -> None: | ||
| """Canonical UTF-8 evidence at the ceiling is valid; one byte over fails closed.""" | ||
|
|
||
| self.assertIn("_measure_agent_task_semantic_observation_bytes", self.namespace) | ||
| helper = self.namespace["_measure_agent_task_semantic_observation_bytes"] | ||
| maximum = self.namespace["MAX_AGENT_TASK_SEMANTIC_OBSERVATION_BYTES"] | ||
|
|
||
| # Canonical compact JSON for {"x":"..."} uses exactly eight structural bytes. | ||
| exact = {"x": "a" * (maximum - 8)} | ||
| oversized = {"x": "a" * (maximum - 7)} | ||
| self.assertEqual(helper(exact), maximum) | ||
| with self.assertRaises(ValueError): | ||
| helper(oversized) | ||
| with self.assertRaises(ValueError): | ||
| helper({}) | ||
| with self.assertRaises(TypeError): | ||
| helper("not-an-observation") | ||
|
|
||
| def test_real_agent_task_path_uses_the_bounded_measurement_helper(self) -> None: | ||
| """The real controlled browser pass must not bypass the bounded helper.""" | ||
|
|
||
| runner = RUNNER.read_text(encoding="utf-8") | ||
| self.assertIn( | ||
| "semantic_observation_bytes = _measure_agent_task_semantic_observation_bytes(", | ||
| runner, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
90 changes: 90 additions & 0 deletions
90
tests/test_agent_task_semantic_observation_schema_contract.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,90 @@ | ||
| """Contract for the controlled Agent Task semantic-observation evidence schema.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| import pathlib | ||
| import unittest | ||
|
|
||
| ROOT = pathlib.Path(__file__).resolve().parents[1] | ||
| RUNNER = ROOT / "scripts" / "ci" / "run_mv3_compatibility.py" | ||
|
|
||
|
|
||
| class AgentTaskSemanticObservationSchemaContractTests(unittest.TestCase): | ||
| """Keep unreviewed page content outside the controlled semantic evidence object.""" | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls) -> None: | ||
| cls.tree = ast.parse(RUNNER.read_text(encoding="utf-8"), filename=str(RUNNER)) | ||
|
|
||
| @staticmethod | ||
| def _literal_dict_keys(node: ast.Dict) -> tuple[str, ...]: | ||
| """Return exact string-literal dictionary keys or fail the contract.""" | ||
|
|
||
| keys: list[str] = [] | ||
| for key in node.keys: | ||
| if not isinstance(key, ast.Constant) or not isinstance(key.value, str): | ||
| raise AssertionError("semantic observation keys must be string literals") | ||
| keys.append(key.value) | ||
| return tuple(keys) | ||
|
|
||
| def _semantic_observation_assignment(self) -> ast.Dict: | ||
| """Find the one executable controlled-observation construction.""" | ||
|
|
||
| assignments = [ | ||
| node | ||
| for node in ast.walk(self.tree) | ||
| if isinstance(node, ast.Assign) | ||
| and any( | ||
| isinstance(target, ast.Name) and target.id == "semantic_observation" | ||
| for target in node.targets | ||
| ) | ||
| ] | ||
| self.assertEqual(len(assignments), 1) | ||
| value = assignments[0].value | ||
| self.assertIsInstance(value, ast.Dict) | ||
| return value | ||
|
|
||
| def test_controlled_observation_has_only_reviewed_role_name_fields(self) -> None: | ||
| """Raw page text or instruction-like fields cannot drift into emitted evidence.""" | ||
|
|
||
| observation = self._semantic_observation_assignment() | ||
| self.assertEqual(self._literal_dict_keys(observation), ("input", "submit")) | ||
| semantic_nodes = dict(zip(self._literal_dict_keys(observation), observation.values)) | ||
|
|
||
| expected_values = { | ||
| "input": {"role": "input_role", "name": "input_name"}, | ||
| "submit": {"role": "submit_role", "name": "submit_name"}, | ||
| } | ||
| for semantic_key, expected_fields in expected_values.items(): | ||
| semantic_node = semantic_nodes[semantic_key] | ||
| self.assertIsInstance(semantic_node, ast.Dict) | ||
| self.assertEqual(self._literal_dict_keys(semantic_node), ("role", "name")) | ||
| actual_fields = dict( | ||
| zip(self._literal_dict_keys(semantic_node), semantic_node.values) | ||
| ) | ||
| for field_name, expected_variable in expected_fields.items(): | ||
| value = actual_fields[field_name] | ||
| self.assertIsInstance(value, ast.Name) | ||
| self.assertEqual(value.id, expected_variable) | ||
|
|
||
| def test_exact_observation_flows_through_bounded_measurement(self) -> None: | ||
| """The reviewed object must be the exact object sent to the byte-bound helper.""" | ||
|
|
||
| calls = [ | ||
| node | ||
| for node in ast.walk(self.tree) | ||
| if isinstance(node, ast.Call) | ||
| and isinstance(node.func, ast.Name) | ||
| and node.func.id == "_measure_agent_task_semantic_observation_bytes" | ||
| ] | ||
| self.assertEqual(len(calls), 1) | ||
| self.assertEqual(len(calls[0].args), 1) | ||
| argument = calls[0].args[0] | ||
| self.assertIsInstance(argument, ast.Name) | ||
| self.assertEqual(argument.id, "semantic_observation") | ||
| self.assertFalse(calls[0].keywords) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.