Skip to content

Automate browser based workflows with AI

SKY-12339: code-first authoring producer reads per-column tables (TABLE_COLUMN) into keyed returns via the shared reader (#7349)

Andrew Neilson809dfe2b2612
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index 905bf89b3..6ae49393f 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -1413,14 +1413,17 @@ def _set_return_expression(root: _ExtractionReturnNode, segments: Sequence[tuple
current.value_expression = expression
-def _array_prefix(binding: LiveReadBinding) -> tuple[tuple[str, bool], ...]:
- segments = output_path_segments(binding.output_path)
+def _array_prefix_of_segments(segments: Sequence[tuple[str, bool]]) -> tuple[tuple[str, bool], ...]:
for index, (_name, is_array) in enumerate(segments):
if is_array:
- return segments[: index + 1]
+ return tuple(segments[: index + 1])
return ()
+def _array_prefix(binding: LiveReadBinding) -> tuple[tuple[str, bool], ...]:
+ return _array_prefix_of_segments(output_path_segments(binding.output_path))
+
+
def _key_value_scalar_read_statements(binding: LiveReadBinding, variable: str, *, guard_empty: bool) -> list[str]:
container = f"page.locator({json.dumps(binding.selector)})"
target = f"{container}.nth({binding.selector_index})"
@@ -1450,6 +1453,80 @@ def _key_value_scalar_read_statements(binding: LiveReadBinding, variable: str, *
return statements
+def _table_group_read_lines(
+ bindings: list[LiveReadBinding],
+ *,
+ row_selector: str,
+ row_count: int,
+ prefix: tuple[tuple[str, bool], ...],
+ group_index: int,
+ records_variable: str,
+ cell_variable_base: str,
+ assemble_as_literal: bool,
+ guard_empty: bool = False,
+ none_leaf_segments: Sequence[tuple[tuple[str, bool], ...]] = (),
+) -> list[str]:
+ lines: list[str] = []
+ record_root = _ExtractionReturnNode()
+ for none_segments in none_leaf_segments:
+ _set_return_expression(record_root, none_segments, "None")
+ exemplar = bindings[0]
+ table = f"page.locator({json.dumps(exemplar.selector)})"
+ selected_table = f"{table}.nth({exemplar.selector_index})"
+ rows = f"page.locator({json.dumps(row_selector)})"
+ headers = f'{table}.nth({exemplar.selector_index}).locator(":scope > thead > tr > th")'
+ lines.append(f"if await {table}.count() != {exemplar.selector_count}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table identity changed")')
+ lines.append(f"if not await {table}.nth({exemplar.selector_index}).is_visible():")
+ lines.append(f'{_INDENT}raise ValueError("Observed table is no longer visible")')
+ lines.append(f'if await {selected_table}.locator(":scope table").count() != 0:')
+ lines.append(f'{_INDENT}raise ValueError("Observed table gained a nested table")')
+ lines.append(f'if await {table}.nth({exemplar.selector_index}).locator("[colspan], [rowspan]").count() != 0:')
+ lines.append(f'{_INDENT}raise ValueError("Observed table gained spanning cells")')
+ lines.append(f"if await {headers}.count() != {len(exemplar.headers)}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table header cardinality changed")')
+ for header_index, header_text in enumerate(exemplar.headers):
+ lines.append(f"if (await {headers}.nth({header_index}).inner_text()).strip() != {json.dumps(header_text)}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table header identity changed")')
+ lines.append(f"if await {rows}.count() != {row_count}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table row count changed")')
+ row_expressions: list[str] = []
+ if not assemble_as_literal:
+ lines.append(f"{records_variable} = []")
+ for row_index in range(row_count):
+ row = f"{rows}.nth({row_index})"
+ cells = f'{row}.locator(":scope > th, :scope > td")'
+ lines.append(f"if not await {row}.is_visible():")
+ lines.append(f'{_INDENT}raise ValueError("Observed table row is no longer visible")')
+ lines.append(f"if await {cells}.count() != {exemplar.row_cell_counts[row_index]}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table direct-cell cardinality changed")')
+ lines.append(f'if await {row}.locator(":scope > th").count() != 0:')
+ lines.append(f'{_INDENT}raise ValueError("Observed table row gained a row header")')
+ lines.append(
+ f'if " ".join((await {row}.inner_text()).split()) != {json.dumps(exemplar.row_identities[row_index])}:'
+ )
+ lines.append(f'{_INDENT}raise ValueError("Observed table row identity changed")')
+ for binding_index, binding in enumerate(sorted(bindings, key=lambda item: item.output_path)):
+ value_variable = f"{cell_variable_base}_{group_index}_{row_index}_{binding_index}"
+ lines.append(f"if not await {cells}.nth({binding.column_index}).is_visible():")
+ lines.append(f'{_INDENT}raise ValueError("Observed table cell is no longer visible")')
+ lines.append(f"{value_variable} = (await {cells}.nth({binding.column_index}).inner_text()).strip()")
+ if guard_empty:
+ lines.append(f"if not {value_variable}:")
+ lines.append(f'{_INDENT}raise ValueError("Observed table cell value is empty")')
+ _set_return_expression(
+ record_root, output_path_segments(binding.output_path)[len(prefix) :], value_variable
+ )
+ row_expression = _return_node_expression(record_root)
+ if assemble_as_literal:
+ row_expressions.append(row_expression)
+ else:
+ lines.append(f"{records_variable}.append({row_expression})")
+ if assemble_as_literal:
+ lines.append(f"{records_variable} = [{', '.join(row_expressions)}]")
+ return lines
+
+
def synthesize_extraction_suffix(plan: RequestedOutputExtractionPlan) -> SynthesizedExtractionSuffix | None:
if not plan.live_reads:
return None
@@ -1471,50 +1548,18 @@ def synthesize_extraction_suffix(plan: RequestedOutputExtractionPlan) -> Synthes
table_groups.setdefault((binding.row_selector, binding.row_count, prefix), []).append(binding)
for group_index, ((row_selector, row_count, prefix), bindings) in enumerate(sorted(table_groups.items())):
records_variable = f"_extraction_records_{group_index}"
- record_root = _ExtractionReturnNode()
- exemplar = bindings[0]
- table = f"page.locator({json.dumps(exemplar.selector)})"
- selected_table = f"{table}.nth({exemplar.selector_index})"
- rows = f"page.locator({json.dumps(row_selector)})"
- headers = f'{table}.nth({exemplar.selector_index}).locator(":scope > thead > tr > th")'
- lines.append(f"if await {table}.count() != {exemplar.selector_count}:")
- lines.append(f'{_INDENT}raise ValueError("Observed table identity changed")')
- lines.append(f"if not await {table}.nth({exemplar.selector_index}).is_visible():")
- lines.append(f'{_INDENT}raise ValueError("Observed table is no longer visible")')
- lines.append(f'if await {selected_table}.locator(":scope table").count() != 0:')
- lines.append(f'{_INDENT}raise ValueError("Observed table gained a nested table")')
- lines.append(f'if await {table}.nth({exemplar.selector_index}).locator("[colspan], [rowspan]").count() != 0:')
- lines.append(f'{_INDENT}raise ValueError("Observed table gained spanning cells")')
- lines.append(f"if await {headers}.count() != {len(exemplar.headers)}:")
- lines.append(f'{_INDENT}raise ValueError("Observed table header cardinality changed")')
- for header_index, header_text in enumerate(exemplar.headers):
- lines.append(f"if (await {headers}.nth({header_index}).inner_text()).strip() != {json.dumps(header_text)}:")
- lines.append(f'{_INDENT}raise ValueError("Observed table header identity changed")')
- lines.append(f"if await {rows}.count() != {row_count}:")
- lines.append(f'{_INDENT}raise ValueError("Observed table row count changed")')
- lines.append(f"{records_variable} = []")
- for row_index in range(row_count):
- row = f"{rows}.nth({row_index})"
- cells = f'{row}.locator(":scope > th, :scope > td")'
- lines.append(f"if not await {row}.is_visible():")
- lines.append(f'{_INDENT}raise ValueError("Observed table row is no longer visible")')
- lines.append(f"if await {cells}.count() != {exemplar.row_cell_counts[row_index]}:")
- lines.append(f'{_INDENT}raise ValueError("Observed table direct-cell cardinality changed")')
- lines.append(f'if await {row}.locator(":scope > th").count() != 0:')
- lines.append(f'{_INDENT}raise ValueError("Observed table row gained a row header")')
- lines.append(
- f'if " ".join((await {row}.inner_text()).split()) != {json.dumps(exemplar.row_identities[row_index])}:'
+ lines.extend(
+ _table_group_read_lines(
+ bindings,
+ row_selector=row_selector,
+ row_count=row_count,
+ prefix=prefix,
+ group_index=group_index,
+ records_variable=records_variable,
+ cell_variable_base="_extraction_cell",
+ assemble_as_literal=False,
)
- lines.append(f'{_INDENT}raise ValueError("Observed table row identity changed")')
- for binding_index, binding in enumerate(sorted(bindings, key=lambda item: item.output_path)):
- value_variable = f"_extraction_cell_{group_index}_{row_index}_{binding_index}"
- lines.append(f"if not await {cells}.nth({binding.column_index}).is_visible():")
- lines.append(f'{_INDENT}raise ValueError("Observed table cell is no longer visible")')
- lines.append(f"{value_variable} = (await {cells}.nth({binding.column_index}).inner_text()).strip()")
- _set_return_expression(
- record_root, output_path_segments(binding.output_path)[len(prefix) :], value_variable
- )
- lines.append(f"{records_variable}.append({_return_node_expression(record_root)})")
+ )
_set_return_expression(return_root, prefix, records_variable)
lines.append(f"return {_return_node_expression(return_root)}")
@@ -1524,6 +1569,8 @@ def synthesize_extraction_suffix(plan: RequestedOutputExtractionPlan) -> Synthes
_ENVELOPE_SCALAR_VAR_BASE = "_envelope_value"
+_ENVELOPE_CELL_VAR_BASE = "_envelope_cell"
+_ENVELOPE_RECORDS_VAR_BASE = "_envelope_records"
@dataclass(frozen=True, slots=True)
@@ -1579,12 +1626,14 @@ def produce_covered_static_return_envelope(
download_required_paths: set[str],
expects_download: bool,
) -> ProducedStaticReturnEnvelope | None:
- """Author the unique terminal keyed return covering every scout-bound scalar path, plus
- None-default declaration children and the resolved download descriptor for the mixed shape.
-
- Fail-closed: emits only when it owns the sole terminal return (zero returns, or the single
- generator-owned download-idiom return), when every scalar path is plan-bound, and when no
- minted local collides with an identifier already in the block."""
+ """Author the unique terminal keyed return covering every scout-bound scalar KEY_VALUE path and
+ every TABLE_COLUMN path as list-of-record reads via the shared ``_table_group_read_lines`` reader,
+ seeding per-row None leaves for sibling declaration columns alongside None-default top-level
+ declarations and the resolved download descriptor for the mixed shape.
+
+ Return None when it does not own the sole terminal return (zero returns, or the single
+ generator-owned download-idiom return), on any ungrounded or unbound required path, on nested-array
+ paths, on an array declaration with no unique matching table group, and on any minted-name collision."""
if download_required_paths and not expects_download:
return None
if not scalar_required_paths:
@@ -1599,16 +1648,35 @@ def produce_covered_static_return_envelope(
except SyntaxError:
return None
- bindings_by_path = {
- binding.output_path: binding for binding in plan.live_reads if binding.kind == LiveReadKind.KEY_VALUE
- }
- ordered_scalar_paths = sorted(scalar_required_paths)
- if any(path not in bindings_by_path for path in ordered_scalar_paths):
+ bindings_by_path = {binding.output_path: binding for binding in plan.live_reads}
+ ordered_required_paths = sorted(scalar_required_paths)
+ if any(path not in bindings_by_path for path in ordered_required_paths):
return None
- minted_vars = [f"{_ENVELOPE_SCALAR_VAR_BASE}_{index}" for index in range(len(ordered_scalar_paths))]
+ scalar_paths = [path for path in ordered_required_paths if bindings_by_path[path].kind == LiveReadKind.KEY_VALUE]
+ table_paths = [path for path in ordered_required_paths if bindings_by_path[path].kind == LiveReadKind.TABLE_COLUMN]
+ if len(scalar_paths) + len(table_paths) != len(ordered_required_paths):
+ return None
+
+ table_groups: dict[tuple[str, int, tuple[tuple[str, bool], ...]], list[LiveReadBinding]] = {}
+ for path in table_paths:
+ binding = bindings_by_path[path]
+ prefix = _array_prefix(binding)
+ if not prefix or not binding.row_selector or binding.row_count <= 0:
+ return None
+ if any(is_array for _name, is_array in output_path_segments(binding.output_path)[len(prefix) :]):
+ return None
+ table_groups.setdefault((binding.row_selector, binding.row_count, prefix), []).append(binding)
+
+ minted_vars = [f"{_ENVELOPE_SCALAR_VAR_BASE}_{index}" for index in range(len(scalar_paths))]
+ minted_names = set(minted_vars)
+ for group_index, ((_row_selector, row_count, _prefix), group_bindings) in enumerate(sorted(table_groups.items())):
+ minted_names.add(f"{_ENVELOPE_RECORDS_VAR_BASE}_{group_index}")
+ for row_index in range(row_count):
+ for binding_index in range(len(group_bindings)):
+ minted_names.add(f"{_ENVELOPE_CELL_VAR_BASE}_{group_index}_{row_index}_{binding_index}")
existing_names = _bound_or_referenced_identifiers(tree)
- if any(var in existing_names for var in minted_vars):
+ if minted_names & existing_names:
return None
returns = _snippet_scope_returns(tree.body)
@@ -1636,13 +1704,48 @@ def produce_covered_static_return_envelope(
return None
preserved_lines = stripped_code.splitlines()
+ top_level_declarations: list[tuple[tuple[str, bool], ...]] = []
+ group_none_leaves: dict[tuple[str, int, tuple[tuple[str, bool], ...]], list[tuple[tuple[str, bool], ...]]] = {}
+ for path in sorted(declaration_paths):
+ segments = output_path_segments(path)
+ declaration_prefix = _array_prefix_of_segments(segments)
+ if not declaration_prefix:
+ top_level_declarations.append(segments)
+ continue
+ relative_segments = segments[len(declaration_prefix) :]
+ if any(is_array for _name, is_array in relative_segments):
+ return None
+ matching_groups = [key for key in table_groups if key[2] == declaration_prefix]
+ if len(matching_groups) != 1:
+ return None
+ group_none_leaves.setdefault(matching_groups[0], []).append(relative_segments)
+
return_root = _ExtractionReturnNode()
scalar_statements: list[str] = []
- for path, variable in zip(ordered_scalar_paths, minted_vars):
+ for path, variable in zip(scalar_paths, minted_vars):
scalar_statements.extend(_key_value_scalar_read_statements(bindings_by_path[path], variable, guard_empty=True))
_set_return_expression(return_root, output_path_segments(path), variable)
- for path in sorted(declaration_paths):
- _set_return_expression(return_root, output_path_segments(path), "None")
+ table_statements: list[str] = []
+ for group_index, (group_key, group_bindings) in enumerate(sorted(table_groups.items())):
+ row_selector, row_count, prefix = group_key
+ records_variable = f"{_ENVELOPE_RECORDS_VAR_BASE}_{group_index}"
+ table_statements.extend(
+ _table_group_read_lines(
+ group_bindings,
+ row_selector=row_selector,
+ row_count=row_count,
+ prefix=prefix,
+ group_index=group_index,
+ records_variable=records_variable,
+ cell_variable_base=_ENVELOPE_CELL_VAR_BASE,
+ assemble_as_literal=True,
+ guard_empty=True,
+ none_leaf_segments=group_none_leaves.get(group_key, ()),
+ )
+ )
+ _set_return_expression(return_root, prefix, records_variable)
+ for segments in top_level_declarations:
+ _set_return_expression(return_root, segments, "None")
if download_descriptor_key:
return_root.children.setdefault(
download_descriptor_key, _ExtractionReturnNode()
@@ -1650,6 +1753,7 @@ def produce_covered_static_return_envelope(
body_lines = list(preserved_lines)
body_lines.extend(scalar_statements)
+ body_lines.extend(table_statements)
body_lines.append(f"return {_return_node_expression(return_root)}")
produced_code = "\n".join(body_lines).strip() + "\n"
keyed_paths = tuple(sorted(scalar_required_paths | declaration_paths))
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index dfca744ae..e1944ae31 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -15328,6 +15328,149 @@ def test_producer_generated_code_raises_on_empty_scalar_text() -> None:
assert empty_guards
+def _table_binding(output_path: str, column_index: int) -> LiveReadBinding:
+ return LiveReadBinding(
+ output_path=output_path,
+ kind=LiveReadKind.TABLE_COLUMN,
+ selector="#records",
+ selector_count=1,
+ selector_index=0,
+ row_selector="#records > tbody > tr",
+ row_count=2,
+ column_index=column_index,
+ relation_label=output_path.rsplit(".", 1)[-1],
+ headers=("Location", "Status"),
+ row_cell_counts=(2, 2),
+ row_identities=("Alpha Open", "Beta Closed"),
+ )
+
+
+def test_producer_table_column_emits_credited_list_literal_and_revalidates() -> None:
+ plan = _scalar_plan(
+ _table_binding("output.locations[].location", 0),
+ _table_binding("output.locations[].status", 1),
+ )
+ required = {"output.locations[].location", "output.locations[].status"}
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths=required,
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert isinstance(envelope, ProducedStaticReturnEnvelope)
+ assert envelope.code.count("return {") == 1
+ assert "_envelope_records_0 = [{" in envelope.code
+ assert ".append(" not in envelope.code
+ produced = workflow_update_module._code_block_produced_output_paths(envelope.code)
+ assert required <= produced
+ _, violations = workflow_update_module._extraction_code_with_required_static_return(
+ envelope.code, required_paths=required
+ )
+ assert violations == []
+
+
+def test_producer_mixed_table_and_scalar_single_return_revalidates() -> None:
+ plan = _scalar_plan(
+ _scalar_binding("output.account_number", "Account Number", ".acct"),
+ _table_binding("output.locations[].location", 0),
+ _table_binding("output.locations[].status", 1),
+ )
+ required = {"output.account_number", "output.locations[].location", "output.locations[].status"}
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths=required,
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert isinstance(envelope, ProducedStaticReturnEnvelope)
+ assert envelope.code.count("return {") == 1
+ produced = workflow_update_module._code_block_produced_output_paths(envelope.code)
+ assert required <= produced
+ _, violations = workflow_update_module._extraction_code_with_required_static_return(
+ envelope.code, required_paths=required
+ )
+ assert violations == []
+
+
+def test_producer_abstains_when_required_table_path_ungrounded() -> None:
+ plan = _scalar_plan(_table_binding("output.locations[].location", 0))
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths={"output.locations[].location", "output.locations[].status"},
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert envelope is None
+
+
+def test_producer_abstains_on_table_cell_variable_collision() -> None:
+ plan = _scalar_plan(_table_binding("output.locations[].location", 0))
+ envelope = produce_covered_static_return_envelope(
+ '_envelope_cell_0_0_0 = 1\nawait page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths={"output.locations[].location"},
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert envelope is None
+
+
+def test_producer_table_with_sibling_declaration_emits_per_row_none_leaf() -> None:
+ plan = _scalar_plan(_table_binding("output.locations[].location", 0))
+ covered = {"output.locations[].location"}
+ declared = {"output.locations[].status"}
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths=covered,
+ declaration_paths=declared,
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert isinstance(envelope, ProducedStaticReturnEnvelope)
+ assert envelope.code.count("return {") == 1
+ assert '"status": None' in envelope.code
+ produced = workflow_update_module._code_block_produced_output_paths(envelope.code)
+ assert (covered | declared) <= produced
+ _, violations = workflow_update_module._extraction_code_with_required_static_return(
+ envelope.code, required_paths=covered, declaration_paths=declared
+ )
+ assert violations == []
+
+
+def test_producer_abstains_on_array_declaration_without_matching_table_group() -> None:
+ plan = _scalar_plan(_scalar_binding("output.account_number", "Account Number", ".acct"))
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths={"output.account_number"},
+ declaration_paths={"output.locations[].status"},
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert envelope is None
+
+
+def test_producer_abstains_on_nested_array_table_path() -> None:
+ plan = _scalar_plan(_table_binding("output.groups[].members[].name", 0))
+ envelope = produce_covered_static_return_envelope(
+ 'await page.locator("#lookup").click()\n',
+ plan=plan,
+ scalar_required_paths={"output.groups[].members[].name"},
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+ assert envelope is None
+
+
def test_produce_split_extraction_envelope_mixed_abstains(monkeypatch: pytest.MonkeyPatch) -> None:
ctx = SimpleNamespace(
reached_download_target=SimpleNamespace(selector="#download-invoice"),
diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.py
index 0901900fe..020c92f28 100644
--- a/tests/unit/test_copilot_code_block_synthesis.py
+++ b/tests/unit/test_copilot_code_block_synthesis.py
@@ -26,11 +26,13 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
_READONLY_DEFERRED_VAR,
_SYNTHESIZED_BLOCK_LABEL,
CREDENTIAL_FILL_TOOL_NAME,
+ ProducedStaticReturnEnvelope,
_get_by_role_expr,
_get_by_role_expr_strict,
build_synthesized_artifact_metadata,
code_contains_credential_fill,
is_optional_dismissal_only_trajectory,
+ produce_covered_static_return_envelope,
render_synthesized_offer_text,
synthesize_code_block,
synthesize_code_block_with_extraction,
@@ -233,6 +235,51 @@ async def test_generated_recipe_executes_and_fails_closed_on_runtime_drift() ->
await _execute_recipe(page)
+def _produce_table_envelope() -> ProducedStaticReturnEnvelope | None:
+ return produce_covered_static_return_envelope(
+ "x = 1",
+ plan=_extraction_plan(),
+ scalar_required_paths=set(_extraction_plan().requested_output_paths),
+ declaration_paths=set(),
+ download_required_paths=set(),
+ expects_download=False,
+ )
+
+
+async def _execute_envelope(page: _RecipePage, envelope: ProducedStaticReturnEnvelope | None) -> dict[str, object]:
+ assert envelope is not None
+ namespace: dict[str, object] = {}
+ exec("async def recipe(page):\n" + textwrap.indent(envelope.code, " "), namespace)
+ recipe = namespace["recipe"]
+ assert callable(recipe)
+ return await recipe(page)
+
+
+async def test_produced_envelope_executes_table_and_scalar_reads() -> None:
+ result = await _execute_envelope(_RecipePage(), _produce_table_envelope())
+ assert result["output"]["record_id"] == "record-123"
+ assert result["output"]["overall_state"] == "Ready"
+ assert len(result["output"]["records"]) == 3
+ assert result["output"]["records"][0]["detail"] == "Detail 0"
+ assert result["output"]["records"][2]["state"] == "Ready"
+
+
+async def test_produced_envelope_guard_raises_on_empty_cell() -> None:
+ page = _RecipePage()
+ page.text[("#records > tbody > tr|:scope > th, :scope > td", (0, 1))] = ""
+ with pytest.raises(ValueError, match="table cell value is empty"):
+ await _execute_envelope(page, _produce_table_envelope())
+
+
+def test_suffix_omits_empty_cell_guard() -> None:
+ suffix = synthesize_extraction_suffix(_extraction_plan())
+ assert suffix is not None
+ assert "table cell value is empty" not in suffix.code
+ assert "scalar value is empty" not in suffix.code
+
+
def test_plan_compiler_requires_exact_reveal_and_is_idempotent() -> None:
trajectory = [
_interaction(