Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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: 6 additions & 1 deletion orchestrator/routes/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,16 @@ def mutate_contract(identifier: str) -> tuple[Response, int]:
"role": role.value,
"field_path": field_path,
"error": result.message,
"error_kind": result.error_kind,
},
)
# 403 only for authorization rejections; value/path errors
# are 400 so a client doesn't retry them as if a different
# role might succeed (#2495).
status_code = 403 if result.error_kind == "authorization" else 400
return _error(
result.message,
status_code=403,
status_code=status_code,
details={"role": role.value, "field_path": field_path},
)

Expand Down
51 changes: 50 additions & 1 deletion orchestrator/tests/test_contracts_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,56 @@ def test_role_validation_enforced(self, client, fake_worktree):
"new_value": "abc1234",
},
)
assert response.status_code in (403, 400)
assert response.status_code == 403

def test_invalid_path_returns_400(self, client, fake_worktree):
"""#2495: out-of-range index is a value error, not authorization.

Implementer is authorized to mutate ``phases.*.status``, but the
seeded contract has no phases — index 0 is out of range and
``apply_mutation`` translates the ``IndexError`` to ``MutationResult``
with ``error_kind="value"``. The route returns 400 so a client
does not retry as a different role.
"""
pipeline_id, worktree = fake_worktree
_seed_contract(worktree, pipeline_id)

response = self._mutate(
client,
pipeline_id,
role="implementer",
body_overrides={
"field_path": "phases.0.status",
"new_value": "complete",
},
)
assert response.status_code == 400
assert "Failed to apply mutation" in json.loads(response.data)["message"]

def test_invalid_value_returns_400(self, client, fake_worktree):
"""#2495: out-of-domain value (e.g. arbitrary string for a
``PipelinePhase`` enum) is a value error, not authorization.

Reviewer is authorized to mutate ``current_phase``, but pydantic's
``validate_assignment=True`` (added for #2465) raises
``ValidationError`` for ``"garbage"``. ``apply_mutation``
translates that to ``MutationResult`` with ``error_kind="value"``,
and the route returns 400.
"""
pipeline_id, worktree = fake_worktree
_seed_contract(worktree, pipeline_id)

response = self._mutate(
client,
pipeline_id,
role="reviewer",
body_overrides={
"field_path": "current_phase",
"new_value": "garbage",
},
)
assert response.status_code == 400
assert "Invalid value for current_phase" in json.loads(response.data)["message"]


class TestValidateMutation:
Expand Down
17 changes: 15 additions & 2 deletions shared/egg_contracts/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from dataclasses import dataclass
from typing import Any
from typing import Any, Literal

from pydantic import ValidationError

Expand All @@ -25,14 +25,24 @@ class ValidationResult:
required_role: str | None = None


MutationErrorKind = Literal["authorization", "value"]


@dataclass
class MutationResult:
"""Result of applying a mutation."""
"""Result of applying a mutation.

``error_kind`` discriminates failure causes for the route boundary
(#2495): ``"authorization"`` is a role-permission rejection (HTTP
403); ``"value"`` is a malformed path or out-of-domain value (HTTP
400/422). ``None`` on success.
"""

success: bool
message: str
contract: Contract | None = None
audit_entry: AuditEntry | None = None
error_kind: MutationErrorKind | None = None


def validate_mutation(
Expand Down Expand Up @@ -103,6 +113,7 @@ def apply_mutation(
return MutationResult(
success=False,
message=validation.message,
error_kind="authorization",
)

# Get the old value and apply the mutation
Expand All @@ -119,6 +130,7 @@ def apply_mutation(
return MutationResult(
success=False,
message=f"Failed to apply mutation: {e}",
error_kind="value",
)
except ValidationError as e:
# ``Contract.model_config = ConfigDict(validate_assignment=True)``
Expand All @@ -130,6 +142,7 @@ def apply_mutation(
return MutationResult(
success=False,
message=f"Invalid value for {field_path}: {e}",
error_kind="value",
)

# Create audit entry. ``current_phase`` mutations get the dedicated
Expand Down
Loading