Skip to content

Automate browser based workflows with AI

Keep useful page facts when inspection output is oversized (#8339)

Andrew Neilson319bab9b20e0
diff --git a/skyvern/forge/sdk/copilot/composition_browser_expressions.py b/skyvern/forge/sdk/copilot/composition_browser_expressions.py
index 9bff17aec..737b969ac 100644
--- a/skyvern/forge/sdk/copilot/composition_browser_expressions.py
+++ b/skyvern/forge/sdk/copilot/composition_browser_expressions.py
@@ -30,6 +30,7 @@ from skyvern.forge.sdk.copilot.composition_evidence import (
_RENDERED_INTERCEPTS_OUTSIDE_CONTROL_ATTR,
_RENDERED_STYLE_SNAPSHOT_ATTR,
_RESULT_CONTAINER_HINTS,
+ COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS,
OBSERVED_CHECKED_FIELD_TYPES,
OBSERVED_VALUE_FIELD_TYPES,
)
@@ -488,9 +489,6 @@ COMPOSITION_VISUAL_OBSTRUCTION_CANDIDATES_EXPRESSION = (
"})()"
)
-# Safety bound; an over-cap payload is returned as a typed structured-extraction failure.
-COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS = 120_000
-
# Injected from composition_evidence so the JS matches the parser's caps/vocabulary (single source of truth).
_STRUCTURED_CONST_HEADER = (
f"const ANTI_BOT_PATTERNS={json.dumps(list(_ANTI_BOT_PATTERNS))};"
@@ -517,7 +515,9 @@ _STRUCTURED_CONST_HEADER = (
f"const MAX_PAGE_OBSTRUCTIONS={int(_MAX_PAGE_OBSTRUCTIONS)};"
f"const MAX_VISIBLE_CONTROLS={int(_MAX_VISIBLE_CONTROLS)};"
f"const MAX_VISIBLE_TEXT_EXCERPT_CHARS={int(_MAX_VISIBLE_TEXT_EXCERPT_CHARS)};"
- f"const ANTI_BOT_SCAN_BYTES={int(_ANTI_BOT_SCAN_BYTES)};" + _JS_IMPLICIT_ROLE_HELPER
+ f"const ANTI_BOT_SCAN_BYTES={int(_ANTI_BOT_SCAN_BYTES)};"
+ + _JS_IMPLICIT_ROLE_HELPER
+ + f"const STRUCTURED_EVIDENCE_MAX_CHARS={int(COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS)};"
)
# Mirrors parse_composition_html's structural extraction; Python re-bounds the values to the exact caps.
@@ -1573,7 +1573,85 @@ const haystack = (pageTitle + '\n' + scanHtml.slice(0, ANTI_BOT_SCAN_BYTES)).toL
const antiBotIndicators = ANTI_BOT_PATTERNS.filter((p) => haystack.includes(p));
const visibleText = document.body ? (document.body.innerText || '') : '';
-return JSON.stringify({
+const boundedStructuredEvidence = (payload) => {
+ const pythonCharCount = (value) => Array.from(value).length;
+ let serialized = JSON.stringify(payload);
+ let serializedCharCount = pythonCharCount(serialized);
+ if (serializedCharCount <= STRUCTURED_EVIDENCE_MAX_CHARS) return serialized;
+
+ const originalCharCount = serializedCharCount;
+ const omissions = [];
+ payload.size_compaction = { original_char_count: originalCharCount, omissions: omissions };
+ const recordOmission = (category, omittedCount, unit) => {
+ if (omittedCount <= 0) return;
+ const existing = omissions.find((item) => item.category === category);
+ if (existing) existing.omitted_count += omittedCount;
+ else omissions.push({ category: category, omitted_count: omittedCount, unit: unit });
+ };
+ const refresh = () => {
+ serialized = JSON.stringify(payload);
+ serializedCharCount = pythonCharCount(serialized);
+ };
+ const shedWhileNeeded = (shed) => {
+ while (serializedCharCount > STRUCTURED_EVIDENCE_MAX_CHARS && shed()) refresh();
+ };
+ const shedArrayTail = (category) => () => {
+ const values = payload[category];
+ if (!Array.isArray(values) || !values.length) return false;
+ values.pop();
+ recordOmission(category, 1, 'entries');
+ return true;
+ };
+ const shedOptionRound = () => {
+ let removed = 0;
+ for (let formIndex = payload.forms.length - 1; formIndex >= 0; formIndex--) {
+ const fields = Array.isArray(payload.forms[formIndex].fields) ? payload.forms[formIndex].fields : [];
+ for (let fieldIndex = fields.length - 1; fieldIndex >= 0; fieldIndex--) {
+ const options = fields[fieldIndex].options;
+ if (!Array.isArray(options) || !options.length) continue;
+ options.pop();
+ removed += 1;
+ }
+ }
+ recordOmission('forms.fields.options', removed, 'entries');
+ return removed > 0;
+ };
+ const shedResultDetailRound = (key) => () => {
+ let removed = 0;
+ for (let index = payload.result_containers.length - 1; index >= 0; index--) {
+ const values = payload.result_containers[index][key];
+ if (!Array.isArray(values) || !values.length) continue;
+ values.pop();
+ removed += 1;
+ }
+ recordOmission('result_containers.' + key, removed, 'entries');
+ return removed > 0;
+ };
+
+ refresh();
+ shedWhileNeeded(() => {
+ if (!payload.visible_text_excerpt) return false;
+ const omittedCount = pythonCharCount(payload.visible_text_excerpt);
+ payload.visible_text_excerpt = '';
+ recordOmission('visible_text_excerpt', omittedCount, 'characters');
+ return true;
+ });
+ shedWhileNeeded(shedOptionRound);
+ shedWhileNeeded(shedResultDetailRound('sample_rows'));
+ shedWhileNeeded(shedResultDetailRound('rows'));
+ shedWhileNeeded(shedArrayTail('navigation_targets'));
+ shedWhileNeeded(shedArrayTail('clickable_controls'));
+ shedWhileNeeded(shedArrayTail('forms'));
+ shedWhileNeeded(shedArrayTail('result_containers'));
+ shedWhileNeeded(shedArrayTail('key_value_relations'));
+ shedWhileNeeded(shedArrayTail('visual_obstruction_candidates'));
+ shedWhileNeeded(shedArrayTail('modal_overlays'));
+ shedWhileNeeded(shedArrayTail('page_obstructions'));
+ shedWhileNeeded(shedArrayTail('challenge_controls'));
+ return serialized;
+};
+
+const structuredEvidence = {
page_title: pageTitle,
forms: forms,
navigation_targets_truncated: navigationTargetsTruncated,
@@ -1593,7 +1671,8 @@ return JSON.stringify({
visible_text_excerpt: visibleText.length > MAX_VISIBLE_TEXT_EXCERPT_CHARS * 2 ? visibleText.slice(0, MAX_VISIBLE_TEXT_EXCERPT_CHARS * 2) : visibleText,
body_has_markup: !!(document.body && (document.body.children.length > 0 || (document.body.textContent || '').trim().length > 0)),
anti_bot_indicators: antiBotIndicators,
-});
+};
+return boundedStructuredEvidence(structuredEvidence);
"""
diff --git a/skyvern/forge/sdk/copilot/composition_evidence.py b/skyvern/forge/sdk/copilot/composition_evidence.py
index ddb596ca8..4eb4afcde 100644
--- a/skyvern/forge/sdk/copilot/composition_evidence.py
+++ b/skyvern/forge/sdk/copilot/composition_evidence.py
@@ -25,6 +25,7 @@ from skyvern.forge.sdk.copilot.challenge_evidence import (
normalized_challenge_kind,
vision_challenge_carrier,
)
+from skyvern.forge.sdk.copilot.composition_evidence_size import size_compaction_omits
from skyvern.forge.sdk.copilot.page_identity import page_record_matches_url, page_records_share_location
from skyvern.forge.sdk.copilot.runtime import ScoutedSelectorCandidate
from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence
@@ -32,6 +33,7 @@ from skyvern.utils.yaml_loader import safe_load_no_dates
LOG = structlog.get_logger()
INTERNAL_VALIDATION_FAILURE_PREFIX = "Workflow validation failed: "
+COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS = 120_000
# Block types whose acted page, when no url is on the block, is the current
# frontier (observation of the page suffices). navigation without a url is the
@@ -110,6 +112,21 @@ _EMPTY_RESULT_TEXT_PATTERNS: frozenset[str] = frozenset(
)
_MAX_VISUAL_SUMMARY_CHARS = 500
_MAX_VISUAL_OMISSIONS = 5
+_SIZE_COMPACTION_CATEGORY_UNITS: dict[str, str] = {
+ "visible_text_excerpt": "characters",
+ "forms.fields.options": "entries",
+ "result_containers.rows": "entries",
+ "result_containers.sample_rows": "entries",
+ "navigation_targets": "entries",
+ "clickable_controls": "entries",
+ "forms": "entries",
+ "result_containers": "entries",
+ "key_value_relations": "entries",
+ "visual_obstruction_candidates": "entries",
+ "modal_overlays": "entries",
+ "page_obstructions": "entries",
+ "challenge_controls": "entries",
+}
_ANTI_BOT_SCAN_BYTES = 250_000
_NON_ENTRY_FIELD_TYPES: frozenset[str] = frozenset(
{"hidden", "submit", "button", "reset", "checkbox", "radio", "file", "image"}
@@ -819,7 +836,9 @@ def has_witnessed_value_content(evidence: dict[str, Any]) -> bool:
warnings = evidence.get("inspection_warnings")
if isinstance(warnings, list) and warnings:
return False
- if evidence.get("key_value_relations_truncated") is not True:
+ if evidence.get("key_value_relations_truncated") is not True and not size_compaction_omits(
+ evidence, {"key_value_relations"}
+ ):
relations = evidence.get("key_value_relations")
if isinstance(relations, list):
for relation in relations:
@@ -828,7 +847,10 @@ def has_witnessed_value_content(evidence: dict[str, Any]) -> bool:
value_text = relation.get("value_text")
if isinstance(value_text, str) and value_text.strip():
return True
- if evidence.get("result_containers_truncated") is not True:
+ if evidence.get("result_containers_truncated") is not True and not size_compaction_omits(
+ evidence,
+ {"result_containers", "result_containers.rows", "result_containers.sample_rows"},
+ ):
containers = evidence.get("result_containers")
if isinstance(containers, list):
for container in containers:
@@ -3933,6 +3955,47 @@ def _structured_visual_obstruction_candidates(value: Any) -> list[dict[str, Any]
return candidates
+def _structured_size_compaction(value: Any) -> dict[str, Any] | None:
+ if not isinstance(value, dict):
+ return None
+ original_char_count = value.get("original_char_count")
+ if (
+ not isinstance(original_char_count, int)
+ or isinstance(original_char_count, bool)
+ or original_char_count <= COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ ):
+ return None
+ omissions: list[dict[str, Any]] = []
+ seen_categories: set[str] = set()
+ for item in value.get("omissions") or []:
+ if not isinstance(item, dict):
+ continue
+ category = item.get("category")
+ if not isinstance(category, str):
+ continue
+ expected_unit = _SIZE_COMPACTION_CATEGORY_UNITS.get(category)
+ if expected_unit is None:
+ LOG.warning(
+ "copilot_structured_size_compaction_unknown_category_ignored",
+ category=category,
+ )
+ continue
+ omitted_count = item.get("omitted_count")
+ if (
+ category in seen_categories
+ or item.get("unit") != expected_unit
+ or not isinstance(omitted_count, int)
+ or isinstance(omitted_count, bool)
+ or omitted_count <= 0
+ ):
+ continue
+ seen_categories.add(category)
+ omissions.append({"category": category, "omitted_count": omitted_count, "unit": expected_unit})
+ if not omissions:
+ return None
+ return {"original_char_count": original_char_count, "omissions": omissions}
+
+
def parse_composition_structured(data: Any, *, inspected_url: str, current_url: str) -> dict[str, Any] | None:
"""Map bounded structured JSON to PageEvidence; None denotes an invalid structured result."""
if not isinstance(data, dict):
@@ -3960,6 +4023,7 @@ def parse_composition_structured(data: Any, *, inspected_url: str, current_url:
+ _structured_interaction_blocking_obstructions(data.get("page_obstructions"))
)[:_MAX_PAGE_OBSTRUCTIONS]
visual_obstruction_candidates = _structured_visual_obstruction_candidates(data.get("visual_obstruction_candidates"))
+ size_compaction = _structured_size_compaction(data.get("size_compaction"))
visible_text = _schema_text(_structured_str(data.get("visible_text_excerpt")), _MAX_VISIBLE_TEXT_EXCERPT_CHARS)
# Re-validate JS-reported indicators against _ANTI_BOT_PATTERNS and union a title scan.
@@ -3984,6 +4048,7 @@ def parse_composition_structured(data: Any, *, inspected_url: str, current_url:
"inspected_url": inspected_url,
"current_url": current_url,
"page_title": page_title,
+ **({"size_compaction": size_compaction} if size_compaction is not None else {}),
"forms": forms,
# Ahead of the list it describes: tool output is head-truncated, so a flag placed after a
# long navigation_targets is dropped exactly on the pages where it is true.
diff --git a/skyvern/forge/sdk/copilot/composition_evidence_size.py b/skyvern/forge/sdk/copilot/composition_evidence_size.py
new file mode 100644
index 000000000..7a4084d93
--- /dev/null
+++ b/skyvern/forge/sdk/copilot/composition_evidence_size.py
@@ -0,0 +1,17 @@
+"""Shared size-compaction facts for bounded composition evidence."""
+
+from __future__ import annotations
+
+from collections.abc import Iterable
+from typing import Any
+
+
+def size_compaction_omits(evidence: dict[str, Any], categories: Iterable[str]) -> bool:
+ compaction = evidence.get("size_compaction")
+ if not isinstance(compaction, dict):
+ return False
+ category_set = set(categories)
+ return any(
+ isinstance(omission, dict) and omission.get("category") in category_set
+ for omission in compaction.get("omissions") or []
+ )
diff --git a/skyvern/forge/sdk/copilot/output_extraction_plan.py b/skyvern/forge/sdk/copilot/output_extraction_plan.py
index a17b6d82d..8432f7275 100644
--- a/skyvern/forge/sdk/copilot/output_extraction_plan.py
+++ b/skyvern/forge/sdk/copilot/output_extraction_plan.py
@@ -9,6 +9,7 @@ from dataclasses import dataclass
from enum import StrEnum
from typing import Any, TypeGuard
+from skyvern.forge.sdk.copilot.composition_evidence_size import size_compaction_omits
from skyvern.forge.sdk.copilot.page_identity import safe_page_origin
@@ -737,9 +738,14 @@ def _intact_binding_channels(packet: dict[str, Any]) -> tuple[bool, bool]:
binding whose own evidence is complete. Truncation still voids the channel it happened in,
where an unseen duplicate could have made the bind ambiguous.
"""
+ key_values_omitted = size_compaction_omits(packet, {"key_value_relations"})
+ containers_omitted = size_compaction_omits(
+ packet,
+ {"result_containers", "result_containers.rows", "result_containers.sample_rows"},
+ )
return (
- packet.get("key_value_relations_truncated") is False,
- packet.get("result_containers_truncated") is False,
+ packet.get("key_value_relations_truncated") is False and not key_values_omitted,
+ packet.get("result_containers_truncated") is False and not containers_omitted,
)
diff --git a/skyvern/forge/sdk/copilot/result_evidence.py b/skyvern/forge/sdk/copilot/result_evidence.py
index 72ae9400d..5d1133cfd 100644
--- a/skyvern/forge/sdk/copilot/result_evidence.py
+++ b/skyvern/forge/sdk/copilot/result_evidence.py
@@ -9,6 +9,7 @@ from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any
+from skyvern.forge.sdk.copilot.composition_evidence_size import size_compaction_omits
from skyvern.forge.sdk.copilot.output_extraction_plan import (
LiveReadBinding,
LiveReadKind,
@@ -323,6 +324,17 @@ def mint_scout_observation_contract(
quorum, and truncated or warned captures void the mint."""
if not isinstance(page_evidence, Mapping) or not url.strip() or not labels_by_path:
return None
+ packet = dict(page_evidence)
+ if size_compaction_omits(
+ packet,
+ {
+ "key_value_relations",
+ "result_containers",
+ "result_containers.rows",
+ "result_containers.sample_rows",
+ },
+ ):
+ return None
if page_evidence.get("result_containers_truncated") is not False:
return None
if page_evidence.get("key_value_relations_truncated") is not False:
@@ -330,8 +342,6 @@ def mint_scout_observation_contract(
warnings = page_evidence.get("inspection_warnings")
if isinstance(warnings, list) and warnings:
return None
- packet = dict(page_evidence)
-
lexical_by_path: dict[str, list[LiveReadBinding]] = {}
for binding in _key_value_bindings(packet, labels_by_path):
lexical_by_path.setdefault(binding.output_path, []).append(binding)
diff --git a/skyvern/forge/sdk/copilot/tools/scouting.py b/skyvern/forge/sdk/copilot/tools/scouting.py
index a8b8de4e0..3a0acc35b 100644
--- a/skyvern/forge/sdk/copilot/tools/scouting.py
+++ b/skyvern/forge/sdk/copilot/tools/scouting.py
@@ -994,6 +994,7 @@ def _evidence_list_len(packet: dict[str, Any] | None, key: str) -> int:
_PAGE_EVIDENCE_STATE_KEYS = (
"page_title",
+ "size_compaction",
"forms",
"navigation_targets",
"navigation_targets_truncated",
@@ -1431,7 +1432,7 @@ def _build_scout_page_summary(evidence: dict[str, Any]) -> dict[str, Any]:
continue
seen_disclosures.add(identity)
disclosure_controls.append(summary)
- return {
+ page_summary: dict[str, Any] = {
"page_title": _summary_text(evidence.get("page_title")),
"forms": forms_summary,
"navigation_target_count": len(nav_targets),
@@ -1449,6 +1450,10 @@ def _build_scout_page_summary(evidence: dict[str, Any]) -> dict[str, Any]:
"modal_dismiss_controls": dismiss_entries,
"interaction_blocking_layers": interaction_blocking_layers,
}
+ size_compaction = evidence.get("size_compaction")
+ if isinstance(size_compaction, dict):
+ page_summary["size_compaction"] = size_compaction
+ return page_summary
def _drop_scout_page_summary_selectors(summary: dict[str, Any]) -> bool:
diff --git a/tests/unit/test_copilot_composition_evidence.py b/tests/unit/test_copilot_composition_evidence.py
index d95ba91ba..1e2e85f8e 100644
--- a/tests/unit/test_copilot_composition_evidence.py
+++ b/tests/unit/test_copilot_composition_evidence.py
@@ -34,6 +34,7 @@ from skyvern.forge.sdk.copilot.composition_browser_expressions import (
_STRUCTURED_EVIDENCE_BODY,
COMPOSITION_STRIPPED_HTML_EXPRESSION,
COMPOSITION_STRUCTURED_EVIDENCE_EXPRESSION,
+ COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS,
COMPOSITION_VISUAL_OBSTRUCTION_CANDIDATES_EXPRESSION,
)
from skyvern.forge.sdk.copilot.composition_evidence import (
@@ -2059,6 +2060,33 @@ def test_normalize_block_observation_refs_warns_on_unexpected_container_type() -
)
+def test_parse_structured_evidence_warns_on_unknown_size_compaction_category() -> None:
+ payload = {
+ "size_compaction": {
+ "original_char_count": 120_001,
+ "omissions": [
+ {"category": "future_category", "omitted_count": 1, "unit": "entries"},
+ {"category": "navigation_targets", "omitted_count": 2, "unit": "entries"},
+ ],
+ }
+ }
+
+ with patch("skyvern.forge.sdk.copilot.composition_evidence.LOG.warning") as warning:
+ parsed = parse_composition_structured(
+ payload, inspected_url="https://example.com", current_url="https://example.com"
+ )
+
+ assert parsed is not None
+ assert parsed["size_compaction"] == {
+ "original_char_count": 120_001,
+ "omissions": [{"category": "navigation_targets", "omitted_count": 2, "unit": "entries"}],
+ }
+ warning.assert_called_once_with(
+ "copilot_structured_size_compaction_unknown_category_ignored",
+ category="future_category",
+ )
+
+
def test_composition_gate_reports_string_typed_observation_step_from_raw_refs() -> None:
workflow_yaml = _yaml(
{"block_type": "goto_url", "label": "open_home", "url": "https://example.com/"},
@@ -3567,6 +3595,7 @@ async def test_structured_browser_packet_reports_collapsed_disclosure_relationsh
)
packet = json.loads(raw)
+ assert "size_compaction" not in packet
assert packet["clickable_controls"][0]["expanded"] is False
assert packet["clickable_controls"][0]["controls"] == "alternatives"
assert packet["clickable_controls"][0]["controlled_region_visible"] is False
@@ -4625,6 +4654,71 @@ class _RecordingCompositionServer:
return {"ok": False, "error": f"unexpected tool {tool_name}"}
+class _LiveCompositionServer:
+ def __init__(self, page: Page) -> None:
+ self.page = page
+ self.calls: list[str] = []
+ self.results: list[str] = []
+
+ async def call_internal_tool(self, tool_name: str, arguments: dict[str, Any]) -> dict[str, Any]:
+ if tool_name != "skyvern_evaluate":
+ raise AssertionError(f"unexpected tool {tool_name}")
+ expression = arguments.get("expression")
+ if not isinstance(expression, str):
+ raise TypeError("structured extraction requires a string expression")
+ self.calls.append(tool_name)
+ result = await self.page.evaluate(expression)
+ if isinstance(result, str):
+ self.results.append(result)
+ return {"ok": True, "data": {"result": result}}
+
+
+def _dense_oversized_structured_html() -> str:
+ forms: list[str] = []
+ for form_index in range(3):
+ fields: list[str] = []
+ for field_index in range(5):
+ options = "".join(
+ f'<option value="value-{form_index}-{field_index}-{option_index}-{"V" * 145}">'
+ f"Option {form_index}-{field_index}-{option_index} {'T' * 105}</option>"
+ for option_index in range(30)
+ )
+ fields.append(
+ f'<label for="field-{form_index}-{field_index}">Dense field {form_index}-{field_index}</label>'
+ f'<select id="field-{form_index}-{field_index}" name="field-{form_index}-{field_index}">'
+ f"{options}</select>"
+ )
+ forms.append(
+ f'<form id="form-{form_index}" action="/submit/{form_index}">{"".join(fields)}'
+ f'<button id="submit-{form_index}" type="submit">Submit {form_index}</button></form>'
+ )
+ return (
+ "<html><head><title>Dense comparison page</title></head><body>"
+ + "".join(forms)
+ + '<table id="results" class="results"><thead><tr><th>Plan</th><th>Price</th></tr></thead>'
+ "<tbody><tr><td>Starter</td><td>$10</td></tr></tbody></table></body></html>"
+ )
+
+
+def _dense_astral_structured_html(*, value_chars: int = 75, text_chars: int = 55) -> str:
+ forms: list[str] = []
+ for form_index in range(3):
+ fields: list[str] = []
+ for field_index in range(5):
+ options = "".join(
+ f'<option value="value-{form_index}-{field_index}-{option_index}-{"🚀" * value_chars}">'
+ f"Option {form_index}-{field_index}-{option_index} {'🌟' * text_chars}</option>"
+ for option_index in range(30)
+ )
+ fields.append(
+ f'<label for="astral-{form_index}-{field_index}">Astral field {form_index}-{field_index}</label>'
+ f'<select id="astral-{form_index}-{field_index}" name="astral-{form_index}-{field_index}">'
+ f"{options}</select>"
+ )
+ forms.append(f'<form id="astral-form-{form_index}">{"".join(fields)}</form>')
+ return "<html><head><title>Astral page</title></head><body>" + "".join(forms) + "</body></html>"
+
+
_HTML_FORM_PAGE = (
"<html><head><title>T</title></head><body>"
"<form id='f'><input name='x'><button type='submit'>Go</button></form>"
@@ -4703,6 +4797,138 @@ async def test_capture_reports_oversize_structured_dict_without_calling_get_html
assert server.calls.count("skyvern_get_html") == 0
+@_skip_no_browser
+async def test_capture_compacts_dense_live_structured_packet_before_the_python_bound() -> None:
+ async with async_playwright() as playwright:
+ browser = await playwright.chromium.launch(headless=True)
+ page = await browser.new_page()
+ await page.set_content(_dense_oversized_structured_html())
+ server = _LiveCompositionServer(page)
+ ctx = SimpleNamespace(discovery_mcp_server=server)
+ try:
+ evidence, error = await tools_module._capture_composition_evidence(
+ ctx,
+ inspected_url="https://example.com/compare",
+ current_url="https://example.com/compare",
+ )
+ finally:
+ await browser.close()
+
+ assert len(server.results) == 1
+ raw = server.results[0]
+ assert len(raw) <= COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ payload = json.loads(raw)
+ compaction = payload["size_compaction"]
+ assert compaction["original_char_count"] > COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ assert [omission["category"] for omission in compaction["omissions"]] == [
+ "visible_text_excerpt",
+ "forms.fields.options",
+ ]
+ assert compaction["omissions"][0] == {
+ "category": "visible_text_excerpt",
+ "omitted_count": 6000,
+ "unit": "characters",
+ }
+ option_omission = next(
+ omission for omission in compaction["omissions"] if omission["category"] == "forms.fields.options"
+ )
+ retained_options = sum(
+ len(field.get("options") or []) for form in payload["forms"] for field in form.get("fields") or []
+ )
+ assert option_omission == {
+ "category": "forms.fields.options",
+ "omitted_count": 450 - retained_options,
+ "unit": "entries",
+ }
+ assert error is None
+ assert evidence is not None
+ assert evidence["page_title"] == "Dense comparison page"
+ assert [form["id"] for form in evidence["forms"]] == ["form-0", "form-1", "form-2"]
+ assert evidence["result_containers"][0]["id"] == "results"
+ assert evidence["size_compaction"] == compaction
+ assert evidence["inspection_warnings"] == []
+ model_visible = _model_facing_inspect_result({"ok": True, "data": evidence})
+ assert model_visible["data"]["size_compaction"] == compaction
+
+
+@_skip_no_browser
+async def test_capture_preserves_python_under_limit_astral_packet_byte_for_byte() -> None:
+ unbounded_expression = COMPOSITION_STRUCTURED_EVIDENCE_EXPRESSION.replace(
+ "return boundedStructuredEvidence(structuredEvidence);",
+ "return JSON.stringify(structuredEvidence);",
+ )
+ assert unbounded_expression != COMPOSITION_STRUCTURED_EVIDENCE_EXPRESSION
+
+ async with async_playwright() as playwright:
+ browser = await playwright.chromium.launch(headless=True)
+ page = await browser.new_page()
+ await page.set_content(_dense_astral_structured_html())
+ expected_raw = await page.evaluate(unbounded_expression)
+ server = _LiveCompositionServer(page)
+ ctx = SimpleNamespace(discovery_mcp_server=server)
+ try:
+ evidence, error = await tools_module._capture_composition_evidence(
+ ctx,
+ inspected_url="https://example.com/astral",
+ current_url="https://example.com/astral",
+ )
+ finally:
+ await browser.close()
+
+ assert isinstance(expected_raw, str)
+ assert len(expected_raw) <= COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ assert len(expected_raw.encode("utf-16-le")) // 2 > COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ assert server.results == [expected_raw]
+ assert "size_compaction" not in json.loads(expected_raw)
+ assert error is None
+ assert evidence is not None
+ assert evidence["page_title"] == "Astral page"
+ assert "size_compaction" not in evidence
+
+
+@_skip_no_browser
+async def test_capture_reports_python_character_counts_for_compacted_astral_packet() -> None:
+ unbounded_expression = COMPOSITION_STRUCTURED_EVIDENCE_EXPRESSION.replace(
+ "return boundedStructuredEvidence(structuredEvidence);",
+ "return JSON.stringify(structuredEvidence);",
+ )
+
+ async with async_playwright() as playwright:
+ browser = await playwright.chromium.launch(headless=True)
+ page = await browser.new_page()
+ await page.set_content(_dense_astral_structured_html(value_chars=145, text_chars=105))
+ expected_raw = await page.evaluate(unbounded_expression)
+ server = _LiveCompositionServer(page)
+ ctx = SimpleNamespace(discovery_mcp_server=server)
+ try:
+ evidence, error = await tools_module._capture_composition_evidence(
+ ctx,
+ inspected_url="https://example.com/astral",
+ current_url="https://example.com/astral",
+ )
+ finally:
+ await browser.close()
+
+ assert isinstance(expected_raw, str)
+ assert len(expected_raw) > COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ expected_payload = json.loads(expected_raw)
+ raw = server.results[0]
+ assert len(raw) <= COMPOSITION_STRUCTURED_EVIDENCE_MAX_CHARS
+ compaction = json.loads(raw)["size_compaction"]
+ assert compaction["original_char_count"] == len(expected_raw)
+ assert compaction["omissions"][0] == {
+ "category": "visible_text_excerpt",
+ "omitted_count": len(expected_payload["visible_text_excerpt"]),
+ "unit": "characters",
+ }
+ assert error is None
+ assert evidence is not None
+ assert evidence["size_compaction"] == compaction
+
+
@pytest.mark.asyncio
async def test_capture_reports_structured_timeout_without_calling_get_html() -> None:
server = _RecordingCompositionServer(
@@ -5149,6 +5375,17 @@ def test_has_witnessed_value_content_false_on_truncated_kv() -> None:
assert has_witnessed_value_content(packet) is False
+def test_has_witnessed_value_content_false_on_size_compacted_kv_with_dom_cap_flag_clear() -> None:
+ packet = _kv_value_content_packet()
+ packet["size_compaction"] = {
+ "original_char_count": 130_000,
+ "omissions": [{"category": "key_value_relations", "omitted_count": 1, "unit": "entries"}],
+ }
+
+ assert packet["key_value_relations_truncated"] is False
+ assert has_witnessed_value_content(packet) is False
+
+
def test_has_witnessed_value_content_false_on_inspection_warnings() -> None:
packet = _kv_value_content_packet()
packet["inspection_warnings"] = ["capture_incomplete"]
diff --git a/tests/unit/test_copilot_result_evidence.py b/tests/unit/test_copilot_result_evidence.py
index 0f29e5995..de2916d6d 100644
--- a/tests/unit/test_copilot_result_evidence.py
+++ b/tests/unit/test_copilot_result_evidence.py
@@ -253,6 +253,11 @@ def test_mint_returns_none_on_truncated_or_warned_capture() -> None:
truncated["result_containers_truncated"] = True
warned = _kv_page_evidence()
warned["inspection_warnings"] = ["partial capture"]
+ size_compacted = _kv_page_evidence()
+ size_compacted["size_compaction"] = {
+ "original_char_count": 130_000,
+ "omissions": [{"category": "key_value_relations", "omitted_count": 1, "unit": "entries"}],
+ }
labels = {"output.overall_credentialing_result": ("Overall Credentialing Result",)}
assert (
@@ -267,6 +272,16 @@ def test_mint_returns_none_on_truncated_or_warned_capture() -> None:
)
is None
)
+ assert size_compacted["key_value_relations_truncated"] is False
+ assert (
+ mint_scout_observation_contract(
+ size_compacted,
+ labels_by_path=labels,
+ url="https://example.com/x",
+ has_bounded_page_schema=True,
+ )
+ is None
+ )
def test_mint_returns_none_on_reveal_truncation_signal() -> None:
diff --git a/tests/unit/test_copilot_scout_act_observe.py b/tests/unit/test_copilot_scout_act_observe.py
index 4dd52d675..8e6fdff5b 100644
--- a/tests/unit/test_copilot_scout_act_observe.py
+++ b/tests/unit/test_copilot_scout_act_observe.py
@@ -844,6 +844,23 @@ class TestActObserveSuccess:
assert [control["text"] for control in page["modal_dismiss_controls"]] == ["Accept"]
assert len(json.dumps(result)) <= _SCOUT_RESULT_CHAR_CAP
+ @pytest.mark.asyncio
+ async def test_result_summary_carries_structured_size_omissions_to_the_authoring_model(self) -> None:
+ payload = _bounded_extractor_payload()
+ payload["size_compaction"] = {
+ "original_char_count": 132_400,
+ "omissions": [
+ {"category": "forms.fields.options", "omitted_count": 180, "unit": "entries"},
+ {"category": "visible_text_excerpt", "omitted_count": 6000, "unit": "characters"},
+ ],
+ }
+ ctx = _ctx(server=_server_returning(payload))
+
+ result = await _run_click(ctx)
+
+ assert result["data"]["page"]["size_compaction"] == payload["size_compaction"]
+ assert ctx.flow_evidence[0]["evidence"]["inspection_warnings"] == []
+
@pytest.mark.asyncio
async def test_result_summary_carries_disclosure_controls_to_the_authoring_model(self) -> None:
payload = _bounded_extractor_payload()