From cf4467aa5e878775172add2eda7279eb0bbd553c Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:25:06 +0100 Subject: [PATCH] feat(valid-detection-benchmark): implement causal workflow journal with lease-protected head --- .../specs/valid-detection-benchmark/tasks.md | 2 +- benchmarks/valid/workflow_journal.py | 799 ++++++++++++++++++ .../test_valid_benchmark_workflow_journal.py | 626 ++++++++++++++ 3 files changed, 1426 insertions(+), 1 deletion(-) create mode 100644 benchmarks/valid/workflow_journal.py create mode 100644 tests/test_valid_benchmark_workflow_journal.py diff --git a/.kiro/specs/valid-detection-benchmark/tasks.md b/.kiro/specs/valid-detection-benchmark/tasks.md index 1079fb5f..f498d2ed 100644 --- a/.kiro/specs/valid-detection-benchmark/tasks.md +++ b/.kiro/specs/valid-detection-benchmark/tasks.md @@ -207,7 +207,7 @@ - Done when every terminal and resumable state has a deterministic digest and unsafe or free-form content cannot enter the shared outcome. - _Requirements: 8.1, 8.2, 8.3, 8.4, 8.5, 10.1, 10.2, 10.3_ - _Boundary: Workflow Outcome_ -- [ ] 7.2 (P) Implement the causal workflow journal and lease-protected head +- [x] 7.2 (P) Implement the causal workflow journal and lease-protected head - Append progress and heartbeat entries with exact sequence, predecessor, attempt, lease owner, counts, phase, and deadlines. - Make the initial CAS head select the exact custodian-sealed automatic-retry authority reference and digest before first execution, and preserve that selection unchanged through every successor. - Protect the current head with compare-and-swap, allow one resume owner only after an expired validated head, and refuse stale-owner append or renewal. diff --git a/benchmarks/valid/workflow_journal.py b/benchmarks/valid/workflow_journal.py new file mode 100644 index 00000000..03ee7bd7 --- /dev/null +++ b/benchmarks/valid/workflow_journal.py @@ -0,0 +1,799 @@ +"""Task 7.2 causal workflow journal with a lease-protected compare-and-swap head. + +The journal is the transient causal record of one benchmark workflow run. Every +entry names its exact sequence, predecessor, attempt, lease owner, phase, +counts, and deadlines; the single CAS-protected head selects the exact +custodian-sealed automatic-retry authority before first execution and preserves +that selection through every successor. All serialized fields are closed +vocabularies, bounded integers, or full digests, so exception text, paths, +labels, payloads, secrets, and customer content have no entry point. +""" + +from __future__ import annotations + +import json +import re +from collections.abc import Mapping +from dataclasses import dataclass +from datetime import datetime +from enum import StrEnum +from typing import Protocol, TypeVar, cast + +from benchmarks.valid.custody_probe_io import valid_version_id +from benchmarks.valid.replacement_artifact_contract import ( + ArtifactAccessClass, + ArtifactKind, + ArtifactReference, + canonical_json_bytes, +) +from benchmarks.valid.store_configuration import StorePurpose, StoreSettings +from benchmarks.valid.workflow_outcome import ( + WORKFLOW_COUNT_FIELDS, + WorkflowCounts, + WorkflowPhase, + WorkflowReasonCode, + WorkflowStatus, +) +from openultrasast.core import canonical +from openultrasast.core.canonical import JsonValue + +WORKFLOW_JOURNAL_SCHEMA = "openultrasast.valid-detection-benchmark.workflow-journal.v1" +WORKFLOW_HEAD_SCHEMA = "openultrasast.valid-detection-benchmark.workflow-journal-head.v1" +WORKFLOW_AUTHORITY_SCHEMA = "openultrasast.valid-detection-benchmark.workflow-journal-authority.v1" +WORKFLOW_ENTRY_FIELDS = frozenset( + { + "schema_version", + "workflow_digest", + "sequence", + "previous_entry_digest", + "attempt_id", + "lease_owner_id", + "phase", + "heartbeat_at", + "lease_expires_at", + "counts", + "entry_digest", + } +) +WORKFLOW_HEAD_FIELDS = frozenset( + { + "schema_version", + "workflow_digest", + "sequence", + "entry_digest", + "lease_owner_id", + "lease_expires_at", + "retry_authority_ref", + "retry_authority_digest", + "head_digest", + } +) +WORKFLOW_AUTHORITY_FIELDS = frozenset( + { + "schema_version", + "artifact_kind", + "access_class", + "provenance_digest", + "content_digest", + "byte_length", + } +) +_DIGEST = re.compile(r"[0-9a-f]{64}") +_IDENTIFIER = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,127}") +_TIMESTAMP = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z") +_CONFLICT_CODES = frozenset({"409", "412", "ConditionalRequestConflict", "PreconditionFailed"}) +_NOT_FOUND_CODES = frozenset({"404", "NoSuchKey", "NotFound", "NoSuchVersion"}) +_EnumT = TypeVar("_EnumT", bound=StrEnum) + + +class WorkflowJournalReason(StrEnum): + """Closed refusal reasons; no free-form failure text is serializable.""" + + INVALID_REQUEST = "invalid_request" + NOT_FOUND = "not_found" + MALFORMED_CONTENT = "malformed_content" + INVALID_ENTRY = "invalid_entry" + INVALID_HEAD = "invalid_head" + INVALID_SUCCESSOR = "invalid_successor" + AUTHORITY_SUBSTITUTED = "authority_substituted" + LEASE_OWNER_MISMATCH = "lease_owner_mismatch" + LEASE_NOT_EXPIRED = "lease_not_expired" + LEASE_EXPIRED = "lease_expired" + CHAIN_BROKEN = "chain_broken" + DIGEST_MISMATCH = "digest_mismatch" + READBACK_MISMATCH = "readback_mismatch" + UNREADABLE_RESPONSE = "unreadable_response" + STORE_FAILURE = "store_failure" + + +class WorkflowJournalError(RuntimeError): + """Refuse unsafe journal content or state with a closed reason.""" + + def __init__(self, reason: WorkflowJournalReason) -> None: + self.reason = reason + super().__init__(f"workflow_journal_{reason.value}") + + +def _is_digest(value: object, *, optional: bool = False) -> bool: + if optional and value == "": + return True + return type(value) is str and _DIGEST.fullmatch(value) is not None + + +def _is_identifier(value: object) -> bool: + return type(value) is str and _IDENTIFIER.fullmatch(value) is not None + + +def _is_timestamp(value: object) -> bool: + if type(value) is not str or _TIMESTAMP.fullmatch(value) is None: + return False + try: + datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ") + return True + except ValueError: + return False + + +def _is_sequence(value: object) -> bool: + return type(value) is int and 0 <= value <= 1 << 31 + + +def _require_digest(value: object) -> str: + if not _is_digest(value): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + return cast(str, value) + + +@dataclass(frozen=True) +class WorkflowJournalEntryV1: + """One causal progress or heartbeat record with its exact lease context.""" + + workflow_digest: str + sequence: int + previous_entry_digest: str | None + attempt_id: str + lease_owner_id: str + phase: WorkflowPhase + heartbeat_at: str + lease_expires_at: str + counts: WorkflowCounts + entry_digest: str + + def __post_init__(self) -> None: + _validate_entry(self) + expected = canonical.content_digest(_entry_value(self, include_digest=False)) + _bind_record_digest(self, "entry_digest", self.entry_digest, expected) + + +@dataclass(frozen=True) +class WorkflowHeadV1: + """CAS-protected head binding the immutable selected retry authority.""" + + workflow_digest: str + sequence: int + entry_digest: str + lease_owner_id: str + lease_expires_at: str + retry_authority_ref: ArtifactReference + retry_authority_digest: str + head_digest: str + + def __post_init__(self) -> None: + _validate_head(self) + expected = canonical.content_digest(_head_value(self, include_digest=False)) + _bind_record_digest(self, "head_digest", self.head_digest, expected) + + +def _bind_record_digest(record: object, field: str, current: str, expected: str) -> None: + if current and current != expected: + raise WorkflowJournalError(WorkflowJournalReason.DIGEST_MISMATCH) + if not current: + object.__setattr__(record, field, expected) + + +def _validate_entry(value: WorkflowJournalEntryV1) -> None: + if ( + type(value) is not WorkflowJournalEntryV1 + or type(value.phase) is not WorkflowPhase + or type(value.counts) is not WorkflowCounts + or not _is_digest(value.workflow_digest) + or not _is_sequence(value.sequence) + or (value.previous_entry_digest is not None and not _is_digest(value.previous_entry_digest)) + or not _is_identifier(value.attempt_id) + or not _is_identifier(value.lease_owner_id) + or not _is_timestamp(value.heartbeat_at) + or not _is_timestamp(value.lease_expires_at) + or value.lease_expires_at <= value.heartbeat_at + or not _is_digest(value.entry_digest, optional=True) + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + + +def _validate_head(value: WorkflowHeadV1) -> None: + reference = value.retry_authority_ref + if ( + type(value) is not WorkflowHeadV1 + or type(reference) is not ArtifactReference + or not _is_digest(value.workflow_digest) + or not _is_sequence(value.sequence) + or not _is_digest(value.entry_digest) + or not _is_identifier(value.lease_owner_id) + or not _is_timestamp(value.lease_expires_at) + or not _is_digest(value.retry_authority_digest) + or not _is_digest(value.head_digest, optional=True) + or reference.artifact_kind is not ArtifactKind.WORKFLOW + or reference.access_class is not ArtifactAccessClass.CUSTODIAN_SEALED + or value.retry_authority_digest != reference.content_digest + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + + +def _validate_head_entry(head: WorkflowHeadV1, entry: WorkflowJournalEntryV1) -> None: + if ( + type(head) is not WorkflowHeadV1 + or type(entry) is not WorkflowJournalEntryV1 + or head.workflow_digest != entry.workflow_digest + or head.sequence != entry.sequence + or head.entry_digest != entry.entry_digest + or head.lease_owner_id != entry.lease_owner_id + or head.lease_expires_at != entry.lease_expires_at + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_HEAD) + + +def successor_head(prior: WorkflowHeadV1, entry: WorkflowJournalEntryV1) -> WorkflowHeadV1: + """Build the successor head while preserving the selected authority exactly.""" + return WorkflowHeadV1( + prior.workflow_digest, + entry.sequence, + entry.entry_digest, + entry.lease_owner_id, + entry.lease_expires_at, + prior.retry_authority_ref, + prior.retry_authority_digest, + "", + ) + + +def require_selected_authority( + head: WorkflowHeadV1, candidate: ArtifactReference, authority_digest: str +) -> ArtifactReference: + """Accept a retry authority only when the CAS head selected exactly it.""" + if candidate != head.retry_authority_ref or authority_digest != head.retry_authority_digest: + raise WorkflowJournalError(WorkflowJournalReason.AUTHORITY_SUBSTITUTED) + return candidate + + +def _projection(lease_expired: bool) -> tuple[WorkflowStatus | None, WorkflowReasonCode | None]: + if lease_expired: + return (WorkflowStatus.IMPAIRED, WorkflowReasonCode.STALLED) + return (None, None) + + +@dataclass(frozen=True) +class WorkflowJournalStatus: + """Status rebuilt from the complete validated chain to the head.""" + + workflow_digest: str + head: WorkflowHeadV1 + head_entry: WorkflowJournalEntryV1 + chain_length: int + phase: WorkflowPhase + counts: WorkflowCounts + lease_expired: bool + projected_status: WorkflowStatus | None + projected_reason: WorkflowReasonCode | None + + def __post_init__(self) -> None: + if ( + type(self.lease_expired) is not bool + or type(self.chain_length) is not int + or self.phase != self.head_entry.phase + or self.counts != self.head_entry.counts + or self.chain_length != self.head_entry.sequence + 1 + or (self.projected_status, self.projected_reason) != _projection(self.lease_expired) + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + + +def _counts_value(counts: WorkflowCounts) -> dict[str, JsonValue]: + return { + "planned_arms": counts.planned_arms, + "completed_arms": counts.completed_arms, + "planned_inputs": counts.planned_inputs, + "completed_inputs": counts.completed_inputs, + "deferred_inputs": counts.deferred_inputs, + } + + +def _authority_value(reference: ArtifactReference) -> dict[str, JsonValue]: + return { + "schema_version": WORKFLOW_AUTHORITY_SCHEMA, + "artifact_kind": reference.artifact_kind.value, + "access_class": reference.access_class.value, + "provenance_digest": reference.provenance_digest, + "content_digest": reference.content_digest, + "byte_length": reference.byte_length, + } + + +def _entry_value(value: WorkflowJournalEntryV1, *, include_digest: bool) -> dict[str, JsonValue]: + result: dict[str, JsonValue] = { + "schema_version": WORKFLOW_JOURNAL_SCHEMA, + "workflow_digest": value.workflow_digest, + "sequence": value.sequence, + "previous_entry_digest": value.previous_entry_digest, + "attempt_id": value.attempt_id, + "lease_owner_id": value.lease_owner_id, + "phase": value.phase.value, + "heartbeat_at": value.heartbeat_at, + "lease_expires_at": value.lease_expires_at, + "counts": _counts_value(value.counts), + } + if include_digest: + result["entry_digest"] = value.entry_digest + return result + + +def _head_value(value: WorkflowHeadV1, *, include_digest: bool) -> dict[str, JsonValue]: + result: dict[str, JsonValue] = { + "schema_version": WORKFLOW_HEAD_SCHEMA, + "workflow_digest": value.workflow_digest, + "sequence": value.sequence, + "entry_digest": value.entry_digest, + "lease_owner_id": value.lease_owner_id, + "lease_expires_at": value.lease_expires_at, + "retry_authority_ref": _authority_value(value.retry_authority_ref), + "retry_authority_digest": value.retry_authority_digest, + } + if include_digest: + result["head_digest"] = value.head_digest + return result + + +def journal_entry_bytes(value: WorkflowJournalEntryV1) -> bytes: + """Encode a revalidated journal entry as deterministic canonical JSON.""" + value.__post_init__() + return canonical.canonical_bytes(_entry_value(value, include_digest=True)) + + +def journal_head_bytes(value: WorkflowHeadV1) -> bytes: + """Encode a revalidated journal head as deterministic canonical JSON.""" + value.__post_init__() + return canonical.canonical_bytes(_head_value(value, include_digest=True)) + + +def _unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]: + value: dict[str, object] = {} + for key, item in pairs: + if key in value: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + value[key] = item + return value + + +def _decode_canonical(content: bytes) -> dict[str, object]: + if type(content) is not bytes or not content: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + try: + decoded = json.loads(content, object_pairs_hook=_unique_object) + except (UnicodeError, json.JSONDecodeError): + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) from None + if type(decoded) is not dict or canonical_json_bytes(decoded) != content: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + return decoded + + +def _decode_enum(value: object, enum_type: type[_EnumT]) -> _EnumT: + if type(value) is not str: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + try: + return enum_type(value) + except ValueError: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) from None + + +def _decode_counts(value: object) -> WorkflowCounts: + if type(value) is not dict or set(value) != WORKFLOW_COUNT_FIELDS: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + try: + return WorkflowCounts( + cast(int, value["planned_arms"]), + cast(int, value["completed_arms"]), + cast(int, value["planned_inputs"]), + cast(int, value["completed_inputs"]), + cast(int, value["deferred_inputs"]), + ) + except (KeyError, ValueError): + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) from None + + +def _decode_authority(value: object) -> ArtifactReference: + if ( + type(value) is not dict + or set(value) != WORKFLOW_AUTHORITY_FIELDS + or value.get("schema_version") != WORKFLOW_AUTHORITY_SCHEMA + or type(value.get("byte_length")) is not int + ): + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + try: + return ArtifactReference( + ArtifactKind(cast(str, value["artifact_kind"])), + cast(str, value["content_digest"]), + cast(int, value["byte_length"]), + cast(str, value["provenance_digest"]), + ArtifactAccessClass(cast(str, value["access_class"])), + ) + except (KeyError, ValueError): + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) from None + + +def decode_workflow_journal_entry(content: bytes) -> WorkflowJournalEntryV1: + """Decode only canonical journal-entry bytes under the closed schema.""" + value = _decode_canonical(content) + if set(value) != WORKFLOW_ENTRY_FIELDS or value.get("schema_version") != WORKFLOW_JOURNAL_SCHEMA: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + previous = value["previous_entry_digest"] + return WorkflowJournalEntryV1( + cast(str, value["workflow_digest"]), + cast(int, value["sequence"]), + None if previous is None else cast(str, previous), + cast(str, value["attempt_id"]), + cast(str, value["lease_owner_id"]), + _decode_enum(value["phase"], WorkflowPhase), + cast(str, value["heartbeat_at"]), + cast(str, value["lease_expires_at"]), + _decode_counts(value["counts"]), + cast(str, value["entry_digest"]), + ) + + +def decode_workflow_head(content: bytes) -> WorkflowHeadV1: + """Decode only canonical journal-head bytes under the closed schema.""" + value = _decode_canonical(content) + if set(value) != WORKFLOW_HEAD_FIELDS or value.get("schema_version") != WORKFLOW_HEAD_SCHEMA: + raise WorkflowJournalError(WorkflowJournalReason.MALFORMED_CONTENT) + return WorkflowHeadV1( + cast(str, value["workflow_digest"]), + cast(int, value["sequence"]), + cast(str, value["entry_digest"]), + cast(str, value["lease_owner_id"]), + cast(str, value["lease_expires_at"]), + _decode_authority(value["retry_authority_ref"]), + cast(str, value["retry_authority_digest"]), + cast(str, value["head_digest"]), + ) + + +class S3WorkflowJournalClient(Protocol): + def put_object(self, **kwargs: object) -> Mapping[str, object]: ... + + def get_object(self, **kwargs: object) -> Mapping[str, object]: ... + + +@dataclass(frozen=True) +class _Receipt: + version_id: str + storage_token: str + + +def workflow_head_key(workflow_digest: str) -> str: + return f"workflow/{workflow_digest}/head.json" + + +def workflow_entry_key(workflow_digest: str, entry_digest: str) -> str: + return f"workflow/{workflow_digest}/entries/{entry_digest}.json" + + +def _provider_code(error: Exception) -> str: + try: + response = getattr(error, "response", None) + details = response.get("Error") if isinstance(response, Mapping) else None + code = details.get("Code", "") if isinstance(details, Mapping) else getattr(error, "code", "") + except Exception: + return "" + return code if isinstance(code, str) else "" + + +class S3WorkflowJournal: + """Append-only causal journal with one lease-protected compare-and-swap head.""" + + def __init__(self, settings: StoreSettings, client: S3WorkflowJournalClient) -> None: + if not isinstance(settings, StoreSettings) or settings.purpose is not StorePurpose.TRANSIENT: + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + self._bucket, self._client = settings.bucket, client + + def initialize( + self, + entry: WorkflowJournalEntryV1, + retry_authority_ref: ArtifactReference, + retry_authority_digest: str, + ) -> WorkflowHeadV1 | None: + """Select the sealed retry authority in the first head before execution.""" + if ( + type(entry) is not WorkflowJournalEntryV1 + or entry.sequence != 0 + or entry.previous_entry_digest is not None + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_ENTRY) + head = WorkflowHeadV1( + entry.workflow_digest, + 0, + entry.entry_digest, + entry.lease_owner_id, + entry.lease_expires_at, + retry_authority_ref, + retry_authority_digest, + "", + ) + _validate_head_entry(head, entry) + self._persist_entry(entry.workflow_digest, entry.entry_digest, journal_entry_bytes(entry)) + receipt, conflicted = self._write( + workflow_head_key(entry.workflow_digest), journal_head_bytes(head), absent=True + ) + if conflicted: + return None + self._verify_head(head, journal_head_bytes(head), self._require_receipt(receipt)) + return head + + def current_head(self, workflow_digest: str) -> WorkflowHeadV1: + """Read and validate the durable head after scratch loss or restart.""" + head, _, _ = self._validated_head(_require_digest(workflow_digest)) + return head + + def append_and_swap( + self, + expected_head_digest: str, + entry: WorkflowJournalEntryV1, + head: WorkflowHeadV1, + ) -> bool: + """Append one entry and swap the head only when the exact head is current.""" + _require_digest(expected_head_digest) + _validate_head_entry(head, entry) + prior, _, receipt = self._validated_head(_require_digest(entry.workflow_digest)) + if prior.head_digest != expected_head_digest: + return False + self._validate_successor(prior, entry, head) + self._persist_entry(entry.workflow_digest, entry.entry_digest, journal_entry_bytes(entry)) + return self._swap_head(prior, receipt, head) + + def acquire_expired_lease( + self, + workflow_digest: str, + expected_head_digest: str, + attempt_id: str, + lease_owner_id: str, + *, + phase: WorkflowPhase, + counts: WorkflowCounts, + heartbeat_at: str, + lease_expires_at: str, + ) -> WorkflowHeadV1 | None: + """Grant one resume owner only over an expired validated head.""" + if ( + not _is_digest(workflow_digest) + or not _is_digest(expected_head_digest) + or not _is_identifier(attempt_id) + or not _is_identifier(lease_owner_id) + or type(phase) is not WorkflowPhase + or type(counts) is not WorkflowCounts + or not _is_timestamp(heartbeat_at) + or not _is_timestamp(lease_expires_at) + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + prior, _, receipt = self._validated_head(workflow_digest) + if prior.head_digest != expected_head_digest: + return None + if heartbeat_at < prior.lease_expires_at: + raise WorkflowJournalError(WorkflowJournalReason.LEASE_NOT_EXPIRED) + entry = WorkflowJournalEntryV1( + workflow_digest, + prior.sequence + 1, + prior.entry_digest, + attempt_id, + lease_owner_id, + phase, + heartbeat_at, + lease_expires_at, + counts, + "", + ) + head = successor_head(prior, entry) + self._persist_entry(head.workflow_digest, entry.entry_digest, journal_entry_bytes(entry)) + return head if self._swap_head(prior, receipt, head) else None + + def rebuild_status(self, workflow_digest: str, now: str) -> WorkflowJournalStatus: + """Rebuild status from the complete chain to the head, ignoring orphans.""" + digest = _require_digest(workflow_digest) + if not _is_timestamp(now): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_REQUEST) + head, entry, _ = self._validated_head(digest) + chain_length = self._validate_chain(digest, entry) + lease_expired = now >= head.lease_expires_at + status, reason = _projection(lease_expired) + return WorkflowJournalStatus( + digest, head, entry, chain_length, entry.phase, entry.counts, lease_expired, status, reason + ) + + def _validated_head(self, digest: str) -> tuple[WorkflowHeadV1, WorkflowJournalEntryV1, _Receipt]: + read = self._read(workflow_head_key(digest)) + if read is None: + raise WorkflowJournalError(WorkflowJournalReason.NOT_FOUND) + body, receipt = read + head = decode_workflow_head(body) + if head.workflow_digest != digest: + raise WorkflowJournalError(WorkflowJournalReason.INVALID_HEAD) + entry = self._read_entry(digest, head.entry_digest) + _validate_head_entry(head, entry) + return head, entry, receipt + + def _validate_chain(self, digest: str, head_entry: WorkflowJournalEntryV1) -> int: + entry = head_entry + length = 1 + while entry.sequence > 0: + previous = entry.previous_entry_digest + if type(previous) is not str: + raise WorkflowJournalError(WorkflowJournalReason.CHAIN_BROKEN) + prior = self._read_entry(digest, previous) + if prior.sequence != entry.sequence - 1: + raise WorkflowJournalError(WorkflowJournalReason.CHAIN_BROKEN) + entry = prior + length += 1 + return length + + def _read_entry(self, digest: str, entry_digest: str) -> WorkflowJournalEntryV1: + read = self._read(workflow_entry_key(digest, entry_digest)) + if read is None: + raise WorkflowJournalError(WorkflowJournalReason.CHAIN_BROKEN) + entry = decode_workflow_journal_entry(read[0]) + if entry.entry_digest != entry_digest or entry.workflow_digest != digest: + raise WorkflowJournalError(WorkflowJournalReason.CHAIN_BROKEN) + return entry + + def _validate_successor( + self, prior: WorkflowHeadV1, entry: WorkflowJournalEntryV1, head: WorkflowHeadV1 + ) -> None: + if ( + entry.workflow_digest != prior.workflow_digest + or entry.sequence != prior.sequence + 1 + or entry.previous_entry_digest != prior.entry_digest + ): + raise WorkflowJournalError(WorkflowJournalReason.INVALID_SUCCESSOR) + if ( + head.retry_authority_ref != prior.retry_authority_ref + or head.retry_authority_digest != prior.retry_authority_digest + ): + raise WorkflowJournalError(WorkflowJournalReason.AUTHORITY_SUBSTITUTED) + if entry.lease_owner_id != prior.lease_owner_id: + raise WorkflowJournalError(WorkflowJournalReason.LEASE_OWNER_MISMATCH) + if entry.heartbeat_at >= prior.lease_expires_at: + raise WorkflowJournalError(WorkflowJournalReason.LEASE_EXPIRED) + + def _swap_head(self, prior: WorkflowHeadV1, receipt: _Receipt, head: WorkflowHeadV1) -> bool: + head_body = journal_head_bytes(head) + committed, conflicted = self._write( + workflow_head_key(head.workflow_digest), head_body, token=receipt.storage_token + ) + if conflicted: + return False + replacement = self._require_receipt(committed) + if ( + replacement.version_id == receipt.version_id + or replacement.storage_token == receipt.storage_token + ): + raise WorkflowJournalError(WorkflowJournalReason.UNREADABLE_RESPONSE) + self._verify_head(head, head_body, replacement) + return True + + def _persist_entry(self, digest: str, entry_digest: str, body: bytes) -> None: + key = workflow_entry_key(digest, entry_digest) + receipt, conflicted = self._write(key, body, absent=True) + if conflicted: + read = self._read(key) + if read is None or read[0] != body: + raise WorkflowJournalError(WorkflowJournalReason.READBACK_MISMATCH) + return + self._require_receipt(receipt) + + def _verify_head(self, head: WorkflowHeadV1, body: bytes, receipt: _Receipt) -> None: + read = self._read(workflow_head_key(head.workflow_digest), receipt.version_id) + if read is None: + raise WorkflowJournalError(WorkflowJournalReason.READBACK_MISMATCH) + current_body, current_receipt = read + if current_body != body or current_receipt != receipt or decode_workflow_head(current_body) != head: + raise WorkflowJournalError(WorkflowJournalReason.READBACK_MISMATCH) + + def _write( + self, key: str, body: bytes, *, absent: bool = False, token: str | None = None + ) -> tuple[_Receipt | None, bool]: + request: dict[str, object] = { + "Bucket": self._bucket, + "Key": key, + "Body": body, + **({"IfNoneMatch": "*"} if absent else {}), + **({"IfMatch": token} if token is not None else {}), + } + code = "" + try: + response = self._client.put_object(**request) + except Exception as error: + code, response = _provider_code(error), None + if response is None: + return (None, code in _CONFLICT_CODES) + if ( + not isinstance(response, Mapping) + or not valid_version_id(response.get("VersionId")) + or type(response.get("ETag")) is not str + or not response.get("ETag") + ): + raise WorkflowJournalError(WorkflowJournalReason.UNREADABLE_RESPONSE) + return (_Receipt(cast(str, response.get("VersionId")), cast(str, response.get("ETag"))), False) + + def _read(self, key: str, version_id: str | None = None) -> tuple[bytes, _Receipt] | None: + request: dict[str, object] = { + "Bucket": self._bucket, + "Key": key, + **({"VersionId": version_id} if version_id is not None else {}), + } + code = "" + try: + response = self._client.get_object(**request) + except Exception as error: + code, response = _provider_code(error), None + if response is None: + if code in _NOT_FOUND_CODES: + return None + raise WorkflowJournalError(WorkflowJournalReason.STORE_FAILURE) + return self._decode_read(response) + + @staticmethod + def _decode_read(response: Mapping[str, object]) -> tuple[bytes, _Receipt]: + source = response.get("Body") + try: + body = source.read() if hasattr(source, "read") else source + except Exception: + body = None + version_id, token, length = response.get("VersionId"), response.get("ETag"), response.get("ContentLength") + if ( + type(body) is not bytes + or not body + or type(version_id) is not str + or type(length) is not int + or length != len(body) + or not valid_version_id(version_id) + or type(token) is not str + or not token + ): + raise WorkflowJournalError(WorkflowJournalReason.UNREADABLE_RESPONSE) + return body, _Receipt(version_id, token) + + @staticmethod + def _require_receipt(receipt: _Receipt | None) -> _Receipt: + if receipt is None: + raise WorkflowJournalError(WorkflowJournalReason.STORE_FAILURE) + return receipt + + +__all__ = [ + "S3WorkflowJournal", + "S3WorkflowJournalClient", + "WorkflowHeadV1", + "WorkflowJournalEntryV1", + "WorkflowJournalError", + "WorkflowJournalReason", + "WorkflowJournalStatus", + "WORKFLOW_AUTHORITY_FIELDS", + "WORKFLOW_AUTHORITY_SCHEMA", + "WORKFLOW_ENTRY_FIELDS", + "WORKFLOW_HEAD_FIELDS", + "WORKFLOW_HEAD_SCHEMA", + "WORKFLOW_JOURNAL_SCHEMA", + "decode_workflow_head", + "decode_workflow_journal_entry", + "journal_entry_bytes", + "journal_head_bytes", + "require_selected_authority", + "successor_head", + "workflow_entry_key", + "workflow_head_key", +] diff --git a/tests/test_valid_benchmark_workflow_journal.py b/tests/test_valid_benchmark_workflow_journal.py new file mode 100644 index 00000000..10d89db7 --- /dev/null +++ b/tests/test_valid_benchmark_workflow_journal.py @@ -0,0 +1,626 @@ +"""Task 7.2 causal workflow journal, lease-protected CAS head, stalled projection.""" + +from __future__ import annotations + +import io +import json +import re +import sys +import threading +from collections.abc import Callable +from hashlib import sha256 +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from benchmarks.valid import workflow_journal # noqa: E402 +from benchmarks.valid.replacement_artifact_contract import ( # noqa: E402 + ArtifactAccessClass, + ArtifactKind, + ArtifactReference, + canonical_json_bytes, +) +from benchmarks.valid.store_configuration import StorePurpose, StoreSettings # noqa: E402 +from benchmarks.valid.workflow_outcome import ( # noqa: E402 + WorkflowCounts, + WorkflowPhase, + WorkflowReasonCode, + WorkflowStatus, +) +from openultrasast.core import canonical # noqa: E402 + +_HEX64 = re.compile(r"[0-9a-f]{64}") + + +class _ProviderError(Exception): + def __init__(self, code: str) -> None: + self.response = {"Error": {"Code": code, "Message": "provider password=/private/path"}} + super().__init__("provider secret") + + +class _Version: + def __init__(self, version_id: str, body: bytes, etag: str) -> None: + self.version_id = version_id + self.body = body + self.etag = etag + + +class _Client: + def __init__(self) -> None: + self.lock = threading.Lock() + self.versions: dict[str, list[_Version]] = {} + self.next_version = 1 + self.force_head_conflict = False + self.provider_failure = False + self.corrupt_reads = False + self.head_write_barrier: threading.Barrier | None = None + + def put_object(self, **kwargs: object) -> dict[str, object]: + key = str(kwargs["Key"]) + barrier = self.head_write_barrier + if barrier is not None and key.endswith("head.json") and "IfMatch" in kwargs: + barrier.wait(timeout=5) + with self.lock: + if self.provider_failure: + raise _ProviderError("InternalError") + versions = self.versions.setdefault(key, []) + current = versions[-1] if versions else None + if self.force_head_conflict and key.endswith("head.json"): + raise _ProviderError("PreconditionFailed") + if kwargs.get("IfNoneMatch") == "*" and current is not None: + raise _ProviderError("PreconditionFailed") + expected = kwargs.get("IfMatch") + if expected is not None and (current is None or current.etag != expected): + raise _ProviderError("PreconditionFailed") + body = kwargs["Body"] + assert isinstance(body, bytes) and body + version_id = f"version-{self.next_version}" + self.next_version += 1 + etag = sha256(body + version_id.encode()).hexdigest() + versions.append(_Version(version_id, body, etag)) + return {"ETag": etag, "VersionId": version_id} + + def get_object(self, **kwargs: object) -> dict[str, object]: + with self.lock: + if self.provider_failure: + raise _ProviderError("InternalError") + key = str(kwargs["Key"]) + requested = kwargs.get("VersionId") + matching = [ + item + for item in self.versions.get(key, []) + if requested is None or item.version_id == requested + ] + if not matching: + raise _ProviderError("NoSuchKey") + version = matching[-1] + body = b"corrupt" if self.corrupt_reads else version.body + return { + "Body": io.BytesIO(body), + "ContentLength": len(body), + "VersionId": version.version_id, + "ETag": version.etag, + } + + def drop(self, key: str) -> None: + with self.lock: + self.versions.pop(key, None) + + def raw_put(self, key: str, body: bytes) -> None: + with self.lock: + version_id = f"version-{self.next_version}" + self.next_version += 1 + self.versions.setdefault(key, []).append( + _Version(version_id, body, sha256(body + version_id.encode()).hexdigest()) + ) + + +def _digest(seed: str) -> str: + return canonical.content_digest({"seed": seed}) + + +def _settings(purpose: StorePurpose = StorePurpose.TRANSIENT) -> StoreSettings: + return StoreSettings( + purpose, + f"https://{purpose.value}.invalid:9000", + f"valid-{purpose.value}", + "ACCESS-SECRET", + "SECRET-SECRET", + ) + + +def _journal(client: _Client) -> workflow_journal.S3WorkflowJournal: + return workflow_journal.S3WorkflowJournal(_settings(), client) + + +def _authority( + seed: str = "authority", + kind: ArtifactKind = ArtifactKind.WORKFLOW, + access: ArtifactAccessClass = ArtifactAccessClass.CUSTODIAN_SEALED, +) -> ArtifactReference: + content = _digest(seed) + return ArtifactReference(kind, content, 256, _digest(f"{seed}-provenance"), access) + + +def _at(minute: int) -> str: + hour, minute = divmod(minute, 60) + return f"2026-11-20T{9 + hour:02d}:{minute:02d}:00Z" + + +def _entry(**overrides: object) -> workflow_journal.WorkflowJournalEntryV1: + value: dict[str, object] = { + "workflow_digest": _digest("workflow"), + "sequence": 1, + "previous_entry_digest": _digest("entry-0"), + "attempt_id": "attempt-1", + "lease_owner_id": "owner-a", + "phase": WorkflowPhase.ARM_EXECUTION, + "heartbeat_at": _at(1), + "lease_expires_at": _at(31), + "counts": WorkflowCounts(2, 1, 12, 5, 0), + "entry_digest": "", + } + value.update(overrides) + return workflow_journal.WorkflowJournalEntryV1(**value) # type: ignore[arg-type] + + +def _initial_entry() -> workflow_journal.WorkflowJournalEntryV1: + return _entry( + sequence=0, + previous_entry_digest=None, + phase=WorkflowPhase.PREFLIGHT, + counts=WorkflowCounts(2, 0, 12, 0, 0), + ) + + +def _started( + client: _Client, authority: ArtifactReference | None = None +) -> tuple[workflow_journal.S3WorkflowJournal, workflow_journal.WorkflowHeadV1]: + journal = _journal(client) + selected = authority if authority is not None else _authority() + head = journal.initialize(_initial_entry(), selected, selected.content_digest) + assert head is not None + return journal, head + + +def _successor( + head: workflow_journal.WorkflowHeadV1, sequence: int, **overrides: object +) -> tuple[workflow_journal.WorkflowJournalEntryV1, workflow_journal.WorkflowHeadV1]: + fields: dict[str, object] = { + "heartbeat_at": _at(sequence), + "lease_expires_at": _at(sequence + 30), + } + fields.update(overrides) + entry = _entry(sequence=sequence, previous_entry_digest=head.entry_digest, **fields) + return entry, workflow_journal.successor_head(head, entry) + + +def _appended( + journal: workflow_journal.S3WorkflowJournal, + head: workflow_journal.WorkflowHeadV1, + sequence: int, + **overrides: object, +) -> workflow_journal.WorkflowHeadV1: + entry, successor = _successor(head, sequence, **overrides) + assert journal.append_and_swap(head.head_digest, entry, successor) is True + return successor + + +def _run_threads(action: Callable[[], object], count: int = 2) -> list[object]: + results: list[object] = [] + lock = threading.Lock() + + def target() -> None: + outcome = action() + with lock: + results.append(outcome) + + threads = [threading.Thread(target=target) for _ in range(count)] + for thread in threads: + thread.start() + for thread in threads: + thread.join(timeout=10) + return results + + +def _reserialized(content: bytes, mutate: Callable[[dict[str, object]], None]) -> bytes: + value = json.loads(content) + assert isinstance(value, dict) + mutate(value) + return canonical_json_bytes(value) + + +def test_entry_binds_exact_causal_fields_with_byte_identical_digest() -> None: + first = _entry() + second = _entry() + assert first == second + assert workflow_journal.journal_entry_bytes(first) == workflow_journal.journal_entry_bytes(second) + assert _HEX64.fullmatch(first.entry_digest) + changed = _entry(counts=WorkflowCounts(2, 2, 12, 6, 0)) + assert changed.entry_digest != first.entry_digest + decoded = workflow_journal.decode_workflow_journal_entry( + workflow_journal.journal_entry_bytes(first) + ) + assert decoded == first + + +def test_entry_rejects_free_form_or_noncanonical_content() -> None: + cases: list[dict[str, object]] = [ + {"workflow_digest": "not-a-digest"}, + {"sequence": -1}, + {"sequence": "1"}, + {"previous_entry_digest": "not-a-digest"}, + {"attempt_id": "attempt;/rm -rf"}, + {"attempt_id": ""}, + {"lease_owner_id": "owner with spaces"}, + {"phase": "tuning"}, + {"heartbeat_at": "2026-11-20 09:00:00"}, + {"heartbeat_at": "2026-13-20T09:00:00Z"}, + {"lease_expires_at": _at(1)}, + {"counts": (2, 1, 12, 5, 0)}, + ] + for overrides in cases: + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + _entry(**overrides) + assert report.value.reason is workflow_journal.WorkflowJournalReason.INVALID_REQUEST + + +def test_head_round_trip_and_noncanonical_bytes_are_refused() -> None: + authority = _authority() + entry = _initial_entry() + head = workflow_journal.WorkflowHeadV1( + entry.workflow_digest, + 0, + entry.entry_digest, + entry.lease_owner_id, + entry.lease_expires_at, + authority, + authority.content_digest, + "", + ) + content = workflow_journal.journal_head_bytes(head) + assert workflow_journal.decode_workflow_head(content) == head + assert b"password" not in content and b"/private" not in content + + def drop_field(value: dict[str, object]) -> None: + value.pop("head_digest") + + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + workflow_journal.decode_workflow_head(_reserialized(content, drop_field)) + assert report.value.reason is workflow_journal.WorkflowJournalReason.MALFORMED_CONTENT + with pytest.raises(workflow_journal.WorkflowJournalError): + workflow_journal.decode_workflow_head(b'{"schema_version": "x"} ') + with pytest.raises(workflow_journal.WorkflowJournalError): + workflow_journal.decode_workflow_journal_entry(b"") + + +def test_initial_head_selects_exact_sealed_authority_before_first_execution() -> None: + client = _Client() + _, head = _started(client) + authority = head.retry_authority_ref + assert authority.artifact_kind is ArtifactKind.WORKFLOW + assert authority.access_class is ArtifactAccessClass.CUSTODIAN_SEALED + assert head.retry_authority_digest == authority.content_digest + assert head.sequence == 0 + assert (head.workflow_digest, head.lease_owner_id) == (_digest("workflow"), "owner-a") + assert head.entry_digest == _initial_entry().entry_digest + + +def test_initial_head_refuses_unsealed_foreign_or_mismatched_authority() -> None: + client = _Client() + journal = _journal(client) + unsealed = _authority(access=ArtifactAccessClass.PRIVATE) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.initialize(_initial_entry(), unsealed, unsealed.content_digest) + assert report.value.reason is workflow_journal.WorkflowJournalReason.INVALID_REQUEST + foreign = _authority(kind=ArtifactKind.OBSERVATION) + with pytest.raises(workflow_journal.WorkflowJournalError): + journal.initialize(_initial_entry(), foreign, foreign.content_digest) + sealed = _authority() + with pytest.raises(workflow_journal.WorkflowJournalError): + journal.initialize(_initial_entry(), sealed, _digest("other-authority")) + with pytest.raises(workflow_journal.WorkflowJournalError) as initial: + journal.initialize(_entry(), sealed, sealed.content_digest) + assert initial.value.reason is workflow_journal.WorkflowJournalReason.INVALID_ENTRY + durable = None + with pytest.raises(workflow_journal.WorkflowJournalError): + durable = workflow_journal.S3WorkflowJournal(_settings(StorePurpose.DURABLE), _Client()) + durable.initialize(_initial_entry(), sealed, sealed.content_digest) + assert durable is None + + +def test_initialize_has_exactly_one_winner_and_preserves_first_head() -> None: + client = _Client() + journal, head = _started(client) + other = _authority("other") + assert journal.initialize(_initial_entry(), other, other.content_digest) is None + assert journal.current_head(_digest("workflow")) == head + + +def test_successor_heads_preserve_the_selected_authority_unchanged() -> None: + client = _Client() + journal, head = _started(client) + first = _appended(journal, head, 1, counts=WorkflowCounts(2, 1, 12, 5, 0)) + second = _appended(journal, first, 2, counts=WorkflowCounts(2, 1, 12, 6, 0)) + authority = head.retry_authority_ref + for successor in (first, second): + assert successor.retry_authority_ref == authority + assert successor.retry_authority_digest == head.retry_authority_digest + assert second.entry_digest != first.entry_digest + assert journal.current_head(_digest("workflow")) == second + + +def test_substituted_authority_cannot_replace_the_selection() -> None: + client = _Client() + journal, head = _started(client) + entry = _entry(previous_entry_digest=head.entry_digest) + substituted = workflow_journal.WorkflowHeadV1( + entry.workflow_digest, + 1, + entry.entry_digest, + entry.lease_owner_id, + entry.lease_expires_at, + _authority("substituted"), + _digest("substituted"), + "", + ) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.append_and_swap(head.head_digest, entry, substituted) + assert report.value.reason is workflow_journal.WorkflowJournalReason.AUTHORITY_SUBSTITUTED + assert journal.current_head(_digest("workflow")) == head + with pytest.raises(workflow_journal.WorkflowJournalError) as guard: + workflow_journal.require_selected_authority(head, _authority("substituted"), _digest("s")) + assert guard.value.reason is workflow_journal.WorkflowJournalReason.AUTHORITY_SUBSTITUTED + assert workflow_journal.require_selected_authority( + head, head.retry_authority_ref, head.retry_authority_digest + ) + + +def test_missing_predecessor_or_forked_entries_fail() -> None: + client = _Client() + journal, head = _started(client) + fork = _entry(sequence=1, previous_entry_digest=_digest("orphan-entry")) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.append_and_swap(head.head_digest, fork, workflow_journal.successor_head(head, fork)) + assert report.value.reason is workflow_journal.WorkflowJournalReason.INVALID_SUCCESSOR + collision = _entry(sequence=0, previous_entry_digest=None) + with pytest.raises(workflow_journal.WorkflowJournalError): + journal.append_and_swap( + head.head_digest, collision, workflow_journal.successor_head(head, collision) + ) + skip = _entry(sequence=2, previous_entry_digest=head.entry_digest) + with pytest.raises(workflow_journal.WorkflowJournalError): + journal.append_and_swap(head.head_digest, skip, workflow_journal.successor_head(head, skip)) + assert journal.current_head(_digest("workflow")) == head + + +def test_stale_expected_head_digest_or_provider_conflict_returns_false() -> None: + client = _Client() + journal, head = _started(client) + first = _appended(journal, head, 1) + entry, successor = _successor(head, 1) + assert journal.append_and_swap(head.head_digest, entry, successor) is False + client.force_head_conflict = True + entry2, successor2 = _successor(first, 2) + assert journal.append_and_swap(first.head_digest, entry2, successor2) is False + client.force_head_conflict = False + assert journal.current_head(_digest("workflow")) == first + + +def test_concurrent_append_has_exactly_one_winner() -> None: + client = _Client() + journal, head = _started(client) + client.head_write_barrier = threading.Barrier(2) + entry, successor = _successor(head, 1) + + results = _run_threads(lambda: journal.append_and_swap(head.head_digest, entry, successor)) + client.head_write_barrier = None + assert sorted(str(result) for result in results) == ["False", "True"] + assert journal.current_head(_digest("workflow")) == successor + + +def test_stale_owner_append_and_renewal_are_refused() -> None: + client = _Client() + journal, head = _started(client) + foreign, foreign_head = _successor(head, 1, lease_owner_id="owner-b") + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.append_and_swap(head.head_digest, foreign, foreign_head) + assert report.value.reason is workflow_journal.WorkflowJournalReason.LEASE_OWNER_MISMATCH + assert journal.current_head(_digest("workflow")) == head + + +def test_expired_lease_blocks_owner_append_and_renewal() -> None: + client = _Client() + journal, head = _started(client) + late, late_head = _successor(head, 1, heartbeat_at=_at(31), lease_expires_at=_at(61)) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.append_and_swap(head.head_digest, late, late_head) + assert report.value.reason is workflow_journal.WorkflowJournalReason.LEASE_EXPIRED + assert journal.current_head(_digest("workflow")) == head + + +def test_acquisition_requires_an_expired_validated_head() -> None: + client = _Client() + journal, head = _started(client) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.acquire_expired_lease( + _digest("workflow"), + head.head_digest, + "attempt-2", + "owner-b", + phase=WorkflowPhase.ARM_EXECUTION, + counts=WorkflowCounts(2, 0, 12, 0, 0), + heartbeat_at=_at(1), + lease_expires_at=_at(31), + ) + assert report.value.reason is workflow_journal.WorkflowJournalReason.LEASE_NOT_EXPIRED + with pytest.raises(workflow_journal.WorkflowJournalError) as missing: + journal.acquire_expired_lease( + _digest("unknown-workflow"), + _digest("unknown-head"), + "attempt-2", + "owner-b", + phase=WorkflowPhase.ARM_EXECUTION, + counts=WorkflowCounts(2, 0, 12, 0, 0), + heartbeat_at=_at(31), + lease_expires_at=_at(61), + ) + assert missing.value.reason is workflow_journal.WorkflowJournalReason.NOT_FOUND + + +def test_concurrent_resume_has_exactly_one_winner() -> None: + client = _Client() + journal, head = _started(client) + client.head_write_barrier = threading.Barrier(2) + + def acquire() -> object: + return journal.acquire_expired_lease( + _digest("workflow"), + head.head_digest, + "attempt-2", + "owner-b", + phase=WorkflowPhase.ARM_EXECUTION, + counts=WorkflowCounts(2, 0, 12, 0, 0), + heartbeat_at=_at(31), + lease_expires_at=_at(61), + ) + + winners = [result for result in _run_threads(acquire) if result is not None] + client.head_write_barrier = None + assert len(winners) == 1 + resumed = winners[0] + assert isinstance(resumed, workflow_journal.WorkflowHeadV1) + assert (resumed.lease_owner_id, resumed.sequence) == ("owner-b", 1) + assert resumed.retry_authority_ref == head.retry_authority_ref + assert resumed.retry_authority_digest == head.retry_authority_digest + assert ( + journal.acquire_expired_lease( + _digest("workflow"), + head.head_digest, + "attempt-3", + "owner-c", + phase=WorkflowPhase.SCORING, + counts=WorkflowCounts(2, 0, 12, 0, 0), + heartbeat_at=_at(32), + lease_expires_at=_at(62), + ) + is None + ) + with pytest.raises(workflow_journal.WorkflowJournalError) as live: + journal.acquire_expired_lease( + _digest("workflow"), + resumed.head_digest, + "attempt-3", + "owner-c", + phase=WorkflowPhase.SCORING, + counts=WorkflowCounts(2, 0, 12, 0, 0), + heartbeat_at=_at(32), + lease_expires_at=_at(62), + ) + assert live.value.reason is workflow_journal.WorkflowJournalReason.LEASE_NOT_EXPIRED + current = _appended( + journal, resumed, 2, attempt_id="attempt-2", lease_owner_id="owner-b", + heartbeat_at=_at(45), lease_expires_at=_at(75), + ) + stale = _entry( + sequence=3, + previous_entry_digest=current.entry_digest, + attempt_id="attempt-1", + lease_owner_id="owner-a", + heartbeat_at=_at(46), + lease_expires_at=_at(76), + ) + with pytest.raises(workflow_journal.WorkflowJournalError) as old: + journal.append_and_swap(current.head_digest, stale, workflow_journal.successor_head(current, stale)) + assert old.value.reason is workflow_journal.WorkflowJournalReason.LEASE_OWNER_MISMATCH + + +def test_scratch_loss_recovery_resolves_the_same_selected_authority() -> None: + client = _Client() + journal, head = _started(client) + _appended(journal, head, 1, counts=WorkflowCounts(2, 1, 12, 5, 0)) + recovered = _journal(client) + current = recovered.current_head(_digest("workflow")) + assert current.retry_authority_ref == head.retry_authority_ref + assert current.retry_authority_digest == head.retry_authority_digest + status = recovered.rebuild_status(_digest("workflow"), _at(2)) + assert status.chain_length == 2 + assert status.phase is WorkflowPhase.ARM_EXECUTION + assert status.counts == WorkflowCounts(2, 1, 12, 5, 0) + + +def test_rebuild_ignores_orphan_and_stale_entries() -> None: + client = _Client() + journal, head = _started(client) + _appended(journal, head, 1, counts=WorkflowCounts(2, 1, 12, 5, 0)) + second = _appended( + journal, + journal.current_head(_digest("workflow")), + 2, + counts=WorkflowCounts(2, 2, 12, 8, 0), + ) + orphan = _entry(attempt_id="attempt-9", sequence=9) + client.raw_put( + workflow_journal.workflow_entry_key(_digest("workflow"), orphan.entry_digest), + workflow_journal.journal_entry_bytes(orphan), + ) + status = journal.rebuild_status(_digest("workflow"), _at(3)) + assert status.chain_length == 3 + assert status.counts == WorkflowCounts(2, 2, 12, 8, 0) + assert status.phase is WorkflowPhase.ARM_EXECUTION + assert status.head == second + + +def test_missing_predecessor_in_rebuilt_chain_fails_closed() -> None: + client = _Client() + journal, head = _started(client) + _appended(journal, head, 1) + client.drop(workflow_journal.workflow_entry_key(_digest("workflow"), _initial_entry().entry_digest)) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.rebuild_status(_digest("workflow"), _at(2)) + assert report.value.reason is workflow_journal.WorkflowJournalReason.CHAIN_BROKEN + + +def test_expired_authoritative_head_projects_impaired_stalled_only() -> None: + client = _Client() + journal, head = _started(client) + _appended( + journal, + head, + 1, + heartbeat_at=_at(1), + lease_expires_at=_at(6), + counts=WorkflowCounts(2, 1, 12, 5, 0), + ) + moving = journal.rebuild_status(_digest("workflow"), _at(5)) + assert moving.lease_expired is False + assert moving.projected_status is None + assert moving.projected_reason is None + expired = journal.rebuild_status(_digest("workflow"), _at(6)) + assert expired.lease_expired is True + assert expired.projected_status is WorkflowStatus.IMPAIRED + assert expired.projected_reason is WorkflowReasonCode.STALLED + assert expired.projected_reason is not WorkflowReasonCode.INSTRUMENT_FAILURE + assert expired.counts == WorkflowCounts(2, 1, 12, 5, 0) + + +def test_orphan_or_corrupt_store_state_fails_closed() -> None: + client = _Client() + journal = _journal(client) + with pytest.raises(workflow_journal.WorkflowJournalError) as report: + journal.current_head(_digest("workflow")) + assert report.value.reason is workflow_journal.WorkflowJournalReason.NOT_FOUND + with pytest.raises(workflow_journal.WorkflowJournalError): + journal.current_head("not-a-digest") + _started(client) + client.corrupt_reads = True + with pytest.raises(workflow_journal.WorkflowJournalError) as corrupt: + journal.current_head(_digest("workflow")) + assert corrupt.value.reason is workflow_journal.WorkflowJournalReason.MALFORMED_CONTENT + client.corrupt_reads = False + client.provider_failure = True + with pytest.raises(workflow_journal.WorkflowJournalError) as failed: + journal.current_head(_digest("workflow")) + assert failed.value.reason is workflow_journal.WorkflowJournalReason.STORE_FAILURE