diff --git a/docs/glossary.md b/docs/glossary.md index d009c353c5..5aa27f5d09 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -11,8 +11,24 @@ inventory to ordered hashed plan parts, their allocation rows, and aggregate coverage. `bind_plan_set` records hashed content evidence rather than command lines; aggregate coverage is decided only by the server-side `plan_set_coverage` preflight. Consumers receive verified preflight evidence, -never the artifact itself, and the sealed state is authenticated inside the -canonical authority digest. +never the artifact itself. The canonical authority digest is an unkeyed integrity +hash covering the sealed state; it does not establish server issuance. + +For recipe admission, the `PlanSetMaterializer` service held by `ToolContext` +retains each successful bind's root and issued digest for that execution and +authority path. The default implementation, `DefaultPlanSetMaterializer`, stores +these records in memory. Admission +verifies the original path beneath its recorded bind root and compares the +verified digest with that issuance record. Audit inputs retain their invocation +cwd root; standalone binding retains cwd containment and creates no recipe record. + +Records live in the server process, survive same-context execution reinstalls, +and are retained for its lifetime, including obsolete executions. Fresh contexts, +including fleet-side contexts, require their own successful recipe bind. +This boundary trusts the server process, its records, and the supplied root and +its ancestors. The recorded root is a path, without a retained directory handle +or bind-time inode identity; replacing it or an ancestor can change later path +resolution. A sibling worktree does not provide OS-level isolation. ### arch-lens diff --git a/src/autoskillit/core/planset/AGENTS.md b/src/autoskillit/core/planset/AGENTS.md index db6b149b95..af6d8fd2bc 100644 --- a/src/autoskillit/core/planset/AGENTS.md +++ b/src/autoskillit/core/planset/AGENTS.md @@ -1 +1,5 @@ -The IL-0 plan-set domain owns deterministic regex-only requirement extraction, allocation parsing, aggregate coverage, and verification; the server owns fetching and persistence. `plan_set_coverage` is an in-process server-side aggregate inspection, not a decision made by a child skill. Unsupported Markdown is recorded as a fail-closed marker, `text_digest` covers exact raw item spans rather than normalized text, and skills never open authority artifacts. Part commands are evidence only: the authenticated authority carries the hashed parts and coverage result. Its `state` is included in the canonical payload authenticated by `authority_digest`; admission requires both a matching recomputed digest and `state == sealed`. +The IL-0 plan-set domain owns deterministic regex-only requirement extraction, allocation parsing, aggregate coverage, and verification; the server owns fetching and persistence. `plan_set_coverage` is an in-process server-side aggregate inspection, not a decision made by a child skill. Unsupported Markdown is recorded as a fail-closed marker, `text_digest` covers exact raw item spans rather than normalized text, and skills never open authority artifacts. Part commands are evidence only: the authority carries the hashed parts and coverage result. Its `state` is included in the canonical payload covered by the unkeyed integrity hash `authority_digest`; admission requires both a matching recomputed digest and `state == sealed`. + +Recipe admission also requires server-held issuance provenance: the `PlanSetMaterializer` service held by `ToolContext` records each successful recipe bind's resolved root and digest by execution ID and authority path. Its default implementation is `DefaultPlanSetMaterializer`. Admission verifies the original caller path under that recorded root, then compares the verified digest with the issued digest. Standalone binds keep cwd-rooted containment and create no recipe record; audit inputs retain their invocation-cwd root. + +Records remain in memory for the server process lifetime, including obsolete executions. Reinstalling an execution on the same context preserves them; fresh contexts, including fleet-side contexts, need their own successful recipe bind. This trusts the server process, its records, and the supplied root and its ancestors. The root is a path rather than a retained directory descriptor or bind-time inode identity, so replacing the root or its ancestors can change later resolution. The digest pin does not protect directory identity, and a sibling worktree does not establish OS-level isolation. diff --git a/src/autoskillit/core/types/protocols/_type_protocols_infra.py b/src/autoskillit/core/types/protocols/_type_protocols_infra.py index b46768aa26..77cb9db396 100644 --- a/src/autoskillit/core/types/protocols/_type_protocols_infra.py +++ b/src/autoskillit/core/types/protocols/_type_protocols_infra.py @@ -236,10 +236,14 @@ def __call__(self, project_dir: Path) -> frozenset[str]: ... @runtime_checkable class PlanSetMaterializer(Protocol): - """Server-owned binder for a content-addressed plan-set authority.""" + """Context-held plan-set binding and successful recipe-bind provenance service.""" async def bind(self, request: PlanSetBindRequest) -> PlanSetBindResult: ... + def lookup_binding( + self, execution_generation: str, authority_path: str + ) -> tuple[Path, str] | None: ... + @dataclass(frozen=True, slots=True) class QuotaPolicy: diff --git a/src/autoskillit/pipeline/context.py b/src/autoskillit/pipeline/context.py index 2c8ee835a2..fde463827f 100644 --- a/src/autoskillit/pipeline/context.py +++ b/src/autoskillit/pipeline/context.py @@ -229,7 +229,8 @@ class ToolContext: into server-owned canonical audit authority artifacts. plan_set_materializer: PlanSetMaterializer — binds ordered plan parts to server-owned - plan-set authority artifacts. + plan-set authority artifacts and retains successful recipe-bind + roots and digests for server-held provenance lookup. committed_disposition_resolver: CommittedDispositionResolver — resolves only disposition paths backed by the durable audit-admission ledger. diff --git a/src/autoskillit/server/_plan_set_materializer.py b/src/autoskillit/server/_plan_set_materializer.py index 4abc8fd6f5..c376dcde6d 100644 --- a/src/autoskillit/server/_plan_set_materializer.py +++ b/src/autoskillit/server/_plan_set_materializer.py @@ -2,6 +2,7 @@ from __future__ import annotations +import os from collections.abc import Sequence from datetime import UTC, datetime from pathlib import Path @@ -21,6 +22,7 @@ IssueSnapshotRef, PlanPartRef, PlanSetAuthority, + PlanSetBindingMode, PlanSetBindRequest, PlanSetBindResult, PlanSetRejectReason, @@ -169,6 +171,13 @@ class DefaultPlanSetMaterializer: def __init__(self, github_client: GitHubFetcher | None) -> None: self._github_client = github_client + self._bindings: dict[tuple[str, str], tuple[Path, str]] = {} + + def lookup_binding( + self, execution_generation: str, authority_path: str + ) -> tuple[Path, str] | None: + """Return successful recipe-bind provenance; standalone binds have no record.""" + return self._bindings.get((execution_generation, os.path.abspath(authority_path))) async def bind(self, request: PlanSetBindRequest) -> PlanSetBindResult: predecessor: PlanSetAuthority | None = None @@ -224,7 +233,7 @@ async def bind(self, request: PlanSetBindRequest) -> PlanSetBindResult: parent_path = str(Path(request.parent_authority_path).resolve()) if predecessor is parent: predecessor_path = parent_path - return await self._bind( + result = await self._bind( request, root, parent, @@ -233,6 +242,13 @@ async def bind(self, request: PlanSetBindRequest) -> PlanSetBindResult: predecessor_path, predecessor_digest, ) + if result.success and request.binding_mode is PlanSetBindingMode.RECIPE: + key = ( + request.execution_generation, + os.path.abspath(result.plan_set_authority_path), + ) + self._bindings[key] = (root, result.plan_set_authority_digest) + return result except Exception as exc: logger.error("plan-set binding failed", exc_info=True) return _failure( diff --git a/src/autoskillit/server/_recipe_admission.py b/src/autoskillit/server/_recipe_admission.py new file mode 100644 index 0000000000..c89cbd1134 --- /dev/null +++ b/src/autoskillit/server/_recipe_admission.py @@ -0,0 +1,9 @@ +"""Recipe execution admission failures shared by server recipe and tool code.""" + + +class RecipeExecutionAdmissionError(RuntimeError): + """Stable pre-launch attested-execution rejection.""" + + def __init__(self, code: str, message: str) -> None: + super().__init__(message) + self.code = code diff --git a/src/autoskillit/server/recipe/_plan_set_preflight.py b/src/autoskillit/server/recipe/_plan_set_preflight.py index 96893f4e3f..4f90f248e3 100644 --- a/src/autoskillit/server/recipe/_plan_set_preflight.py +++ b/src/autoskillit/server/recipe/_plan_set_preflight.py @@ -2,15 +2,95 @@ from __future__ import annotations +from collections.abc import Mapping from pathlib import Path +from typing import TYPE_CHECKING from autoskillit.core import ( + BoundScalar, + InstalledRecipeExecution, + InvocationTemplate, PlanSetBindingMode, + PlanSetPreflightEvidence, PlanSetPreflightRequest, + PlanSetRejectReason, PlanSetVerification, + PreflightKind, RecipeExecutionId, verify_plan_set_authority, ) +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError + +if TYPE_CHECKING: + from autoskillit.pipeline import ToolContext + + +def _resolve_plan_set_preflight( + tool_ctx: ToolContext, + installed: InstalledRecipeExecution, + bound_input_map: Mapping[str, BoundScalar], + execution_id: str, + step_name: str, + template: InvocationTemplate, + kind: PreflightKind, +) -> PlanSetPreflightEvidence | None: + """Use recipe_execution_preflight_* for input or service failures. + + Authority and verifier rejections use input_preflight_plan_set_* codes. + """ + authority_path = bound_input_map.get("plan_set_authority_path") + if authority_path in (None, ""): + return None + stage_scope = kind is PreflightKind.PLAN_SET_STAGE_OBLIGATIONS + plan_path = None if stage_scope else bound_input_map.get("plan_path") + if not isinstance(authority_path, str) or (not stage_scope and not isinstance(plan_path, str)): + raise RecipeExecutionAdmissionError( + "recipe_execution_preflight_input", + "plan-set preflight requires bound string plan_path and authority path", + ) + materializer = tool_ctx.plan_set_materializer + if materializer is None or installed.plan_set_preflight_resolver is None: + raise RecipeExecutionAdmissionError( + "recipe_execution_preflight_unavailable", + "plan-set materializer or preflight resolver is unavailable", + ) + binding = materializer.lookup_binding(execution_id, authority_path) + if binding is None: + raise RecipeExecutionAdmissionError( + f"input_preflight_plan_set_{PlanSetRejectReason.AUTHORITY_INVALID.value}", + "authority was not bound by this server for the current recipe execution", + ) + bind_root, issued_digest = binding + verification = installed.plan_set_preflight_resolver.resolve( + PlanSetPreflightRequest( + execution_generation=execution_id, + expected_kitchen_id=tool_ctx.kitchen_id, + step_name=step_name, + skill_name=template.invocation.skill_name or "", + plan_path=plan_path if isinstance(plan_path, str) else None, + plan_set_authority_path=authority_path, + allow_part_drift=stage_scope, + ), + allowed_root=bind_root, + ) + if not verification.accepted: + reason = verification.reason.value if verification.reason else "rejected" + raise RecipeExecutionAdmissionError( + f"input_preflight_plan_set_{reason}", + "; ".join(verification.detail), + ) + evidence = verification.evidence + if evidence is None: + raise RecipeExecutionAdmissionError( + "recipe_execution_preflight_unavailable", + "accepted plan-set verification did not provide evidence", + ) + if evidence.plan_set_authority_digest != issued_digest: + raise RecipeExecutionAdmissionError( + "input_preflight_plan_set_authority_digest", + "verified authority digest differs from its server-issued binding", + ) + return evidence class DefaultPlanSetPreflightResolver: diff --git a/src/autoskillit/server/recipe/_recipe_execution.py b/src/autoskillit/server/recipe/_recipe_execution.py index 6bc716d1ff..017b287a2d 100644 --- a/src/autoskillit/server/recipe/_recipe_execution.py +++ b/src/autoskillit/server/recipe/_recipe_execution.py @@ -23,7 +23,6 @@ InventoryAdmissionDecision, InvocationTemplate, PlanSetPreflightEvidence, - PlanSetPreflightRequest, PreflightEvidence, PreflightKind, RecipeBindingProjection, @@ -55,6 +54,10 @@ compute_skill_contract_identity, ) from autoskillit.server._misc import clear_run_skill_state +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError +from autoskillit.server.recipe._plan_set_preflight import ( + _resolve_plan_set_preflight, +) if TYPE_CHECKING: from autoskillit.core import ( @@ -69,7 +72,6 @@ __all__ = [ "DefaultInputPreflightResolver", - "RecipeExecutionAdmissionError", "bind_attested_runtime_invocation", "build_bound_child_prompt", "build_recipe_execution_snapshot", @@ -86,14 +88,6 @@ ] -class RecipeExecutionAdmissionError(RuntimeError): - """Stable pre-launch attested-execution rejection.""" - - def __init__(self, code: str, message: str) -> None: - super().__init__(message) - self.code = code - - class DefaultInputPreflightResolver: """Verify audit-cycle input provenance before any child construction.""" @@ -591,53 +585,6 @@ def _resolve_audit_preflight( return audit -def _resolve_plan_set_preflight( - tool_ctx: ToolContext, - installed: InstalledRecipeExecution, - bound_input_map: Mapping[str, BoundScalar], - execution_id: str, - step_name: str, - template: InvocationTemplate, - allowed_root: Path | None, - kind: PreflightKind, -) -> PlanSetPreflightEvidence | None: - authority_path = bound_input_map.get("plan_set_authority_path") - if authority_path in (None, ""): - return None - stage_scope = kind is PreflightKind.PLAN_SET_STAGE_OBLIGATIONS - plan_path = None if stage_scope else bound_input_map.get("plan_path") - if not isinstance(authority_path, str) or (not stage_scope and not isinstance(plan_path, str)): - raise _build_preflight_input_error( - "plan-set preflight requires bound string plan_path and authority path", - ) - if allowed_root is None: - raise _build_preflight_input_error("plan-set preflight requires an allowed root") - if installed.plan_set_preflight_resolver is None: - raise RecipeExecutionAdmissionError( - "recipe_execution_preflight_unavailable", - "plan-set preflight resolver is unavailable", - ) - verification = installed.plan_set_preflight_resolver.resolve( - PlanSetPreflightRequest( - execution_generation=execution_id, - expected_kitchen_id=tool_ctx.kitchen_id, - step_name=step_name, - skill_name=template.invocation.skill_name or "", - plan_path=plan_path if isinstance(plan_path, str) else None, - plan_set_authority_path=authority_path, - allow_part_drift=stage_scope, - ), - allowed_root=allowed_root, - ) - if not verification.accepted: - reason = verification.reason.value if verification.reason else "rejected" - raise RecipeExecutionAdmissionError( - f"input_preflight_plan_set_{reason}", - "; ".join(verification.detail), - ) - return verification.evidence - - def resolve_attested_input_preflight( tool_ctx: ToolContext, installed: InstalledRecipeExecution, @@ -674,7 +621,6 @@ def resolve_attested_input_preflight( execution_id, step_name, template, - allowed_root, PreflightKind(preflight_name), ) else: diff --git a/src/autoskillit/server/recipe/_recipe_initialization.py b/src/autoskillit/server/recipe/_recipe_initialization.py index 53c1ca7b29..39fec4cfb5 100644 --- a/src/autoskillit/server/recipe/_recipe_initialization.py +++ b/src/autoskillit/server/recipe/_recipe_initialization.py @@ -33,9 +33,9 @@ record_initialization_page, start_recipe_initialization, ) +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.lifecycle._state import _get_ctx_or_none from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, install_recipe_execution, prepare_recipe_execution, ready_recipe_execution_state, diff --git a/src/autoskillit/server/tools/tools_execution/_run_skill_admission.py b/src/autoskillit/server/tools/tools_execution/_run_skill_admission.py index 9073bb4e9a..9f97fd5f89 100644 --- a/src/autoskillit/server/tools/tools_execution/_run_skill_admission.py +++ b/src/autoskillit/server/tools/tools_execution/_run_skill_admission.py @@ -37,10 +37,10 @@ load_current_prior_authority, normalize_audited_plan_refs, ) +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server._tracker_authority import _restore_reserved_tracker_authority from autoskillit.server.lifecycle._guards import _check_dry_walkthrough_plan from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, bind_attested_runtime_invocation, build_bound_child_prompt, build_standalone_child_invocation, @@ -226,7 +226,7 @@ def _prepare_audit_reservation( ) -> None: """Prepare and reserve the audit slot for an attested publication.""" assert state._audit_publication is not None - assert state._clone_allowed_root is not None + assert state._invocation_temp_root is not None assert state._installed_execution is not None state._slot_intent_digest = compute_audit_slot_intent_digest( execution_id=state.recipe_execution_id, @@ -245,14 +245,14 @@ def _prepare_audit_reservation( if isinstance(prior_path, str) and prior_path: prior_authority = load_current_prior_authority( prior_path, - allowed_root=state._clone_allowed_root, + allowed_root=state._invocation_temp_root, ledger=state.tool_ctx.audit_admission_ledger, recipe_execution_id=state._recipe_execution_key, ) state._audited_plan_refs = ( normalize_audited_plan_refs( str(state._bound_input_map.get("all_plan_paths") or ""), - allowed_root=state._clone_allowed_root, + allowed_root=state._invocation_temp_root, ) if state._bound_input_map.get("all_plan_paths") else prior_authority.audited_plan_refs @@ -264,7 +264,7 @@ def _prepare_audit_reservation( else: state._audited_plan_refs = normalize_audited_plan_refs( str(state._bound_input_map.get("all_plan_paths") or ""), - allowed_root=state._clone_allowed_root, + allowed_root=state._invocation_temp_root, ) state._cycle_id, state._scope_id, state._part_id = derive_initial_lifecycle_ids( recipe_execution_id=state._recipe_execution_key, @@ -294,7 +294,7 @@ def _prepare_audit_reservation( cycle_id=state._cycle_id, scope_id=state._scope_id, part_id=state._part_id, - allowed_root=state._clone_allowed_root, + allowed_root=state._invocation_temp_root, parent_authority_digest=state._parent_digest, retry_after_audit_attempt_id=( AuditAttemptId(state.retry_after_audit_attempt_id) @@ -328,7 +328,7 @@ def _handle_audit_reservation_decision(state: _RunSkillDispatchState) -> str | N """Construct the dispatch prompt or return an audit reservation terminal result.""" assert state._reservation_outcome is not None assert state._audited_plan_refs is not None - assert state._clone_allowed_root is not None + assert state._invocation_temp_root is not None match state._reservation_outcome.decision: case ReservationDecision.DISPATCH_NEW | ReservationDecision.REDISPATCH_OPEN: assert state._reservation_outcome.reservation is not None @@ -402,7 +402,7 @@ def _handle_audit_reservation_decision(state: _RunSkillDispatchState) -> str | N ) case ReservationDecision.PUBLISHED_PENDING_FINALIZATION: assert state._reservation_outcome.reservation is not None - state._authority = AuditCycleVerifier(state._clone_allowed_root).load_authority( + state._authority = AuditCycleVerifier(state._invocation_temp_root).load_authority( state._reservation_outcome.reservation.authority_path ) state._published = AuditMaterializationResult( @@ -479,7 +479,7 @@ def _admit_recipe_execution(state: _RunSkillDispatchState) -> str | None: if state._target_contract is None: raise SkillContractError("audit output contract is unavailable") select_audit_output_contract(state._target_contract, state._audit_output_mode) - # Resolve the clone-local containment anchor before every publication branch (#4387). + # Resolve the invocation-cwd temp root for audit inputs and publication (#4387). # Only active recipe executions consume it, so only they require a non-empty cwd. if state._installed_execution is not None and not state.cwd: return json.dumps( @@ -489,7 +489,7 @@ def _admit_recipe_execution(state: _RunSkillDispatchState) -> str | None: retriable=False, ) ) - state._clone_allowed_root = resolve_temp_dir( + state._invocation_temp_root = resolve_temp_dir( Path(state.cwd), state.tool_ctx.config.workspace.temp_dir ) if state._dynamic_recipe_call: @@ -598,7 +598,7 @@ def _admit_attested_recipe_invocation(state: _RunSkillDispatchState) -> str | No step_name=state.step_name, template=state._invocation_template, bound_inputs=state._bound_recipe_inputs, - allowed_root=state._clone_allowed_root, + allowed_root=state._invocation_temp_root, ) state._preflight_result = resolved_preflights.audit state._plan_set_preflight = resolved_preflights.plan_set diff --git a/src/autoskillit/server/tools/tools_execution/_state.py b/src/autoskillit/server/tools/tools_execution/_state.py index 02db0c2d7c..2bb9b66903 100644 --- a/src/autoskillit/server/tools/tools_execution/_state.py +++ b/src/autoskillit/server/tools/tools_execution/_state.py @@ -190,7 +190,8 @@ class _RunSkillDispatchState: _claims_recipe_execution: bool = False _dynamic_recipe_call: bool = False _audit_output_mode: AuditOutputMode | None = None - _clone_allowed_root: Path | None = None + # Invocation-cwd temp root used as the audit containment boundary. + _invocation_temp_root: Path | None = None _slot_intent_digest: str | None = None _bound_input_map: dict[str, BoundScalar] | None = None _prior_input_field: str | None = None diff --git a/tests/_test_filter.py b/tests/_test_filter.py index e14485a094..aba4f83fbf 100644 --- a/tests/_test_filter.py +++ b/tests/_test_filter.py @@ -1352,6 +1352,7 @@ class SourceMapRejection(enum.StrEnum): "hooks/test_recipe_contract_freshness.py", "migration", # Server file-level entries importing autoskillit.recipe: + "server/test_plan_set_preflight_implement.py", "server/test_stage_obligation_pipeline.py", "server/test_tools_plan_set.py", "server/test_run_skill_delegated_output_write.py", diff --git a/tests/recipe/test_bundled_recipes_pipeline_structure.py b/tests/recipe/test_bundled_recipes_pipeline_structure.py index 28d1c00adb..05c57c67a6 100644 --- a/tests/recipe/test_bundled_recipes_pipeline_structure.py +++ b/tests/recipe/test_bundled_recipes_pipeline_structure.py @@ -1307,10 +1307,23 @@ def test_create_impl_worktree_precedes_implement(self, recipe) -> None: def test_implement_uses_worktree_path_as_cwd(self, recipe) -> None: impl_step = recipe.steps["implement"] cwd = impl_step.with_args.get("cwd", "") - assert "context.worktree_path" in cwd, ( + assert cwd == "${{ context.worktree_path }}", ( f"implement step cwd must reference context.worktree_path, got {cwd!r}" ) + def test_implement_retains_clone_plan_set_bindings(self, recipe) -> None: + skill_inputs = recipe.steps["implement"].with_args["skill_inputs"] + assert skill_inputs["plan_path"] == "${{ context.plan_path }}" + assert skill_inputs["plan_set_authority_path"] == "${{ context.plan_set_authority_path }}" + + def test_retry_uses_worktree_and_retained_plan_set_bindings(self, recipe) -> None: + retry = recipe.steps["retry_worktree"] + assert retry.with_args["cwd"] == "${{ context.worktree_path }}" + skill_inputs = retry.with_args["skill_inputs"] + assert skill_inputs["plan_path"] == "${{ context.plan_path }}" + assert skill_inputs["plan_set_authority_path"] == "${{ context.plan_set_authority_path }}" + assert skill_inputs["worktree_path"] == "${{ context.worktree_path }}" + # --------------------------------------------------------------------------- # Recipe cmd quoting — bundled recipes must not have shell expansion diff --git a/tests/server/test_attestation_delivery_reachability.py b/tests/server/test_attestation_delivery_reachability.py index f05ded015d..c19f8f3c17 100644 --- a/tests/server/test_attestation_delivery_reachability.py +++ b/tests/server/test_attestation_delivery_reachability.py @@ -26,6 +26,7 @@ ) from autoskillit.execution import CODEX_RECIPE_DELIVERY_BUDGET from autoskillit.pipeline import ReadyRecipe +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._recipe_delivery import ( load_recipe_artifact, persist_recipe_artifact, @@ -33,7 +34,6 @@ ) from autoskillit.server.recipe._recipe_delivery_helpers import _attested_render from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, bind_attested_runtime_invocation, get_recipe_execution, install_recipe_execution, diff --git a/tests/server/test_attested_admission_replay.py b/tests/server/test_attested_admission_replay.py index e21fb65bbc..447db29960 100644 --- a/tests/server/test_attested_admission_replay.py +++ b/tests/server/test_attested_admission_replay.py @@ -22,8 +22,8 @@ from autoskillit.core.io import load_yaml from autoskillit.recipe import load_and_validate from autoskillit.recipe.schema import RecipeKind +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, bind_attested_runtime_invocation, build_recipe_execution_snapshot, ) diff --git a/tests/server/test_kitchen_lifecycle.py b/tests/server/test_kitchen_lifecycle.py index 5e0b93b209..a13fb93d09 100644 --- a/tests/server/test_kitchen_lifecycle.py +++ b/tests/server/test_kitchen_lifecycle.py @@ -355,8 +355,8 @@ async def test_same_campaign_reopen_issues_fresh_recipe_authority( write_initial_state, ) from autoskillit.pipeline import ReadyRecipe + from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, bind_attested_runtime_invocation, ) from autoskillit.server.tools.tools_kitchen import open_kitchen diff --git a/tests/server/test_plan_set_materializer.py b/tests/server/test_plan_set_materializer.py index 53eb3dc43f..9e1d2ef1e1 100644 --- a/tests/server/test_plan_set_materializer.py +++ b/tests/server/test_plan_set_materializer.py @@ -4,6 +4,7 @@ import json from dataclasses import replace +from datetime import UTC, datetime from pathlib import Path import pytest @@ -27,6 +28,12 @@ _GitHub = PlanSetGitHub +class _FrozenDatetime: + @staticmethod + def now(_zone: object) -> datetime: + return datetime(2026, 10, 25, tzinfo=UTC) + + def _plan_part( allocation_rows: str, *, @@ -105,6 +112,53 @@ async def test_sealed_binding_writes_content_addressed_authority(tmp_path: Path) assert Path(result.plan_set_authority_path).is_file() +@pytest.mark.parametrize("case", ("new", "existing", "unchanged_parent")) +@pytest.mark.asyncio +async def test_successful_recipe_bind_records_lookup_binding( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, case: str +) -> None: + monkeypatch.setattr(materializer_module, "datetime", _FrozenDatetime) + fixture = _FIXTURES / "split_r1_r2" + parts = tuple(tmp_path / name for name in ("part_a.md", "part_b.md")) + for part in parts: + part.write_text((fixture / part.name).read_text(), encoding="utf-8") + request = PlanSetBindRequest( + plan_parts_raw="\n".join(str(part) for part in parts), + allowed_root=tmp_path, + issue_url="owner/repo#1", + parent_authority_path="", + seal=True, + step_name="bind", + execution_generation="raw-execution-id", + kitchen_id="kitchen", + dispatch_id="", + binding_mode=PlanSetBindingMode.RECIPE, + ) + initial = None + if case != "new": + initial = await DefaultPlanSetMaterializer(_GitHub()).bind(request) + assert initial.success + if case == "unchanged_parent": + request = replace(request, parent_authority_path=initial.plan_set_authority_path) + + materializer = DefaultPlanSetMaterializer(_GitHub()) + lookup_path = initial.plan_set_authority_path if initial is not None else "missing.json" + assert materializer.lookup_binding("raw-execution-id", lookup_path) is None + + result = await materializer.bind(request) + + assert result.success + if case == "unchanged_parent": + assert initial is not None + assert result.plan_set_authority_path == initial.plan_set_authority_path + assert result.plan_set_authority_digest == initial.plan_set_authority_digest + authority = Path(result.plan_set_authority_path) + dotted_path = f"{authority.parent}/./{authority.name}" + expected = (tmp_path.resolve(), result.plan_set_authority_digest) + assert materializer.lookup_binding("raw-execution-id", dotted_path) == expected + assert materializer.lookup_binding("other-execution-id", dotted_path) is None + + def _coverage_failure_case( case: str, ) -> tuple[tuple[str, ...], str, tuple[str, ...], tuple[str, ...], tuple[str, ...]]: @@ -271,6 +325,10 @@ async def test_failed_seal_preserves_open_parent_for_gap_replan(tmp_path: Path) materializer = DefaultPlanSetMaterializer(_GitHub()) parent = await materializer.bind(request) assert parent.success + parent_binding = materializer.lookup_binding( + request.execution_generation, parent.plan_set_authority_path + ) + assert parent_binding == (tmp_path.resolve(), parent.plan_set_authority_digest) before = set((tmp_path / "plan-set-authority").rglob("*.json")) failed = await materializer.bind( @@ -291,6 +349,10 @@ async def test_failed_seal_preserves_open_parent_for_gap_replan(tmp_path: Path) assert "R2 [unassigned]" in failed.coverage_gaps assert failed.plan_set_authority_path == parent.plan_set_authority_path assert failed.plan_set_authority_digest == parent.plan_set_authority_digest + assert ( + materializer.lookup_binding(request.execution_generation, parent.plan_set_authority_path) + == parent_binding + ) assert set((tmp_path / "plan-set-authority").rglob("*.json")) == before @@ -606,6 +668,10 @@ async def test_renewal_rebinds_walkthrough_edits_before_implementation(tmp_path: materializer = DefaultPlanSetMaterializer(_GitHub()) initial = await materializer.bind(request) assert initial.success + assert materializer.lookup_binding("generation", initial.plan_set_authority_path) == ( + tmp_path.resolve(), + initial.plan_set_authority_digest, + ) parts[0].write_text( parts[0] .read_text() @@ -645,6 +711,14 @@ def verify(path: str): ) assert renewed.success assert renewed.plan_set_authority_digest != initial.plan_set_authority_digest + assert materializer.lookup_binding("generation", initial.plan_set_authority_path) == ( + tmp_path.resolve(), + initial.plan_set_authority_digest, + ) + assert materializer.lookup_binding("generation", renewed.plan_set_authority_path) == ( + tmp_path.resolve(), + renewed.plan_set_authority_digest, + ) admitted = verify(renewed.plan_set_authority_path) assert admitted.accepted assert admitted.authority is not None and admitted.authority.revision == 2 @@ -685,8 +759,13 @@ async def test_verifier_rejects_foreign_kitchen_and_standalone_binding(tmp_path: dispatch_id="", binding_mode=PlanSetBindingMode.STANDALONE, ) - bound = await DefaultPlanSetMaterializer(_GitHub()).bind(request) + materializer = DefaultPlanSetMaterializer(_GitHub()) + bound = await materializer.bind(request) assert bound.success + assert ( + materializer.lookup_binding(request.execution_generation, bound.plan_set_authority_path) + is None + ) foreign = verify_plan_set_authority( bound.plan_set_authority_path, allowed_root=tmp_path, diff --git a/tests/server/test_plan_set_preflight.py b/tests/server/test_plan_set_preflight.py index 00723e5267..32d1408a9b 100644 --- a/tests/server/test_plan_set_preflight.py +++ b/tests/server/test_plan_set_preflight.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import os from pathlib import Path from types import SimpleNamespace @@ -17,9 +18,9 @@ RecipeExecutionId, ) from autoskillit.server._plan_set_materializer import DefaultPlanSetMaterializer +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._plan_set_preflight import DefaultPlanSetPreflightResolver from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, build_bound_child_prompt, resolve_attested_input_preflight, ) @@ -28,12 +29,15 @@ pytestmark = [pytest.mark.layer("server"), pytest.mark.anyio, pytest.mark.medium] -async def _authority(tmp_path: Path, *, sealed: bool = True) -> tuple[str, Path]: +async def _authority( + tmp_path: Path, *, sealed: bool = True +) -> tuple[str, Path, DefaultPlanSetMaterializer]: fixture = _FIXTURES / "split_r1_r2" parts = (tmp_path / "part_a.md", tmp_path / "part_b.md") for part in parts: part.write_text((fixture / part.name).read_text(), encoding="utf-8") - bound = await DefaultPlanSetMaterializer(_GitHub()).bind( + materializer = DefaultPlanSetMaterializer(_GitHub()) + bound = await materializer.bind( PlanSetBindRequest( plan_parts_raw="\n".join(str(part) for part in parts), allowed_root=tmp_path, @@ -48,12 +52,12 @@ async def _authority(tmp_path: Path, *, sealed: bool = True) -> tuple[str, Path] ) ) assert bound.success - return bound.plan_set_authority_path, parts[0] + return bound.plan_set_authority_path, parts[0], materializer async def test_sealed_resolver_and_prompt_emit_assigned_subset(tmp_path: Path) -> None: assert PreflightKind("plan_set_coverage") is PreflightKind.PLAN_SET_COVERAGE - path, part = await _authority(tmp_path) + path, part, _ = await _authority(tmp_path) resolver = DefaultPlanSetPreflightResolver(RecipeExecutionId("generation"), "kitchen") verified = resolver.resolve( PlanSetPreflightRequest( @@ -80,7 +84,7 @@ async def test_sealed_resolver_and_prompt_emit_assigned_subset(tmp_path: Path) - async def test_unsealed_authority_is_rejected(tmp_path: Path) -> None: - path, part = await _authority(tmp_path, sealed=False) + path, part, _ = await _authority(tmp_path, sealed=False) result = DefaultPlanSetPreflightResolver(RecipeExecutionId("generation"), "kitchen").resolve( PlanSetPreflightRequest( execution_generation="generation", @@ -98,10 +102,11 @@ async def test_unsealed_authority_is_rejected(tmp_path: Path) -> None: async def test_absent_authority_omits_evidence_and_kitchen_error_is_namespaced( tmp_path: Path, ) -> None: - path, part = await _authority(tmp_path) + path, part, materializer = await _authority(tmp_path) resolver = DefaultPlanSetPreflightResolver(RecipeExecutionId("generation"), "kitchen") tool_ctx = SimpleNamespace( kitchen_id="other-kitchen", + plan_set_materializer=materializer, skill_contract_resolver=lambda _command: SimpleNamespace( input_preflight=("plan_set_coverage",) ), @@ -134,13 +139,120 @@ async def test_absent_authority_omits_evidence_and_kitchen_error_is_namespaced( assert exc_info.value.code == "input_preflight_plan_set_kitchen_mismatch" +def _plan_preflight(materializer, resolver, bound_inputs): + return resolve_attested_input_preflight( + SimpleNamespace( + kitchen_id="kitchen", + plan_set_materializer=materializer, + skill_contract_resolver=lambda _: SimpleNamespace( + input_preflight=("plan_set_coverage",) + ), + ), + SimpleNamespace(plan_set_preflight_resolver=resolver), + skill_command="/autoskillit:dry-walkthrough", + execution_id="generation", + step_name="verify", + template=SimpleNamespace(invocation=SimpleNamespace(skill_name="dry-walkthrough")), + bound_inputs=bound_inputs, + ) + + +@pytest.mark.parametrize("kind", ("symlink", "hardlink")) +async def test_recorded_authority_still_rejects_file_links(tmp_path: Path, kind: str) -> None: + authority_path, part, materializer = await _authority(tmp_path) + path = Path(authority_path) + original = path.rename(path.with_suffix(".original")) + if kind == "symlink": + path.symlink_to(original) + else: + os.link(original, path) + with pytest.raises(RecipeExecutionAdmissionError) as exc_info: + _plan_preflight( + materializer, + DefaultPlanSetPreflightResolver(RecipeExecutionId("generation"), "kitchen"), + bound_inputs=(("plan_path", str(part)), ("plan_set_authority_path", authority_path)), + ) + assert exc_info.value.code == f"input_preflight_plan_set_{kind}" + + +def test_plan_only_preflight_uses_issued_root_without_invocation_root(tmp_path: Path) -> None: + issued_root = tmp_path / "producer" + issued_digest = "sha256:issued" + seen_roots: list[Path] = [] + seen_lookups: list[tuple[str, str]] = [] + + def lookup_binding(execution_id, authority_path): + seen_lookups.append((execution_id, authority_path)) + return issued_root, issued_digest + + def resolve(_request, *, allowed_root): + seen_roots.append(allowed_root) + return SimpleNamespace( + accepted=True, + evidence=SimpleNamespace(plan_set_authority_digest=issued_digest), + ) + + resolved = _plan_preflight( + SimpleNamespace(lookup_binding=lookup_binding), + SimpleNamespace(resolve=resolve), + bound_inputs=(("plan_path", "plan.md"), ("plan_set_authority_path", "authority.json")), + ) + + assert resolved.plan_set is not None + assert seen_roots == [issued_root] + assert seen_lookups == [("generation", "authority.json")] + + +def test_accepted_plan_set_verification_without_evidence_fails_closed(tmp_path: Path) -> None: + digest = "sha256:issued" + with pytest.raises(RecipeExecutionAdmissionError) as exc_info: + _plan_preflight( + SimpleNamespace( + lookup_binding=lambda execution_id, authority_path: (tmp_path, digest) + ), + SimpleNamespace( + resolve=lambda request, *, allowed_root: SimpleNamespace( + accepted=True, + evidence=None, + ) + ), + bound_inputs=( + ("plan_path", "plan.md"), + ("plan_set_authority_path", "authority.json"), + ), + ) + + assert exc_info.value.code == "recipe_execution_preflight_unavailable" + + +def test_accepted_plan_set_digest_must_match_issued_digest(tmp_path: Path) -> None: + with pytest.raises(RecipeExecutionAdmissionError) as exc_info: + _plan_preflight( + SimpleNamespace( + lookup_binding=lambda execution_id, authority_path: (tmp_path, "sha256:issued") + ), + SimpleNamespace( + resolve=lambda request, *, allowed_root: SimpleNamespace( + accepted=True, + evidence=SimpleNamespace(plan_set_authority_digest="sha256:different"), + ) + ), + bound_inputs=( + ("plan_path", "plan.md"), + ("plan_set_authority_path", "authority.json"), + ), + ) + assert exc_info.value.code == "input_preflight_plan_set_authority_digest" + + async def _publication_authority(tmp_path: Path, *, sealed: bool = True): from tests.server._plan_set_test_helpers import PlanSetGitHub fixture = _FIXTURES / "publication_pr" part = tmp_path / "part.md" part.write_text((fixture / "part_a.md").read_text()) - bound = await DefaultPlanSetMaterializer(PlanSetGitHub(fixture_dir=fixture)).bind( + materializer = DefaultPlanSetMaterializer(PlanSetGitHub(fixture_dir=fixture)) + bound = await materializer.bind( PlanSetBindRequest( plan_parts_raw=str(part), allowed_root=tmp_path, @@ -157,13 +269,21 @@ async def _publication_authority(tmp_path: Path, *, sealed: bool = True): ) ) assert bound.success, bound - return bound, part + return bound, part, materializer -def _audit_preflight(tmp_path, path, *, kitchen="kitchen", generation="generation"): +def _audit_preflight( + tmp_path, + path, + materializer, + *, + kitchen="kitchen", + generation="generation", +): return resolve_attested_input_preflight( SimpleNamespace( kitchen_id=kitchen, + plan_set_materializer=materializer, skill_contract_resolver=lambda _: SimpleNamespace( input_preflight=("plan_set_stage_obligations",) ), @@ -183,8 +303,8 @@ def _audit_preflight(tmp_path, path, *, kitchen="kitchen", generation="generatio async def test_audit_scope_has_stage_outcomes_while_implementer_scope_has_only_code(tmp_path): - bound, part = await _publication_authority(tmp_path) - evidence = _audit_preflight(tmp_path, bound.plan_set_authority_path).plan_set + bound, part, materializer = await _publication_authority(tmp_path) + evidence = _audit_preflight(tmp_path, bound.plan_set_authority_path, materializer).plan_set assert evidence is not None assert evidence.part_key is None assert evidence.assigned_requirements == () @@ -212,11 +332,20 @@ async def test_audit_scope_has_stage_outcomes_while_implementer_scope_has_only_c ] assert "stage_obligations" not in implementation.evidence.to_dict() part.write_text(part.read_text() + "\nImplementation changed this part.\n") - assert _audit_preflight(tmp_path, bound.plan_set_authority_path).plan_set == evidence + assert ( + _audit_preflight(tmp_path, bound.plan_set_authority_path, materializer).plan_set + == evidence + ) def test_audit_without_authority_preserves_existing_scope(tmp_path): - assert _audit_preflight(tmp_path, "").plan_set is None + assert _audit_preflight(tmp_path, "", None).plan_set is None + + +def test_audit_with_authority_requires_materializer(tmp_path): + with pytest.raises(RecipeExecutionAdmissionError) as exc_info: + _audit_preflight(tmp_path, "authority.json", None) + assert exc_info.value.code == "recipe_execution_preflight_unavailable" @pytest.mark.parametrize( @@ -230,12 +359,21 @@ def test_audit_without_authority_preserves_existing_scope(tmp_path): async def test_audit_rejects_foreign_or_unsealed_authority( tmp_path, sealed, kitchen, generation, reason ): - bound, _ = await _publication_authority(tmp_path, sealed=sealed) + bound, _, materializer = await _publication_authority(tmp_path, sealed=sealed) with pytest.raises(RecipeExecutionAdmissionError) as exc_info: _audit_preflight( - tmp_path, bound.plan_set_authority_path, kitchen=kitchen, generation=generation + tmp_path, + bound.plan_set_authority_path, + materializer, + kitchen=kitchen, + generation=generation, ) - assert exc_info.value.code == f"input_preflight_plan_set_{reason}" + expected = ( + "input_preflight_plan_set_authority_invalid" + if generation != "generation" + else f"input_preflight_plan_set_{reason}" + ) + assert exc_info.value.code == expected def test_audit_contract_declares_authenticated_stage_scope(): diff --git a/tests/server/test_plan_set_preflight_implement.py b/tests/server/test_plan_set_preflight_implement.py index 95d03c681e..e1fe513cdc 100644 --- a/tests/server/test_plan_set_preflight_implement.py +++ b/tests/server/test_plan_set_preflight_implement.py @@ -3,7 +3,10 @@ from __future__ import annotations import json +import subprocess from pathlib import Path +from types import SimpleNamespace +from typing import Any import pytest @@ -13,20 +16,33 @@ PlanSetPreflightRequest, PlanSetRejectReason, RecipeExecutionId, + canonical_json_bytes, + compute_canonical_hash, resolve_temp_dir, ) +from autoskillit.core.planset import verifier as plan_set_verifier from autoskillit.pipeline import ReadyRecipe +from autoskillit.recipe.io import builtin_scripts_dir from autoskillit.server._plan_set_materializer import DefaultPlanSetMaterializer from autoskillit.server.lifecycle._guards import _check_dry_walkthrough_plan from autoskillit.server.recipe._plan_set_preflight import DefaultPlanSetPreflightResolver from autoskillit.server.recipe._recipe_execution import install_recipe_execution from autoskillit.server.tools.tools_execution import run_skill -from tests.server._helpers import _ready_recipe_segment_step +from autoskillit.server.tools.tools_pipeline_tracker import complete_run_skill_result +from tests.fakes import InMemoryHeadlessExecutor +from tests.server._helpers import _ready_recipe_segment_step, _skill_ok from tests.server._pipeline_test_helpers import _write_tracker +from tests.server._plan_set_test_helpers import write_plan_set_parts from tests.server.test_plan_set_materializer import _FIXTURES, _GitHub pytestmark = [pytest.mark.layer("server"), pytest.mark.anyio, pytest.mark.medium] +_IMPLEMENTATION_READY_CASE = ( + "implementation", + "implement", + {"issue_url": "https://github.com/TalonT-Org/AutoSkillit/issues/4261", "task": "test"}, +) + async def _bind( root: Path, @@ -37,6 +53,7 @@ async def _bind( seal: bool = True, parent: str = "", write_parts: bool = True, + materializer: DefaultPlanSetMaterializer | None = None, ): root.mkdir(parents=True, exist_ok=True) fixture = _FIXTURES / "split_r1_r2" @@ -44,7 +61,7 @@ async def _bind( if write_parts: for part in parts: part.write_text((fixture / part.name).read_text(), encoding="utf-8") - materializer = DefaultPlanSetMaterializer(_GitHub()) + materializer = materializer or DefaultPlanSetMaterializer(_GitHub()) bound = await materializer.bind( PlanSetBindRequest( plan_parts_raw="\n".join(str(part) for part in parts), @@ -63,6 +80,72 @@ async def _bind( return bound, parts, materializer +def _implementation_parts(clone: Path, tool_ctx, monkeypatch: pytest.MonkeyPatch): + root, *parts = write_plan_set_parts(clone, tool_ctx, monkeypatch) + plan_root = root / "make-plan" + plan_root.mkdir() + moved = tuple(part.rename(plan_root / part.name) for part in parts) + return plan_root, *moved + + +def _consumer_args( + step: dict[str, Any], + credential: dict[str, Any], + *, + step_name: str, + worktree: Path, + plan: Path, + authority_path: str, +) -> dict[str, Any]: + with_args = step["with"] + assert isinstance(with_args, dict) + skill_inputs = { + "plan_path": str(plan), + "plan_set_authority_path": authority_path, + } + if step_name == "retry_worktree": + skill_inputs["worktree_path"] = str(worktree) + digests = credential["invocation_template_digests"] + assert isinstance(digests, dict) + return { + "skill_command": with_args["skill_command"], + "cwd": str(worktree), + "step_name": step_name, + "output_dir": with_args["output_dir"], + "recipe_execution_id": credential["execution_id"], + "invocation_template_digest": digests[step_name], + "skill_inputs": skill_inputs, + } + + +def _bound_payload(command: str) -> dict[str, Any]: + marker = "AUTOSKILLIT_BOUND_INVOCATION_V1\n" + assert marker in command + payload = json.loads(command.split(marker, 1)[1]) + assert isinstance(payload, dict) + return payload + + +def _create_impl_worktree(clone: Path, temp_dir: str) -> Path: + completed = subprocess.run( + [ + "bash", + str(builtin_scripts_dir() / "create_impl_worktree.sh"), + "impl-plan-set-handoff", + temp_dir, + ], + cwd=clone, + check=True, + capture_output=True, + text=True, + timeout=30, + ) + token = next( + line for line in completed.stdout.splitlines() if line.startswith("worktree_path=") + ) + return Path(token.split("=", 1)[1]).resolve() + + def _preflight( authority_path: str, plan: Path, @@ -166,7 +249,14 @@ async def test_resumed_execution_consumes_original_authority_without_rebinding( original_execution = state.installed_execution original_id = original_execution.snapshot.execution_id kitchen = ready.tool_ctx.kitchen_id - bound, parts, _ = await _bind(tmp_path, kitchen=kitchen, generation=original_id) + materializer = ready.tool_ctx.plan_set_materializer + assert isinstance(materializer, DefaultPlanSetMaterializer) + monkeypatch.setattr(materializer, "_github_client", _GitHub()) + bound, parts, _ = await _bind( + tmp_path, kitchen=kitchen, generation=original_id, materializer=materializer + ) + pin = (tmp_path.resolve(), bound.plan_set_authority_digest) + assert materializer.lookup_binding(original_id, bound.plan_set_authority_path) == pin async def unexpected_bind(_request: object) -> None: raise AssertionError("resume must not re-bind an existing authority") @@ -177,6 +267,8 @@ async def unexpected_bind(_request: object) -> None: assert isinstance(resumed_state, ReadyRecipe) resumed_execution = resumed_state.installed_execution assert resumed_execution.snapshot.execution_id == original_id + assert materializer.lookup_binding(original_id, bound.plan_set_authority_path) == pin + assert materializer.lookup_binding("other-execution", bound.plan_set_authority_path) is None assert resumed_execution.plan_set_preflight_resolver is not None admitted = resumed_execution.plan_set_preflight_resolver.resolve( PlanSetPreflightRequest( @@ -192,6 +284,217 @@ async def unexpected_bind(_request: object) -> None: assert admitted.accepted +@pytest.mark.parametrize( + "tool_ctx_ready_recipe", + [_IMPLEMENTATION_READY_CASE], + indirect=True, +) +async def test_clone_authority_admits_implement_and_retry_from_fresh_sibling_worktree( + tool_ctx_ready_recipe, + git_checkout: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from autoskillit.server.tools.tools_plan_set import bind_plan_set + + ready = tool_ctx_ready_recipe + tool_ctx = ready.tool_ctx + _, part_a, part_b = _implementation_parts(git_checkout, tool_ctx, monkeypatch) + plan_parts = f"{part_a}\n{part_b}" + initial = json.loads( + await bind_plan_set( + plan_parts=plan_parts, + cwd=str(git_checkout), + issue_url="https://github.com/TalonT-Org/AutoSkillit/issues/4261", + step_name="bind_plan_set", + ) + ) + assert initial["success"] is True, initial + part_a.write_text("Dry-walkthrough verified = TRUE\n" + part_a.read_text()) + renewed = json.loads( + await bind_plan_set( + plan_parts=plan_parts, + cwd=str(git_checkout), + parent_authority_path=initial["plan_set_authority_path"], + step_name="renew_plan_set", + ) + ) + assert renewed["success"] is True, renewed + assert renewed["plan_set_authority_digest"] != initial["plan_set_authority_digest"] + + worktree = _create_impl_worktree(git_checkout, str(tool_ctx.config.workspace.temp_dir)) + worktree_temp = resolve_temp_dir(worktree, tool_ctx.config.workspace.temp_dir) + assert not worktree_temp.exists() + authority_path = Path(renewed["plan_set_authority_path"]) + assert part_a.is_file() and authority_path.is_file() + common_git_dir = Path( + subprocess.run( + ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"], + cwd=git_checkout, + check=True, + capture_output=True, + text=True, + timeout=10, + ).stdout.strip() + ) + exclude_path = common_git_dir / "info" / "exclude" + exclude_before = exclude_path.read_bytes() + + executor = InMemoryHeadlessExecutor( + default_result=_skill_ok(f"worktree_path = {worktree}\nhas_implementation_progress = true") + ) + monkeypatch.setattr(tool_ctx, "executor", executor) + _write_tracker( + tool_ctx.project_dir, + "AB", + { + "implement": {"status": "pending"}, + "retry_worktree": {"status": "pending"}, + }, + {}, + kitchen_id=tool_ctx.kitchen_id, + ) + + for step_name in ("implement", "retry_worktree"): + step, credential = _ready_recipe_segment_step(tool_ctx, step_name) + before = len(executor.calls) + response = json.loads( + await run_skill( + **_consumer_args( + step, + credential, + step_name=step_name, + worktree=worktree, + plan=part_a, + authority_path=str(authority_path), + ) + ) + ) + assert response["success"] is True, json.dumps(response) + assert len(executor.calls) == before + 1 + call = executor.calls[-1] + assert Path(call.cwd) == worktree + payload = _bound_payload(call.skill_command) + evidence = payload["verified_plan_set_preflight"] + assert evidence["plan_set_authority_digest"] == renewed["plan_set_authority_digest"] + assert [row["requirement_id"] for row in evidence["assigned_requirements"]] == ["R1"] + assert exclude_path.read_bytes() == exclude_before + assert not worktree_temp.exists() + assert part_a.is_file() and authority_path.is_file() + # Direct tool calls model delivery before the next orchestrator invocation. + completion = tool_ctx.run_skill_completion + assert completion is not None + receipt = completion.publish(response["receipt_id"]) + acknowledgement = json.loads( + await complete_run_skill_result( + receipt.receipt_id, + ctx=SimpleNamespace(session_id=receipt.request_session_id), + ) + ) + assert acknowledgement["success"] is True, acknowledgement + + materializer = tool_ctx.plan_set_materializer + assert materializer is not None + execution_id = ready.receipt["recipe_execution"]["execution_id"] + assert materializer.lookup_binding(execution_id, initial["plan_set_authority_path"]) == ( + resolve_temp_dir(git_checkout, tool_ctx.config.workspace.temp_dir).resolve(), + initial["plan_set_authority_digest"], + ) + assert materializer.lookup_binding(execution_id, str(authority_path)) == ( + resolve_temp_dir(git_checkout, tool_ctx.config.workspace.temp_dir).resolve(), + renewed["plan_set_authority_digest"], + ) + + +@pytest.mark.parametrize("case", ["unissued", "rewritten", "other_execution"]) +@pytest.mark.parametrize( + "tool_ctx_ready_recipe", + [_IMPLEMENTATION_READY_CASE], + indirect=True, +) +async def test_attested_implementation_rejects_untrusted_authority_provenance( + case: str, + tool_ctx_ready_recipe, + git_checkout: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from autoskillit.server.tools.tools_plan_set import bind_plan_set + + ready = tool_ctx_ready_recipe + tool_ctx = ready.tool_ctx + root, part_a, part_b = _implementation_parts(git_checkout, tool_ctx, monkeypatch) + part_a.write_text("Dry-walkthrough verified = TRUE\n" + part_a.read_text()) + execution_id = ready.receipt["recipe_execution"]["execution_id"] + materializer = tool_ctx.plan_set_materializer + assert isinstance(materializer, DefaultPlanSetMaterializer) + + if case == "unissued": + foreign = DefaultPlanSetMaterializer(_GitHub()) + bound, _, _ = await _bind( + root, + kitchen=tool_ctx.kitchen_id, + generation=execution_id, + write_parts=False, + materializer=foreign, + ) + authority_path = bound.plan_set_authority_path + expected_code = "input_preflight_plan_set_authority_invalid" + elif case == "other_execution": + bound, _, _ = await _bind( + root, + kitchen=tool_ctx.kitchen_id, + generation="other-execution", + write_parts=False, + materializer=materializer, + ) + authority_path = bound.plan_set_authority_path + expected_code = "input_preflight_plan_set_authority_invalid" + else: + bound = json.loads( + await bind_plan_set( + plan_parts=f"{part_a}\n{part_b}", + cwd=str(git_checkout), + issue_url="https://github.com/TalonT-Org/AutoSkillit/issues/4261", + step_name="bind_plan_set", + ) + ) + assert bound["success"] is True, bound + authority_path = bound["plan_set_authority_path"] + payload = json.loads(Path(authority_path).read_text()) + payload["dispatch_id"] = "self-consistent-rewrite" + unsigned = dict(payload) + unsigned.pop("authority_digest") + payload["authority_digest"] = compute_canonical_hash( + unsigned, domain=plan_set_verifier.PLAN_SET_AUTHORITY_DOMAIN + ) + Path(authority_path).write_bytes(canonical_json_bytes(payload)) + expected_code = "input_preflight_plan_set_authority_digest" + + executor = InMemoryHeadlessExecutor(default_result=_skill_ok("unexpected dispatch")) + monkeypatch.setattr(tool_ctx, "executor", executor) + _write_tracker( + tool_ctx.project_dir, + "AB", + {"implement": {"status": "pending"}}, + {}, + kitchen_id=tool_ctx.kitchen_id, + ) + step, credential = _ready_recipe_segment_step(tool_ctx, "implement") + response = json.loads( + await run_skill( + **_consumer_args( + step, + credential, + step_name="implement", + worktree=git_checkout, + plan=part_a, + authority_path=authority_path, + ) + ) + ) + assert response["error"].startswith(f"RECIPE EXECUTION REJECTED [{expected_code}]:") + assert executor.calls == [] + + async def test_marker_gate_precedes_digest_admission(tmp_path: Path, tool_ctx) -> None: root = tmp_path / "make-plan" bound, parts, _ = await _bind(root) @@ -224,17 +527,20 @@ async def test_marker_gate_precedes_digest_admission(tmp_path: Path, tool_ctx) - indirect=True, ) async def test_attested_marker_precedes_plan_set_digest_preflight( - tool_ctx_ready_recipe, git_checkout: Path + tool_ctx_ready_recipe, git_checkout: Path, monkeypatch: pytest.MonkeyPatch ) -> None: ready = tool_ctx_ready_recipe assert ready.tool_ctx.config.safety.require_dry_walkthrough step, credential = _ready_recipe_segment_step(ready.tool_ctx, "implement") work_dir = git_checkout allowed_root = resolve_temp_dir(work_dir, ready.tool_ctx.config.workspace.temp_dir) + materializer = DefaultPlanSetMaterializer(_GitHub()) + monkeypatch.setattr(ready.tool_ctx, "plan_set_materializer", materializer) bound, parts, _ = await _bind( allowed_root / "make-plan", kitchen=ready.tool_ctx.kitchen_id, generation=credential["execution_id"], + materializer=materializer, ) _write_tracker( ready.tool_ctx.project_dir, diff --git a/tests/server/test_recipe_execution_preflight_list.py b/tests/server/test_recipe_execution_preflight_list.py index a0ad43324a..c78363e0ae 100644 --- a/tests/server/test_recipe_execution_preflight_list.py +++ b/tests/server/test_recipe_execution_preflight_list.py @@ -8,8 +8,8 @@ import pytest from autoskillit.core import AdmissionStatus +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, resolve_attested_input_preflight, ) @@ -20,9 +20,11 @@ class _AuditResolver: def __init__(self, calls: list[str], status: AdmissionStatus) -> None: self.calls = calls self.status = status + self.allowed_root: Path | None = None def resolve(self, _request: object, *, allowed_root: Path): self.calls.append("audit") + self.allowed_root = allowed_root return SimpleNamespace( decision=SimpleNamespace( status=self.status, @@ -34,34 +36,62 @@ def resolve(self, _request: object, *, allowed_root: Path): class _PlanSetResolver: - def __init__(self, calls: list[str]) -> None: + def __init__(self, calls: list[str], digest: str) -> None: self.calls = calls + self.digest = digest + self.allowed_root: Path | None = None def resolve(self, _request: object, *, allowed_root: Path): self.calls.append("plan_set") - return SimpleNamespace(accepted=True, evidence=SimpleNamespace(to_dict=lambda: {})) + self.allowed_root = allowed_root + return SimpleNamespace( + accepted=True, + evidence=SimpleNamespace( + plan_set_authority_digest=self.digest, + to_dict=lambda: {}, + ), + ) + + +class _PlanSetMaterializer: + def __init__(self, calls: list[str], root: Path, digest: str) -> None: + self.calls = calls + self.root = root + self.digest = digest + + def lookup_binding(self, execution_id: str, authority_path: str) -> tuple[Path, str] | None: + self.calls.append("lookup") + assert execution_id == "generation" + assert authority_path == "authority.json" + return self.root, self.digest @pytest.mark.parametrize( ("status", "expected_calls"), [ (AdmissionStatus.REJECT, ["audit"]), - (AdmissionStatus.PASS, ["audit", "plan_set"]), + (AdmissionStatus.PASS, ["audit", "lookup", "plan_set"]), ], ) def test_preflights_run_in_contract_order( tmp_path: Path, status: AdmissionStatus, expected_calls: list[str] ) -> None: calls: list[str] = [] + invocation_root = tmp_path / "invocation" + bind_root = tmp_path / "bind" + digest = "sha256:issued" + audit_resolver = _AuditResolver(calls, status) + plan_set_resolver = _PlanSetResolver(calls, digest) context = SimpleNamespace( kitchen_id="kitchen", + plan_set_materializer=_PlanSetMaterializer(calls, bind_root, digest), skill_contract_resolver=lambda _command: SimpleNamespace( input_preflight=("audit_cycle_inventory", "plan_set_coverage") ), ) installed = SimpleNamespace( - input_preflight_resolver=_AuditResolver(calls, status), - plan_set_preflight_resolver=_PlanSetResolver(calls), + input_preflight_resolver=audit_resolver, + plan_set_preflight_resolver=plan_set_resolver, ) template = SimpleNamespace(invocation=SimpleNamespace(skill_name="dry-walkthrough")) kwargs = dict( @@ -73,7 +103,7 @@ def test_preflights_run_in_contract_order( ("plan_path", str(tmp_path / "plan.md")), ("plan_set_authority_path", "authority.json"), ), - allowed_root=tmp_path, + allowed_root=invocation_root, ) if status is AdmissionStatus.REJECT: with pytest.raises(RecipeExecutionAdmissionError, match="inventory rejected"): @@ -82,3 +112,6 @@ def test_preflights_run_in_contract_order( resolved = resolve_attested_input_preflight(context, installed, **kwargs) assert resolved.audit is not None and resolved.plan_set is not None assert calls == expected_calls + assert audit_resolver.allowed_root == invocation_root + if status is AdmissionStatus.PASS: + assert plan_set_resolver.allowed_root == bind_root diff --git a/tests/server/test_recipe_initialization.py b/tests/server/test_recipe_initialization.py index 123ea29a81..7c004e04a2 100644 --- a/tests/server/test_recipe_initialization.py +++ b/tests/server/test_recipe_initialization.py @@ -27,8 +27,8 @@ record_initialization_page, ) from autoskillit.server._factory import make_recipe_execution +from autoskillit.server._recipe_admission import RecipeExecutionAdmissionError from autoskillit.server.recipe._recipe_execution import ( - RecipeExecutionAdmissionError, build_recipe_execution_snapshot, install_recipe_execution, prepare_recipe_execution, diff --git a/tests/server/test_tools_plan_set.py b/tests/server/test_tools_plan_set.py index 9767bdd446..dae0370ad6 100644 --- a/tests/server/test_tools_plan_set.py +++ b/tests/server/test_tools_plan_set.py @@ -43,6 +43,11 @@ async def bind(self, request: PlanSetBindRequest) -> PlanSetBindResult: self.request = request return await self.delegate.bind(request) + def lookup_binding( + self, execution_generation: str, authority_path: str + ) -> tuple[Path, str] | None: + return self.delegate.lookup_binding(execution_generation, authority_path) + async def test_headless_bind_writes_snapshot_and_tracks_response( tmp_path: Path, tool_ctx, monkeypatch: pytest.MonkeyPatch