Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/autoskillit/core/types/recipe/_type_recipe_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class ReadingToken:


BoundScalar: TypeAlias = str | int | bool
_TERMINAL_BYPASS_TARGETS = frozenset({"done", "escalate"})


RECIPE_TERMINAL_TARGETS: frozenset[str] = frozenset({"done", "escalate"})


Expand Down Expand Up @@ -291,6 +288,7 @@ class BoundValueOrigin(StrEnum):
RECIPE_INPUT = "recipe_input"
CONTEXT = "context"
TEMPLATE = "template"
DISPATCH_ITEM = "dispatch_item"
ABSENT = "absent"


Expand Down Expand Up @@ -379,6 +377,7 @@ class BindingFailureCode(StrEnum):
AMBIGUOUS_SKILL_INPUT = "ambiguous_skill_input"
INVALID_SKILL_INPUT_TYPE = "invalid_skill_input_type"
INVALID_SKILL_COMMAND = "invalid_skill_command"
INVALID_DISPATCH_ITEMS = "invalid_dispatch_items"


@dataclass(frozen=True, slots=True)
Expand Down Expand Up @@ -595,7 +594,7 @@ def _validate_projection_guards(
raise ValueError("FinalizedRecipeProjection guards must name finalized steps")
if any(
guard.bypass_target not in ordered_step_names
and guard.bypass_target not in _TERMINAL_BYPASS_TARGETS
and guard.bypass_target not in RECIPE_TERMINAL_TARGETS
for guard in guards
):
raise ValueError(
Expand Down
4 changes: 3 additions & 1 deletion src/autoskillit/recipe/_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
from autoskillit.recipe._binding_input import (
_AUTOSKILLIT_TEMPLATE_RE,
_apply_dispatch_fanout,
_bound_value,
_failure,
_inline_skill_inputs,
Expand Down Expand Up @@ -513,7 +514,7 @@ def bind_step_invocation(
tuple(failures),
)

return _bind_run_skill_invocation(
invocation = _bind_run_skill_invocation(
step_name,
step,
tool_name=tool_name,
Expand All @@ -526,6 +527,7 @@ def bind_step_invocation(
hidden_inputs=hidden_inputs,
ingredient_values=ingredient_values,
)
return _apply_dispatch_fanout(invocation, declared_with)


def bind_recipe(
Expand Down
132 changes: 131 additions & 1 deletion src/autoskillit/recipe/_binding_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@

from collections.abc import Mapping
from dataclasses import replace
from typing import Final, TypeGuard
from typing import Final, TypeGuard, cast

import regex as re

from autoskillit.core import (
BindingFailure,
BindingFailureCode,
BoundScalar,
BoundStepInvocation,
BoundValue,
BoundValueOrigin,
BoundValueState,
Expand All @@ -40,6 +41,9 @@
SkillContract,
SkillInput,
)
from autoskillit.recipe.contracts._contracts_types import (
DISPATCH_TOKEN_RE as _DISPATCH_TOKEN_RE,
)
from autoskillit.recipe.contracts._contracts_types import (
INPUT_REF_RE as _INPUT_REF_RE,
)
Expand Down Expand Up @@ -122,6 +126,132 @@ def _failure(
return BindingFailure(code=code, step_name=step_name, name=name, message=message)


def _dispatch_producer(value: object) -> tuple[str, str] | None:
if isinstance(value, str):
for namespace, pattern in (
("context", _EXACT_CONTEXT_REF_RE),
("inputs", _EXACT_INPUT_REF_RE),
):
if match := pattern.fullmatch(value.strip()):
return namespace, match.group(1)
return None


def _dispatch_item_slot(values: tuple[BoundValue, ...]) -> tuple[BoundValue, str] | None:
occurrences = [
(value, match.group(1))
for value in values
if value.is_present and isinstance(value.declared_value, str)
for match in _DISPATCH_TOKEN_RE.finditer(value.declared_value)
]
if len(occurrences) != 1:
return None
value, name = occurrences[0]
if not _DISPATCH_TOKEN_RE.fullmatch(cast(str, value.declared_value).strip()):
return None
return value, name


def _dispatch_occurrence_error(
invocation: BoundStepInvocation, name: str, *, inline: bool
) -> str | None:
for value in invocation.mcp_kwargs:
if not value.is_present or not isinstance(value.declared_value, str):
continue
count = sum(
match.group(1) == name for match in _DISPATCH_TOKEN_RE.finditer(value.declared_value)
)
expected = int(inline and value.name == "skill_command")
if count != expected:
return f"dispatch_items placeholder {name!r} in MCP argument {value.name!r} " + (
"must occur exactly once in the inline skill command"
if expected
else "must not occur outside the inline skill command"
)
return None


def _invalid_dispatch_items(invocation: BoundStepInvocation, message: str) -> BoundStepInvocation:
return replace(
invocation,
failures=invocation.failures
+ (
_failure(
BindingFailureCode.INVALID_DISPATCH_ITEMS,
invocation.step_name,
"dispatch_items",
message,
),
),
)


def _dispatch_dependency(
value: BoundValue, namespace: str, name: str, origin: BoundValueOrigin
) -> BoundValue:
context_dependencies = value.context_dependencies
input_dependencies = value.input_dependencies
if namespace == "context":
context_dependencies = tuple(dict.fromkeys((*context_dependencies, name)))
else:
input_dependencies = tuple(dict.fromkeys((*input_dependencies, name)))
return replace(
value,
origin=origin,
context_dependencies=context_dependencies,
input_dependencies=input_dependencies,
)


def _apply_dispatch_fanout(
invocation: BoundStepInvocation, declared_with: Mapping[str, object]
) -> BoundStepInvocation:
if "dispatch_items" not in declared_with:
return invocation
producer = _dispatch_producer(declared_with["dispatch_items"])
if producer is None:
return _invalid_dispatch_items(
invocation,
f"dispatch_items producer {declared_with['dispatch_items']!r} must be one exact "
"${{ context.<list> }} or ${{ inputs.<list> }} reference",
)
slot = _dispatch_item_slot(invocation.skill_inputs)
if slot is None:
names = ", ".join(value.name for value in invocation.skill_inputs)
return _invalid_dispatch_items(
invocation,
"dispatch_items requires exactly one whole-value {placeholder} occurrence "
f"in one skill input ({names}); missing, repeated or embedded item tokens are invalid",
)
candidate, name = slot
inline = declared_with.get("skill_inputs") is None
if error := _dispatch_occurrence_error(invocation, name, inline=inline):
return _invalid_dispatch_items(invocation, error)
namespace, producer_name = producer
return replace(
invocation,
skill_inputs=tuple(
_dispatch_dependency(value, namespace, producer_name, BoundValueOrigin.DISPATCH_ITEM)
if value is candidate
else value
for value in invocation.skill_inputs
),
mcp_kwargs=tuple(
_dispatch_dependency(
value,
namespace,
producer_name,
BoundValueOrigin.TEMPLATE
if value.origin is BoundValueOrigin.LITERAL
else value.origin,
)
if inline and value.name == "skill_command"
else value
for value in invocation.mcp_kwargs
),
)


# === Skill-input type validation ===


Expand Down
28 changes: 27 additions & 1 deletion src/autoskillit/recipe/contracts/_contracts_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@
INPUT_REF_RE = re.compile(r"\$\{\{\s*inputs\.([A-Za-z_]\w*)\s*\}\}")
_TEMPLATE_REF_RE = re.compile(r"\$\{\{[^}]+\}\}")
RESULT_CAPTURE_RE = re.compile(r"\$\{\{\s*result\.([\w-]+)\s*\}\}")
_PLACEHOLDER_NAME_PATTERN = r"(?!AUTOSKILLIT_)[A-Za-z_][\w.-]*"
DISPATCH_TOKEN_RE = re.compile(r"(?<![{$])\{\s*(" + _PLACEHOLDER_NAME_PATTERN + r")\s*\}(?!\})")
WHOLE_VALUE_PLACEHOLDER_RE = re.compile(r"\{\{?\s*" + _PLACEHOLDER_NAME_PATTERN + r"\s*\}\}?")


_TEXT_INPUT_TYPES = frozenset({"str", "string", "optional_string"})
_PATH_INPUT_TYPES = frozenset({"file_path", "file_path_list", "directory_path"})
_EXACT_REFERENCE_ORIGINS = frozenset({BoundValueOrigin.CONTEXT, BoundValueOrigin.RECIPE_INPUT})


def _is_unsubstituted_dispatch_item(value: BoundScalar, bound: BoundValue) -> bool:
return (
bound.origin is BoundValueOrigin.DISPATCH_ITEM
and isinstance(value, str)
and value.strip() == str(bound.declared_value).strip()
)


@dataclasses.dataclass(frozen=True, slots=True)
class SkillInput:
name: str
Expand Down Expand Up @@ -66,7 +77,9 @@ def accepts(self, value: object) -> bool:
return False

def admit_runtime_wire(self, value: BoundScalar, bound: BoundValue) -> BoundScalar | None:
"""Return the bound value, or None when the declared type rejects its wire form."""
"""Return the bound value, or None when its wire form violates the input contract."""
if _is_unsubstituted_dispatch_item(value, bound):
return None
if self.accepts(value):
return value
if bound.origin not in _EXACT_REFERENCE_ORIGINS:
Expand All @@ -86,6 +99,19 @@ def admit_runtime_wire(self, value: BoundScalar, bound: BoundValue) -> BoundScal
return None if text == bound.absence_value else text

def wire_mismatch_message(self, value: BoundScalar, bound: BoundValue) -> str:
if _is_unsubstituted_dispatch_item(value, bound):
producer = ", ".join(
f"{namespace}.{name}"
for namespace, dependencies in (
("context", bound.context_dependencies),
("inputs", bound.input_dependencies),
)
for name in dependencies
)
return (
f"per-item skill input {self.name!r} received its unsubstituted placeholder "
f"{value!r}; substitute one item from the step's dispatch_items {producer}"
)
message = (
f"runtime skill input {self.name!r} expects {self.type!r}; "
f"received {type(value).__name__}"
Expand Down
17 changes: 11 additions & 6 deletions src/autoskillit/recipe/rules/rules_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
get_skill_contract,
load_bundled_manifest,
)
from autoskillit.recipe.contracts._contracts_types import (
WHOLE_VALUE_PLACEHOLDER_RE as _WHOLE_VALUE_PLACEHOLDER_RE,
)
from autoskillit.recipe.io import iter_steps_with_context
from autoskillit.recipe.registry import RuleFinding, make_finding, semantic_rule
from autoskillit.recipe.schema import Recipe

logger = get_logger(__name__)


_WHOLE_VALUE_PLACEHOLDER_RE = re.compile(r"\{\{?\s*(?!AUTOSKILLIT_)[A-Za-z_]\w*\s*\}\}?")


def _unresolved_template_reason(bound: BoundValue, *, per_item: bool) -> str | None:
value = cast(str, bound.declared_value)
scan = value
Expand Down Expand Up @@ -71,13 +71,14 @@ def _check_unresolved_skill_input_template(ctx: ValidationContext) -> list[RuleF
invocation = ctx.binding_projection.for_step(step_name)
if invocation is None or invocation.skill_name is None:
continue
per_item = "dispatch_items" in (step.declared_with_args or {})
for bound in invocation.skill_inputs:
if bound.state is not BoundValueState.PRESENT or not isinstance(
bound.declared_value, str
):
continue
reason = _unresolved_template_reason(bound, per_item=per_item)
reason = _unresolved_template_reason(
bound, per_item=bound.origin is BoundValueOrigin.DISPATCH_ITEM
)
if reason is not None:
findings.append(
make_finding(
Expand All @@ -87,7 +88,10 @@ def _check_unresolved_skill_input_template(ctx: ValidationContext) -> list[RuleF
f"Skill input '{bound.name}' declares {bound.declared_value!r}: "
f"{reason}. "
"Bind ${{ context.<var> }} from a recipe-level producer step, "
"or ${{ inputs.<name> }}."
"or ${{ inputs.<name> }}. For a per-item fan-out, declare "
"dispatch_items: ${{ context.<list> }} or "
"${{ inputs.<list> }} and use exactly one "
"{placeholder}."
),
)
)
Expand Down Expand Up @@ -178,6 +182,7 @@ def _check_invalid_skill_invocation_binding(
BindingFailureCode.AMBIGUOUS_SKILL_INPUT,
BindingFailureCode.INVALID_SKILL_INPUT_TYPE,
BindingFailureCode.INVALID_SKILL_COMMAND,
BindingFailureCode.INVALID_DISPATCH_ITEMS,
}
for step_name, step in ctx.recipe.steps.items():
if step.tool not in SKILL_TOOLS:
Expand Down
2 changes: 2 additions & 0 deletions tests/contracts/test_tool_param_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def test_compiled_mcp_kwargs_are_exactly_the_kwarg_view() -> None:
)
for param in tool_def.params
}
with_args["dispatch_items"] = "${{ context.plan_paths }}"
with_args["skill_inputs"] = {"plan_path": "{plan_path}"}
step = RecipeStep(name="verify", tool="run_skill", with_args=with_args)

invocation = bind_step_invocation(
Expand Down
31 changes: 31 additions & 0 deletions tests/recipe/test_contracts_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from __future__ import annotations

from dataclasses import replace

import pytest

from autoskillit.core import BoundValue, BoundValueOrigin, BoundValueState
Expand Down Expand Up @@ -163,6 +165,35 @@ def test_skill_input_admits_only_declared_type_wire_values(
assert admitted == expected


@pytest.mark.parametrize(
("dependency_field", "producer"),
[
pytest.param("context_dependencies", "context.phase_ids", id="context-producer"),
pytest.param("input_dependencies", "inputs.phase_ids", id="input-producer"),
],
)
def test_dispatch_item_rejects_its_own_placeholder(dependency_field: str, producer: str) -> None:
skill_input = SkillInput(name="target_phase_id", type="str", required=True)
bound = replace(
_bound_value(
"target_phase_id",
BoundValueOrigin.LITERAL,
declared_value="{phase_id}",
),
origin=BoundValueOrigin.DISPATCH_ITEM,
**{dependency_field: ("phase_ids",)},
)

assert skill_input.admit_runtime_wire("{phase_id}", bound) is None
assert skill_input.admit_runtime_wire(" {phase_id} ", bound) is None
assert skill_input.admit_runtime_wire("P1", bound) == "P1"

message = skill_input.wire_mismatch_message("{phase_id}", bound)
assert "target_phase_id" in message
assert "unsubstituted placeholder" in message
assert producer in message


@pytest.mark.parametrize(
"wire",
[pytest.param(10**19, id="19-digits"), pytest.param(10**5000, id="5000-digits")],
Expand Down
Loading
Loading