From fddd84c4b33e3d803db27172366bc6942c629aa6 Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Thu, 12 Nov 2026 11:25:24 -0300 Subject: [PATCH 1/2] feat(profile-dev-kit): integrate workflow completes only after Approval (Story 7.6) Story-Key: 7-6-integrate-workflow-completes-only-after-approval --- kits/profile-dev-kit/README.md | 29 ++ kits/profile-dev-kit/kit.yaml | 21 ++ .../python/beddel_profile_dev/integrate.py | 77 +++++ .../beddel_profile_dev/tools/integrate.py | 299 ++++++++++++++++++ kits/profile-dev-kit/tests/test_approval.py | 23 +- kits/profile-dev-kit/tests/test_integrate.py | 102 ++++++ .../tests/test_integrate_workflow.py | 99 ++++++ kits/profile-dev-kit/tests/test_manifest.py | 4 + kits/profile-dev-kit/tests/test_workflow.py | 2 +- .../workflows/integrate-story.yaml | 231 ++++++++++++++ 10 files changed, 885 insertions(+), 2 deletions(-) create mode 100644 kits/profile-dev-kit/python/beddel_profile_dev/integrate.py create mode 100644 kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py create mode 100644 kits/profile-dev-kit/tests/test_integrate.py create mode 100644 kits/profile-dev-kit/tests/test_integrate_workflow.py create mode 100644 kits/profile-dev-kit/workflows/integrate-story.yaml diff --git a/kits/profile-dev-kit/README.md b/kits/profile-dev-kit/README.md index 688aca9..53aa6b2 100644 --- a/kits/profile-dev-kit/README.md +++ b/kits/profile-dev-kit/README.md @@ -493,3 +493,32 @@ never ratify or write inventory versions. ```text beddel run -i ... workflows/verify-and-fix-story.yaml ``` + +## Delivery: integrate stage + +`integrate-story` takes `repo_path`, `story_key`, `spec_path`, `branch`, +`base_branch`, `remote`, `change_id`, `parent_path`, `parent_branch` and an +optional `status_path`. It reads the recorded Approval and the latest +consistent Report, validates the feature branch and parent gitlink, then runs +fetch, land, push, pull request, checks, merge and refetch. The pull request +contains references only: the story key, spec path, Change id and approved +revision. + +The stage refuses before host work for an absent or invalid Approval, a pending +Report, unapproved commits, invalid inputs, a bad parent or a malformed status +file. It confirms the merge from Git after a successful fetch and pins the host +merge to the checked branch tip. A rerun reuses a merged tip and an existing +parent pointer instead of repeating those actions. + +It records one `integrate` outcome at the Change-repository boundary and one at +the parent boundary. Common reasons include `approval.absent`, +`verdict.has_pendency`, `revision.unapproved`, `merge.confirmed`, +`merge.reused`, `pointer.unpublished`, `integrate.complete` and +`fetch.failed`. The status mapping is edited only after a successful parent +fetch confirms the pointer on the parent remote. The workflow never approves, +verifies or pushes the parent; the owner commits the status file and pushes +the parent. + +```text +beddel run -i ... workflows/integrate-story.yaml +``` diff --git a/kits/profile-dev-kit/kit.yaml b/kits/profile-dev-kit/kit.yaml index bb6a16c..614a144 100644 --- a/kits/profile-dev-kit/kit.yaml +++ b/kits/profile-dev-kit/kit.yaml @@ -125,6 +125,18 @@ tools: target: "beddel_profile_dev.tools.fix:profile_fix_settle" description: "Validate and commit one verification fix round" category: profile + - name: profile_integrate_prepare + target: "beddel_profile_dev.tools.integrate:profile_integrate_prepare" + description: "Prepare one approved Change for integration" + category: profile + - name: profile_integrate_reconcile + target: "beddel_profile_dev.tools.integrate:profile_integrate_reconcile" + description: "Reconcile remote integration state" + category: profile + - name: profile_integrate_settle + target: "beddel_profile_dev.tools.integrate:profile_integrate_settle" + description: "Settle the parent integration boundary" + category: profile workflows: - name: verify-change @@ -160,6 +172,9 @@ workflows: - name: plan-story path: workflows/plan-story.yaml description: Plans one selected story into one implementation spec through a Coding agent; never implements, verifies or commits. + - name: integrate-story + path: workflows/integrate-story.yaml + description: Integrates one approved Change through the repository host and records the parent boundary; never approves, verifies or pushes the parent. targets: python: @@ -213,6 +228,12 @@ targets: target: "beddel_profile_dev.tools.fix:profile_fix_reconcile" - name: profile_fix_settle target: "beddel_profile_dev.tools.fix:profile_fix_settle" + - name: profile_integrate_prepare + target: "beddel_profile_dev.tools.integrate:profile_integrate_prepare" + - name: profile_integrate_reconcile + target: "beddel_profile_dev.tools.integrate:profile_integrate_reconcile" + - name: profile_integrate_settle + target: "beddel_profile_dev.tools.integrate:profile_integrate_settle" typescript: status: unavailable unavailable_reason: > diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/integrate.py b/kits/profile-dev-kit/python/beddel_profile_dev/integrate.py new file mode 100644 index 0000000..d4d7e0b --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/integrate.py @@ -0,0 +1,77 @@ +"""Pure helpers for the integration stage.""" + +from __future__ import annotations + +import re + +from beddel_profile_dev.implement import STORY_TRAILER + +APPROVALS_DIR = ".beddel-profile/approvals/" +REMOTE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,63}") +STATUS_VALUE = re.compile(r"[a-z][a-z0-9-]{0,31}") +MAX_STATUS_BYTES = 1048576 +MERGE_TRAILER = "Integrate-Merge" + + +def approved_only(paths: list[str] | tuple[str, ...] | set[str]) -> bool: + """Return whether every changed path belongs to the approval directory.""" + return all(path.startswith(APPROVALS_DIR) for path in paths) + + +def _matching(data: bytes, key: str) -> list[tuple[int, str, str]]: + try: + text = data.decode("utf-8") + except UnicodeDecodeError: + return [] + pattern = re.compile(rf"^[ \t]*{re.escape(key)}:[ \t]*[^\s#]*[ \t]*(#.*)?$") + matches: list[tuple[int, str, str]] = [] + for index, raw in enumerate(text.splitlines(keepends=True)): + line = raw.rstrip("\r\n") + match = pattern.fullmatch(line) + if match is not None: + value_match = re.search(r":([ \t]*)([^\s#]+)", line) + if value_match is not None: + matches.append((index, line, value_match.group(2))) + return matches + + +def status_lines(data: bytes, key: str) -> list[int]: + """Return line indexes containing exactly one valid status mapping.""" + return [index for index, _line, _value in _matching(data, key)] + + +def status_value(data: bytes, key: str) -> str | None: + """Return the status value when the requested key occurs exactly once.""" + matches = _matching(data, key) + return matches[0][2] if len(matches) == 1 else None + + +def set_status(data: bytes, key: str, value: str) -> bytes | None: + """Replace one status token without changing any other file bytes.""" + matches = _matching(data, key) + if len(matches) != 1: + return None + try: + text = data.decode("utf-8") + except UnicodeDecodeError: + return None + lines = text.splitlines(keepends=True) + index, line, _old = matches[0] + match = re.match( + rf"^(?P[ \t]*{re.escape(key)}:[ \t]*)[^\s#]+(?P.*)$", line + ) + if match is None or not STATUS_VALUE.fullmatch(value): + return None + lines[index] = ( + f"{match.group('prefix')}{value}{match.group('suffix')}" + + lines[index][len(line) :] + ) + return "".join(lines).encode("utf-8") + + +def pointer_message(story_key: str, sub_path: str, merge: str) -> tuple[str, str]: + """Return the pointer commit subject and its identity trailers.""" + return ( + f"chore: update {sub_path} pointer", + f"{STORY_TRAILER}: {story_key}\n{MERGE_TRAILER}: {merge}", + ) diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py new file mode 100644 index 0000000..6f13c57 --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py @@ -0,0 +1,299 @@ +# fmt: off +# ruff: noqa: I001 +from __future__ import annotations + +import os +import tempfile +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +from beddel.tools import beddel_tool + +from beddel_profile_dev._errors import ProfileError +from beddel_profile_dev._io import changed_paths, confined, footprint, git_run, is_ancestor, read_confined, real_repo_root, resolve_revision +from beddel_profile_dev.integrate import MAX_STATUS_BYTES, REMOTE, STATUS_VALUE, approved_only, pointer_message, set_status, status_lines, status_value +from beddel_profile_dev.inventory import is_valid_id +from beddel_profile_dev.plan import relative_path +from beddel_profile_dev.implement import _BRANCH +from beddel_profile_dev.tools.approval import profile_read_approval +from beddel_profile_dev.tools.implement import _git_text, _repo_error, _symbolic_branch, _valid_story +from beddel_profile_dev.tools.implement import branch_reason +from beddel_profile_dev.tools.report import profile_read_report + + +_REF_NAMES = ("boundary", "spec_path", "branch", "base_branch", "change_id", "revision", "tip", "approval", "report", "pr", "merge", "sub_path", "pointer", "status_path", "status_written") + + +def _refs(**values: Any) -> dict[str, Any]: + return {name: values.get(name, False if name == "status_written" else None) for name in _REF_NAMES} + + +def _state(story: str | None, outcome: str | None, reason: str | None, refs: Mapping[str, Any] | None = None, **values: Any) -> dict[str, Any]: + data = {"stage": "integrate", "phase": values.get("phase"), "story_key": story, "change_id": values.get("change_id"), "outcome": outcome, "reason": reason, "refs": dict(refs or _refs()), "pr": values.get("pr", False), "merged": values.get("merged", False), "repo": values.get("repo"), "spec_path": values.get("spec_path"), "branch": values.get("branch"), "base_branch": values.get("base_branch"), "remote": values.get("remote"), "parent": values.get("parent"), "parent_branch": values.get("parent_branch"), "sub_path": values.get("sub_path"), "status_path": values.get("status_path"), "status_value": values.get("status_value"), "revision": values.get("revision"), "tip": values.get("tip"), "merge": values.get("merge"), "pointer": values.get("pointer")} + return data + + +def _fail(story: str | None, reason: str, refs: Mapping[str, Any] | None = None, **values: Any) -> dict[str, Any]: + values["remote"] = None + return _state(story, "failed", reason, refs, **values) + + +def _refuse(story: str | None, reason: str, refs: Mapping[str, Any] | None = None, **values: Any) -> dict[str, Any]: + values["remote"] = None + return _state(story, "refused", reason, refs, **values) + + +def _repository(repo_path: str) -> Path: + return real_repo_root(repo_path) + + +def _remotes(repo: Path) -> set[str]: + result = git_run(repo, "remote") + if result is None or result.returncode != 0: + raise _repo_error() + return {line for line in result.stdout.decode("utf-8", "replace").splitlines() if line} + + +def _ref_exists(repo: Path, ref: str) -> bool: + result = git_run(repo, "show-ref", "--verify", "--quiet", ref) + if result is None or result.returncode not in (0, 1): + raise _repo_error() + return result.returncode == 0 + + +def _gitlink(repo: Path, tree: str, sub_path: str) -> str | None: + result = git_run(repo, "ls-tree", "-z", tree, "--", sub_path) + if result is None or result.returncode != 0: + raise _repo_error() + entries = [item for item in result.stdout.split(b"\x00") if item] + if len(entries) != 1 or b"\t" not in entries[0]: + return None + mode_oid, path = entries[0].split(b"\t", 1) + fields = mode_oid.decode("ascii", "replace").split() + if len(fields) != 2 or fields[0] != "160000" or path.decode("utf-8", "replace") != sub_path: + return None + return fields[1] + + +def _valid_inputs(story_key: object, spec_path: object, branch: object, base_branch: object, remote: object, change_id: object, parent_path: object, parent_branch: object, status_path: object, status: object) -> tuple[str, str, str, str, str, str, str, str, str] | dict[str, Any]: + story = story_key if isinstance(story_key, str) and _valid_story(story_key) else None + spec = spec_path if isinstance(spec_path, str) and len(spec_path) <= 512 and relative_path(spec_path) else None + if story is None: return _refuse(None, "story_key.invalid", _refs(spec_path=spec)) + if spec is None: return _refuse(story, "path.invalid", _refs()) + if not isinstance(branch, str) or _BRANCH.fullmatch(branch) is None: return _refuse(story, "branch.invalid", _refs(spec_path=spec)) + if not isinstance(base_branch, str) or _BRANCH.fullmatch(base_branch) is None: return _refuse(story, "base.invalid", _refs(spec_path=spec, branch=branch)) + reason = branch_reason(branch, base_branch) + if reason is not None: return _refuse(story, reason, _refs(spec_path=spec, branch=branch, base_branch=base_branch)) + if not isinstance(parent_branch, str) or _BRANCH.fullmatch(parent_branch) is None: return _refuse(story, "base.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch)) + if not isinstance(remote, str) or REMOTE.fullmatch(remote) is None: return _refuse(story, "remote.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch)) + if not isinstance(change_id, str) or not is_valid_id(change_id): return _refuse(story, "change_id.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch)) + if not isinstance(parent_path, str) or not parent_path.strip(): return _refuse(story, "path.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch, change_id=change_id)) + if not isinstance(status_path, str) or len(status_path) > 512 or (status_path and not relative_path(status_path)): return _refuse(story, "path.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch, change_id=change_id)) + if not isinstance(status, str) or STATUS_VALUE.fullmatch(status) is None: return _refuse(story, "status.invalid", _refs(spec_path=spec, branch=branch, base_branch=base_branch, change_id=change_id)) + return story, spec, branch, base_branch, remote, change_id, parent_branch, status_path, status + + +def _with(prior: Mapping[str, Any], *, outcome: str | None = None, reason: str | None = None, refs: Mapping[str, Any] | None = None, **values: Any) -> dict[str, Any]: + data = dict(prior) + data.update(values) + data["outcome"] = outcome + data["reason"] = reason + prior_refs_object = prior.get("refs") + prior_refs: dict[str, Any] = dict(prior_refs_object) if isinstance(prior_refs_object, Mapping) else {} + merged_refs = dict(prior_refs) + merged_refs.update(dict(refs or {})) + data["refs"] = _refs(**merged_refs) + return data + + +def _prior_check(repo: Path, story: str, change: str, prior: object, phase: str) -> dict[str, Any] | None: + if prior is None: return _fail(story, "reconcile.failed" if phase == "confirm" or phase == "land" else "pointer.failed", _refs(change_id=change), change_id=change, phase=phase, repo=str(repo)) + if not isinstance(prior, Mapping) or prior.get("stage") != "integrate" or prior.get("story_key") != story or prior.get("change_id") != change or os.path.realpath(str(prior.get("repo"))) != str(repo): + return _fail(story, "state.mismatch", _refs(change_id=change), change_id=change, phase=phase, repo=str(repo)) + return None + + +def _fetch_check(fetch: object, story: str, change: str, repo: Path, phase: str) -> dict[str, Any] | None: + if not isinstance(fetch, Mapping) or not fetch: + return _fail(story, "fetch.failed", _refs(change_id=change), change_id=change, phase=phase, repo=str(repo)) + return None + + +def _tracking(repo: Path, remote: str, branch: str) -> str: + return resolve_revision(repo, f"refs/remotes/{remote}/{branch}") + + +def _merge_commit(repo: Path, tip: str, tracking: str) -> str | None: + if tip == tracking: return tip + result = git_run(repo, "rev-list", "--first-parent", "--ancestry-path", "--reverse", f"{tip}..{tracking}") + if result is None or result.returncode != 0: raise _repo_error() + first = result.stdout.decode("ascii", "replace").splitlines() + if not first: return None + candidate = first[0].strip() + parent = _git_text(git_run(repo, "rev-parse", f"{candidate}^1")) + if parent == tip: return tip + return candidate if is_ancestor(repo, tip, candidate) else None + + +def _host_ok(value: object) -> bool: + return isinstance(value, Mapping) and bool(value) and not value.get("error") + + +@beddel_tool(name="profile_integrate_prepare", description="Prepare one approved Change for integration", category="profile") +def profile_integrate_prepare(repo_path: str, story_key: str, spec_path: str, branch: str, base_branch: str, remote: str, change_id: str, parent_path: str, parent_branch: str, status_path: str, status_value: str) -> dict[str, Any]: + checked = _valid_inputs(story_key, spec_path, branch, base_branch, remote, change_id, parent_path, parent_branch, status_path, status_value) + if isinstance(checked, dict): return checked + story, spec, branch, base, remote, change, pbranch, status, value = checked + repo = _repository(repo_path) + try: + if remote not in _remotes(repo): return _refuse(story, "remote.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), story_key=story, change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, parent_branch=pbranch, status_path=status, status_value=value) + if not _ref_exists(repo, f"refs/heads/{branch}"): return _refuse(story, "branch.absent", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value) + tip = resolve_revision(repo, f"refs/heads/{branch}") + dirty = changed_paths(repo) - {spec} + if dirty: return _fail(story, "worktree.dirty", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + try: approval_result = profile_read_approval(str(repo), change) + except ProfileError: return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + approval = approval_result.get("approval") if isinstance(approval_result, Mapping) else None + if approval is None: return _refuse(story, "approval.absent", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + if not isinstance(approval, Mapping): return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + revision = approval.get("revision") + if not isinstance(revision, str): return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + try: approved = resolve_revision(repo, revision) + except ProfileError: return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip) + try: report_result = profile_read_report(str(repo), change) + except ProfileError: return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, tip=tip, revision=approved) + report = report_result.get("report") if isinstance(report_result, Mapping) else None + if not isinstance(report, Mapping): return _refuse(story, "approval.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) + if report.get("revision") != approved: return _refuse(story, "report.superseded", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) + if report.get("verdict") != "consistent": return _refuse(story, "verdict.has_pendency", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) + if not is_ancestor(repo, approved, tip) or not approved_only(footprint(repo, approved, tip)): return _refuse(story, "revision.unapproved", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) + parent = _repository(parent_path) + try: sub_path = Path(os.path.relpath(repo, parent)).as_posix() + except ValueError: sub_path = "" + if not relative_path(sub_path) or _gitlink(parent, "HEAD", sub_path) is None: return _refuse(story, "parent.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + if _symbolic_branch(parent) != pbranch: return _refuse(story, "parent.not_current", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + if remote not in _remotes(parent): return _refuse(story, "remote.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + if status: + data = read_confined(parent, status) + if data is None: return _refuse(story, "status.absent", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path, status_path=status), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + if len(data) > MAX_STATUS_BYTES or len(status_lines(data, story)) != 1: return _refuse(story, "status.unmatched", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path, status_path=status), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + proof_value = approval.get("proof") + proof: Mapping[str, Any] = proof_value if isinstance(proof_value, Mapping) else {} + refs = _refs(boundary="repository", spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, report=approval.get("report"), sub_path=sub_path, status_path=status, approval=proof.get("commit")) + return _state(story, None, None, refs, phase="prepare", change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) + except ProfileError: + return _fail(story, "prepare.failed", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=None, parent_branch=pbranch, status_path=status, status_value=value) + except Exception: # noqa: BLE001 + return _fail(story, "prepare.failed", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=None, parent_branch=pbranch, status_path=status, status_value=value) + + +@beddel_tool(name="profile_integrate_reconcile", description="Reconcile remote integration state", category="profile") +def profile_integrate_reconcile(repo_path: str, story_key: str, change_id: str, phase: str, prior: Mapping[str, Any] | None, fetch: Mapping[str, Any] | None, push: Mapping[str, Any] | None = None, pull_request: Mapping[str, Any] | None = None, checks: Mapping[str, Any] | None = None, merge: Mapping[str, Any] | None = None) -> dict[str, Any]: + story = story_key if isinstance(story_key, str) and _valid_story(story_key) else None + if story is None or not isinstance(change_id, str) or not is_valid_id(change_id) or phase not in ("land", "confirm"): return _fail(story, "reconcile.failed", _refs(change_id=change_id), change_id=change_id, phase=phase) + repo = _repository(repo_path) + mismatch = _prior_check(repo, story, change_id, prior, phase) + if mismatch is not None: return mismatch + assert isinstance(prior, Mapping) + if prior.get("outcome") is not None: return dict(prior) + bad_fetch = _fetch_check(fetch, story, change_id, repo, phase) + if bad_fetch is not None: return bad_fetch + try: + branch, remote, base = prior.get("branch"), prior.get("remote"), prior.get("base_branch") + tip = resolve_revision(repo, f"refs/heads/{branch}") + if tip != prior.get("tip"): return _fail(story, "head.moved", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase, tip=tip) + tracking = _tracking(repo, str(remote), str(base)) + if phase == "land": + if is_ancestor(repo, tip, tracking): return _with(prior, merged=True, pr=False, phase="land", tip=tip) + if _symbolic_branch(repo) != branch: return _fail(story, "branch.not_current", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="land", tip=tip, pr=False, merged=False) + return _with(prior, phase="land", pr=True, merged=False, tip=tip) + contained = is_ancestor(repo, tip, tracking) + if contained: + merge_sha = _merge_commit(repo, tip, tracking) + if merge_sha is None: return _fail(story, "merge.unconfirmed", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) + if isinstance(merge, Mapping): + result = merge.get("result") + reported = result.get("merge_sha") if isinstance(result, Mapping) else None + if reported != merge_sha: return _fail(story, "merge.mismatch", _refs(**dict(prior.get("refs", {})), tip=tip, merge=merge_sha), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip, merge=merge_sha) + number = pull_request.get("result", {}).get("number") if isinstance(pull_request, Mapping) and isinstance(pull_request.get("result"), Mapping) else None + refs = _refs(**dict(prior.get("refs", {})), boundary="repository", tip=tip, merge=merge_sha, pr=number) + return _with(prior, outcome="succeeded", reason="merge.confirmed" if prior.get("pr") else "merge.reused", refs=refs, phase="confirm", tip=tip, merge=merge_sha, merged=True) + if prior.get("pr") is True: + for item, reason in ((push, "push.failed"), (pull_request, "pr.failed"), (checks, "checks.failed"), (merge, "merge.failed")): + if not _host_ok(item): return _fail(story, reason, _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) + return _fail(story, "merge.unconfirmed", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) + except ProfileError: + return _fail(story, "reconcile.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase) + except Exception: # noqa: BLE001 + return _fail(story, "reconcile.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase) + + +@beddel_tool(name="profile_integrate_settle", description="Settle the parent integration boundary", category="profile") +def profile_integrate_settle(repo_path: str, story_key: str, change_id: str, phase: str, prior: Mapping[str, Any] | None, fetch: Mapping[str, Any] | None = None) -> dict[str, Any]: + story = story_key if isinstance(story_key, str) and _valid_story(story_key) else None + if story is None or not isinstance(change_id, str) or not is_valid_id(change_id) or phase not in ("pointer", "finish"): return _fail(story, "pointer.failed", _refs(change_id=change_id), change_id=change_id, phase=phase) + repo = _repository(repo_path) + mismatch = _prior_check(repo, story, change_id, prior, phase) + if mismatch is not None: return mismatch + assert isinstance(prior, Mapping) + if phase == "finish": + bad_fetch = _fetch_check(fetch, story, change_id, repo, phase) + if bad_fetch is not None: return bad_fetch + try: + if phase == "pointer" and (prior.get("phase") != "confirm" or prior.get("merged") is not True): return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if phase == "finish" and prior.get("phase") != "pointer": return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + parent = _repository(str(prior.get("parent"))) + sub_path, merge = prior.get("sub_path"), prior.get("merge") + if not isinstance(sub_path, str) or not relative_path(sub_path) or not isinstance(merge, str): return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "pointer")}, phase=phase) + if phase == "pointer": + parent_head = resolve_revision(parent, "HEAD") + current = _gitlink(parent, "HEAD", sub_path) + reused = current == merge + if not reused and current is not None: + try: reused = is_ancestor(repo, merge, resolve_revision(repo, current)) + except ProfileError: reused = False + if reused: return _with(prior, phase="pointer", pointer=parent_head, refs=_refs(**dict(prior.get("refs", {})), boundary="parent", pointer=parent_head, sub_path=sub_path)) + if _symbolic_branch(parent) != prior.get("parent_branch"): return _fail(story, "parent.not_current", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if changed_paths(repo) - {prior.get("spec_path")}: return _fail(story, "worktree.dirty", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if resolve_revision(repo, "HEAD") != merge: + switched = git_run(repo, "switch", "-q", "--detach", merge) + if switched is None or switched.returncode != 0: return _fail(story, "checkout.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if resolve_revision(repo, "HEAD") != merge: return _fail(story, "checkout.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + old = parent_head + subject, trailers = pointer_message(story, sub_path, merge) + committed = git_run(parent, "commit", "-q", "-m", subject, "-m", trailers, "--only", "--", sub_path, timeout=120) + if committed is None or committed.returncode != 0: return _fail(story, "commit.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + pointer = resolve_revision(parent, "HEAD") + if _git_text(git_run(parent, "rev-parse", "HEAD^")) != old or _gitlink(parent, "HEAD", sub_path) != merge: return _fail(story, "commit.changed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=pointer), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer", pointer=pointer) + return _with(prior, phase="pointer", pointer=pointer, refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=pointer)) + tracking = _tracking(parent, str(prior.get("remote")), str(prior.get("parent_branch"))) + remote_gitlink = _gitlink(parent, tracking, sub_path) + published = remote_gitlink == merge + if not published and remote_gitlink is not None: + try: published = is_ancestor(repo, merge, resolve_revision(repo, remote_gitlink)) + except ProfileError: published = False + if not published: return _with(prior, outcome="stopped", reason="pointer.unpublished", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), pointer=prior.get("pointer")) + status = prior.get("status_path") + if not status: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) + data = read_confined(parent, str(status)) + if data is None: return _fail(story, "status.unmatched", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + value = str(prior.get("status_value")) + if status_value(data, story) == value: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) + updated = set_status(data, story, value) + if updated is None: return _fail(story, "status.unmatched", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + target = confined(parent, str(status)) + fd, temporary = tempfile.mkstemp(prefix=".beddel-status-", dir=str(target.parent)) + try: + with os.fdopen(fd, "wb") as handle: handle.write(updated) + os.replace(temporary, target) + except OSError: + try: os.unlink(temporary) + except OSError: pass + return _fail(story, "status.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=True), pointer=prior.get("pointer")) + except ProfileError: + return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase=phase) + except Exception: # noqa: BLE001 + return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase=phase) +# fmt: on diff --git a/kits/profile-dev-kit/tests/test_approval.py b/kits/profile-dev-kit/tests/test_approval.py index b6be662..9724044 100644 --- a/kits/profile-dev-kit/tests/test_approval.py +++ b/kits/profile-dev-kit/tests/test_approval.py @@ -211,7 +211,12 @@ def test_underivable_is_a_refusal( def test_structural_approval_boundaries() -> None: for path in PYTHON_ROOT.rglob("*.py"): - if path.name not in {"approval.py", "approval_decision.py", "_errors.py"}: + if path.name not in { + "approval.py", + "approval_decision.py", + "_errors.py", + "integrate.py", + }: assert "approv" not in path.read_text().lower() source = (PYTHON_ROOT / "beddel_profile_dev" / "tools" / "approval.py").read_text() assert source.count("write_create_only(") == 1 @@ -262,6 +267,22 @@ def test_structural_approval_boundaries() -> None: ) assert "profile-dev/approval/1" in source assert "approval.json" in source + for integrate_source in ( + PYTHON_ROOT / "beddel_profile_dev" / "integrate.py", + PYTHON_ROOT / "beddel_profile_dev" / "tools" / "integrate.py", + ): + integrate_text = integrate_source.read_text() + assert all( + forbidden not in integrate_text + for forbidden in ( + "profile_approve_change", + "profile_record_approval_decision", + "approve-change", + "record-approval-decision", + "write_create_only", + "approval_decision", + ) + ) decision_source = ( PYTHON_ROOT / "beddel_profile_dev" / "tools" / "approval_decision.py" ).read_text() diff --git a/kits/profile-dev-kit/tests/test_integrate.py b/kits/profile-dev-kit/tests/test_integrate.py new file mode 100644 index 0000000..c2c2019 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_integrate.py @@ -0,0 +1,102 @@ +"""Contract tests for the integration helpers and tool boundaries.""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest +from beddel_profile_dev import integrate +from beddel_profile_dev.tools import integrate as tools + +from ._helpers import commit_all, git, init_repo + +STORY = "7-6-integrate-workflow-completes-only-after-approval" + + +def test_status_helpers_preserve_bytes_and_comments() -> None: + data = b" other: keep\n\t" + STORY.encode() + b": pending # owner\nlast: keep\r\n" + assert integrate.status_lines(data, STORY) == [1] + assert integrate.status_value(data, STORY) == "pending" + updated = integrate.set_status(data, STORY, "done") + assert ( + updated + == b" other: keep\n\t" + STORY.encode() + b": done # owner\nlast: keep\r\n" + ) + assert integrate.set_status(data, "missing", "done") is None + assert integrate.status_lines(data + STORY.encode() + b": another\n", STORY) == [ + 1, + 3, + ] + assert ( + integrate.set_status(data + STORY.encode() + b": another\n", STORY, "done") + is None + ) + + +def test_approved_paths_and_pointer_message() -> None: + assert integrate.approved_only([".beddel-profile/approvals/c1/1.yaml"]) + assert integrate.approved_only([]) + assert not integrate.approved_only(["src/code.py"]) + subject, trailers = integrate.pointer_message(STORY, "packages/example", "a" * 40) + assert subject == "chore: update packages/example pointer" + assert trailers == f"Story-Key: {STORY}\nIntegrate-Merge: {'a' * 40}" + + +def test_prepare_rejects_bad_inputs_before_repository_access(tmp_path: Path) -> None: + result = tools.profile_integrate_prepare( + str(tmp_path / "missing"), + "bad key!", + "spec.md", + "feature/work", + "main", + "origin", + "c1", + str(tmp_path / "parent"), + "main", + "status.yaml", + "done", + ) + assert result["outcome"] == "refused" + assert result["reason"] == "story_key.invalid" + + +def test_reconcile_rejects_missing_fetch_and_forged_state(tmp_path: Path) -> None: + repo = init_repo(tmp_path / "repo") + git(repo, "config", "user.name", "Fixture") + git(repo, "config", "user.email", "fixture@example.invalid") + (repo / "file").write_text("x\n") + commit_all(repo) + prior: dict[str, Any] = { + "stage": "integrate", + "phase": "land", + "story_key": STORY, + "change_id": "c1", + "repo": str(repo), + "branch": "feature/work", + "base_branch": "main", + "remote": "origin", + "tip": git(repo, "rev-parse", "HEAD"), + "refs": {}, + "outcome": None, + } + missing = tools.profile_integrate_reconcile( + str(repo), STORY, "c1", "land", prior, None + ) + assert missing["reason"] == "fetch.failed" + forged = tools.profile_integrate_reconcile( + str(repo), STORY, "other", "land", prior, {"ok": True} + ) + assert forged["reason"] == "state.mismatch" + + +def test_settle_rejects_missing_prior(tmp_path: Path) -> None: + repo = init_repo(tmp_path / "repo") + result = tools.profile_integrate_settle(str(repo), STORY, "c1", "pointer", None) + assert result["outcome"] == "failed" + assert result["reason"] == "pointer.failed" + + +@pytest.mark.parametrize("value", ["Done", "done now", "", "x" * 33]) +def test_status_value_contract(value: str) -> None: + assert integrate.STATUS_VALUE.fullmatch(value) is None diff --git a/kits/profile-dev-kit/tests/test_integrate_workflow.py b/kits/profile-dev-kit/tests/test_integrate_workflow.py new file mode 100644 index 0000000..f998e0b --- /dev/null +++ b/kits/profile-dev-kit/tests/test_integrate_workflow.py @@ -0,0 +1,99 @@ +"""Shape and safety checks for the integration entry workflow.""" + +from __future__ import annotations + +from pathlib import Path + +import yaml +from beddel.domain.models import StrategyType +from beddel.domain.parser import WorkflowParser + +PATH = Path(__file__).resolve().parents[1] / "workflows" / "integrate-story.yaml" + + +def test_integrate_workflow_shape() -> None: + document = yaml.safe_load(PATH.read_text()) + assert document["id"] == "integrate-story" + assert document["requires_kits"] == [ + "profile-dev-kit", + "tools-git-kit", + "tools-github-kit", + ] + assert document["allowed_tools"] == [ + "profile_integrate_prepare", + "profile_integrate_reconcile", + "profile_integrate_settle", + "profile_record_stage_outcome", + "git_fetch", + "git_push", + "github_pr_create_or_reuse", + "github_pr_checks_wait", + "github_pr_merge", + ] + assert document["inputs"] == [ + "repo_path", + "story_key", + "spec_path", + "branch", + "base_branch", + "remote", + "change_id", + "parent_path", + "parent_branch", + "status_path", + ] + assert [step["id"] for step in document["steps"]] == [ + "prepare", + "fetch", + "land", + "push", + "pull_request", + "checks", + "merge", + "refetch", + "confirm", + "confirm_reuse", + "record_repo", + "pointer", + "fetch_parent", + "finish", + "record_parent", + ] + assert ( + sum(step["config"].get("tool") == "git_push" for step in document["steps"]) == 1 + ) + push = next(step for step in document["steps"] if step["id"] == "push") + assert "force" not in push["config"]["arguments"] + assert "set_upstream" not in push["config"]["arguments"] + request = next(step for step in document["steps"] if step["id"] == "pull_request") + assert set(request["config"]["arguments"]) == { + "cwd", + "branch", + "base", + "story_key", + "spec_path", + "change_id", + "revision", + } + assert ( + next(step for step in document["steps"] if step["id"] == "merge")["config"][ + "arguments" + ]["merge_method"] + == "merge" + ) + assert {step.get("output_key") for step in document["steps"]} >= {"confirmed"} + + +def test_integrate_workflow_parses_and_conditions_pair() -> None: + workflow = WorkflowParser.parse(PATH.read_text()) + steps = {step.id: step for step in workflow.steps} + assert steps["confirm"].if_condition == "$stepResult.land.result.pr == true" + assert steps["confirm_reuse"].if_condition == "$stepResult.land.result.pr != true" + assert ( + steps["confirm"].config["timeout"] == 2000 + if "timeout" in steps["confirm"].config + else True + ) + assert steps["confirm"].execution_strategy.type is StrategyType.FAIL + assert steps["confirm_reuse"].execution_strategy.type is StrategyType.FAIL + assert steps["confirm"].config["tool"] == "profile_integrate_reconcile" diff --git a/kits/profile-dev-kit/tests/test_manifest.py b/kits/profile-dev-kit/tests/test_manifest.py index 338bb6e..dcab2f3 100644 --- a/kits/profile-dev-kit/tests/test_manifest.py +++ b/kits/profile-dev-kit/tests/test_manifest.py @@ -37,6 +37,9 @@ "profile_implement_settle", "profile_fix_reconcile", "profile_fix_settle", + "profile_integrate_prepare", + "profile_integrate_reconcile", + "profile_integrate_settle", ] @@ -113,6 +116,7 @@ def test_manifest_declares_all_workflows_and_python_targets() -> None: "implement-story", "verify-and-fix-story", "plan-story", + "integrate-story", ] declared = {t["name"] for t in manifest["tools"]} targeted = {t["name"] for t in manifest["targets"]["python"]["tools"]} diff --git a/kits/profile-dev-kit/tests/test_workflow.py b/kits/profile-dev-kit/tests/test_workflow.py index 0c4d565..40dbf41 100644 --- a/kits/profile-dev-kit/tests/test_workflow.py +++ b/kits/profile-dev-kit/tests/test_workflow.py @@ -113,7 +113,7 @@ def test_allowlist_equals_the_tools_the_steps_name() -> None: "profile_capture_evidence", "profile_record_report", ] - assert len(list(WORKFLOWS_DIR.glob("*.yaml"))) == 11 + assert len(list(WORKFLOWS_DIR.glob("*.yaml"))) == 12 def test_review_step_outlives_the_largest_accepted_review_budget() -> None: diff --git a/kits/profile-dev-kit/workflows/integrate-story.yaml b/kits/profile-dev-kit/workflows/integrate-story.yaml new file mode 100644 index 0000000..89a0661 --- /dev/null +++ b/kits/profile-dev-kit/workflows/integrate-story.yaml @@ -0,0 +1,231 @@ +id: integrate-story +name: Integrate Story +description: Entry integration stage that reads the recorded Approval, pushes the feature branch, creates or reuses a pull request, waits for checks, merges through the repository tools, confirms the merge by fetch, records the parent pointer and records the status after the pointer reaches the parent remote. It never approves, verifies or pushes the parent. +version: "1.0" +requires_kits: + - profile-dev-kit + - tools-git-kit + - tools-github-kit +allowed_tools: + - profile_integrate_prepare + - profile_integrate_reconcile + - profile_integrate_settle + - profile_record_stage_outcome + - git_fetch + - git_push + - github_pr_create_or_reuse + - github_pr_checks_wait + - github_pr_merge +inputs: + - repo_path + - story_key + - spec_path + - branch + - base_branch + - remote + - change_id + - parent_path + - parent_branch + - status_path +steps: + - id: prepare + primitive: tool + execution_strategy: + type: skip + config: + tool: profile_integrate_prepare + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + spec_path: "$input.spec_path" + branch: "$input.branch" + base_branch: "$input.base_branch" + remote: "$input.remote" + change_id: "$input.change_id" + parent_path: "$input.parent_path" + parent_branch: "$input.parent_branch" + status_path: "$input.status_path" + status_value: done + - id: fetch + primitive: tool + execution_strategy: + type: skip + config: + tool: git_fetch + timeout: 330 + arguments: + cwd: "$input.repo_path" + remote: "$stepResult.prepare.result.remote" + - id: land + primitive: tool + execution_strategy: + type: fail + config: + tool: profile_integrate_reconcile + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + change_id: "$input.change_id" + phase: land + prior: "$stepResult.prepare.result" + fetch: "$stepResult.fetch" + - id: push + primitive: tool + if: "$stepResult.land.result.pr == true" + execution_strategy: + type: skip + config: + tool: git_push + timeout: 330 + arguments: + cwd: "$input.repo_path" + remote: "$stepResult.land.result.remote" + - id: pull_request + primitive: tool + if: "$stepResult.land.result.pr == true" + execution_strategy: + type: skip + config: + tool: github_pr_create_or_reuse + timeout: 400 + arguments: + cwd: "$input.repo_path" + branch: "$stepResult.land.result.branch" + base: "$stepResult.land.result.base_branch" + story_key: "$stepResult.land.result.story_key" + spec_path: "$stepResult.land.result.spec_path" + change_id: "$stepResult.land.result.change_id" + revision: "$stepResult.land.result.revision" + - id: checks + primitive: tool + if: "$stepResult.land.result.pr == true" + execution_strategy: + type: skip + config: + tool: github_pr_checks_wait + timeout: 2000 + arguments: + cwd: "$input.repo_path" + number: "$stepResult.pull_request.result.number" + - id: merge + primitive: tool + if: "$stepResult.land.result.pr == true" + execution_strategy: + type: skip + config: + tool: github_pr_merge + timeout: 400 + arguments: + cwd: "$input.repo_path" + number: "$stepResult.pull_request.result.number" + merge_method: merge + expected_head_sha: "$stepResult.land.result.tip" + - id: refetch + primitive: tool + execution_strategy: + type: skip + config: + tool: git_fetch + timeout: 330 + arguments: + cwd: "$input.repo_path" + remote: "$stepResult.land.result.remote" + - id: confirm + primitive: tool + if: "$stepResult.land.result.pr == true" + execution_strategy: + type: fail + output_key: confirmed + config: + tool: profile_integrate_reconcile + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + change_id: "$input.change_id" + phase: confirm + prior: "$stepResult.land.result" + fetch: "$stepResult.refetch" + push: "$stepResult.push" + pull_request: "$stepResult.pull_request" + checks: "$stepResult.checks" + merge: "$stepResult.merge" + - id: confirm_reuse + primitive: tool + if: "$stepResult.land.result.pr != true" + execution_strategy: + type: fail + output_key: confirmed + config: + tool: profile_integrate_reconcile + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + change_id: "$input.change_id" + phase: confirm + prior: "$stepResult.land.result" + fetch: "$stepResult.refetch" + - id: record_repo + primitive: tool + execution_strategy: + type: fail + config: + tool: profile_record_stage_outcome + arguments: + repo_path: "$input.repo_path" + story_key: "$stepResult.confirmed.result.story_key" + stage: integrate + outcome: "$stepResult.confirmed.result.outcome" + reason: "$stepResult.confirmed.result.reason" + refs: "$stepResult.confirmed.result.refs" + - id: pointer + primitive: tool + if: "$stepResult.confirmed.result.merged == true" + execution_strategy: + type: fail + config: + tool: profile_integrate_settle + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + change_id: "$input.change_id" + phase: pointer + prior: "$stepResult.confirmed.result" + - id: fetch_parent + primitive: tool + if: "$stepResult.confirmed.result.merged == true" + execution_strategy: + type: skip + config: + tool: git_fetch + timeout: 330 + arguments: + cwd: "$input.parent_path" + remote: "$stepResult.pointer.result.remote" + - id: finish + primitive: tool + if: "$stepResult.confirmed.result.merged == true" + execution_strategy: + type: fail + config: + tool: profile_integrate_settle + arguments: + repo_path: "$input.repo_path" + story_key: "$input.story_key" + change_id: "$input.change_id" + phase: finish + prior: "$stepResult.pointer.result" + fetch: "$stepResult.fetch_parent" + - id: record_parent + primitive: tool + if: "$stepResult.confirmed.result.merged == true" + execution_strategy: + type: fail + config: + tool: profile_record_stage_outcome + arguments: + repo_path: "$input.repo_path" + story_key: "$stepResult.finish.result.story_key" + stage: integrate + outcome: "$stepResult.finish.result.outcome" + reason: "$stepResult.finish.result.reason" + refs: "$stepResult.finish.result.refs" From 856cbf79cdec1391ad989c1c682aba75563b218c Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Thu, 12 Nov 2026 11:44:39 -0300 Subject: [PATCH 2/2] fix(profile-dev-kit): repair integrate gitlink read and state carry, add workflow runs (Story 7.6) Story-Key: 7-6-integrate-workflow-completes-only-after-approval --- .../beddel_profile_dev/tools/integrate.py | 99 ++++--- .../tests/test_integrate_workflow.py | 245 +++++++++++++++++- 2 files changed, 304 insertions(+), 40 deletions(-) diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py index 6f13c57..0816641 100644 --- a/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/integrate.py @@ -3,6 +3,7 @@ from __future__ import annotations import os +import stat import tempfile from collections.abc import Mapping from pathlib import Path @@ -29,6 +30,23 @@ def _refs(**values: Any) -> dict[str, Any]: return {name: values.get(name, False if name == "status_written" else None) for name in _REF_NAMES} +_CARRY_FIELDS = ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision") + + +def _carry(prior: Mapping[str, Any]) -> dict[str, Any]: + return {key: prior.get(key) for key in _CARRY_FIELDS} + + +def _carry_refs(prior: Mapping[str, Any], **updates: Any) -> dict[str, Any]: + refs = dict(prior.get("refs") or {}) if isinstance(prior.get("refs"), Mapping) else {} + refs.update(updates) + return _refs(**refs) + + +def _forwarded(prior: object) -> dict[str, Any] | None: + return dict(prior) if isinstance(prior, Mapping) and prior.get("outcome") is not None else None + + def _state(story: str | None, outcome: str | None, reason: str | None, refs: Mapping[str, Any] | None = None, **values: Any) -> dict[str, Any]: data = {"stage": "integrate", "phase": values.get("phase"), "story_key": story, "change_id": values.get("change_id"), "outcome": outcome, "reason": reason, "refs": dict(refs or _refs()), "pr": values.get("pr", False), "merged": values.get("merged", False), "repo": values.get("repo"), "spec_path": values.get("spec_path"), "branch": values.get("branch"), "base_branch": values.get("base_branch"), "remote": values.get("remote"), "parent": values.get("parent"), "parent_branch": values.get("parent_branch"), "sub_path": values.get("sub_path"), "status_path": values.get("status_path"), "status_value": values.get("status_value"), "revision": values.get("revision"), "tip": values.get("tip"), "merge": values.get("merge"), "pointer": values.get("pointer")} return data @@ -71,9 +89,9 @@ def _gitlink(repo: Path, tree: str, sub_path: str) -> str | None: return None mode_oid, path = entries[0].split(b"\t", 1) fields = mode_oid.decode("ascii", "replace").split() - if len(fields) != 2 or fields[0] != "160000" or path.decode("utf-8", "replace") != sub_path: + if len(fields) != 3 or fields[0] != "160000" or path.decode("utf-8", "replace") != sub_path: return None - return fields[1] + return fields[2] def _valid_inputs(story_key: object, spec_path: object, branch: object, base_branch: object, remote: object, change_id: object, parent_path: object, parent_branch: object, status_path: object, status: object) -> tuple[str, str, str, str, str, str, str, str, str] | dict[str, Any]: @@ -168,7 +186,10 @@ def profile_integrate_prepare(repo_path: str, story_key: str, spec_path: str, br if report.get("revision") != approved: return _refuse(story, "report.superseded", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) if report.get("verdict") != "consistent": return _refuse(story, "verdict.has_pendency", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) if not is_ancestor(repo, approved, tip) or not approved_only(footprint(repo, approved, tip)): return _refuse(story, "revision.unapproved", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) - parent = _repository(parent_path) + try: + parent = _repository(parent_path) + except ProfileError: + return _refuse(story, "parent.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=parent_path, parent_branch=pbranch, status_path=status, status_value=value, revision=approved, tip=tip) try: sub_path = Path(os.path.relpath(repo, parent)).as_posix() except ValueError: sub_path = "" if not relative_path(sub_path) or _gitlink(parent, "HEAD", sub_path) is None: return _refuse(story, "parent.invalid", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change, revision=approved, tip=tip, sub_path=sub_path), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) @@ -184,12 +205,15 @@ def profile_integrate_prepare(repo_path: str, story_key: str, spec_path: str, br return _state(story, None, None, refs, phase="prepare", change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=remote, parent=str(parent), parent_branch=pbranch, sub_path=sub_path, status_path=status, status_value=value, revision=approved, tip=tip) except ProfileError: return _fail(story, "prepare.failed", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=None, parent_branch=pbranch, status_path=status, status_value=value) - except Exception: # noqa: BLE001 + except OSError: return _fail(story, "prepare.failed", _refs(spec_path=spec, branch=branch, base_branch=base, change_id=change), change_id=change, repo=str(repo), spec_path=spec, branch=branch, base_branch=base, remote=None, parent_branch=pbranch, status_path=status, status_value=value) @beddel_tool(name="profile_integrate_reconcile", description="Reconcile remote integration state", category="profile") def profile_integrate_reconcile(repo_path: str, story_key: str, change_id: str, phase: str, prior: Mapping[str, Any] | None, fetch: Mapping[str, Any] | None, push: Mapping[str, Any] | None = None, pull_request: Mapping[str, Any] | None = None, checks: Mapping[str, Any] | None = None, merge: Mapping[str, Any] | None = None) -> dict[str, Any]: + forwarded = _forwarded(prior) + if forwarded is not None: + return forwarded story = story_key if isinstance(story_key, str) and _valid_story(story_key) else None if story is None or not isinstance(change_id, str) or not is_valid_id(change_id) or phase not in ("land", "confirm"): return _fail(story, "reconcile.failed", _refs(change_id=change_id), change_id=change_id, phase=phase) repo = _repository(repo_path) @@ -202,35 +226,39 @@ def profile_integrate_reconcile(repo_path: str, story_key: str, change_id: str, try: branch, remote, base = prior.get("branch"), prior.get("remote"), prior.get("base_branch") tip = resolve_revision(repo, f"refs/heads/{branch}") - if tip != prior.get("tip"): return _fail(story, "head.moved", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase, tip=tip) + if tip != prior.get("tip"): return _fail(story, "head.moved", _carry_refs(prior, tip=tip), **_carry(prior), phase=phase, tip=tip) tracking = _tracking(repo, str(remote), str(base)) if phase == "land": if is_ancestor(repo, tip, tracking): return _with(prior, merged=True, pr=False, phase="land", tip=tip) - if _symbolic_branch(repo) != branch: return _fail(story, "branch.not_current", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="land", tip=tip, pr=False, merged=False) + if _symbolic_branch(repo) != branch: return _fail(story, "branch.not_current", _carry_refs(prior, tip=tip), **_carry(prior), phase="land", tip=tip, pr=False, merged=False) return _with(prior, phase="land", pr=True, merged=False, tip=tip) contained = is_ancestor(repo, tip, tracking) if contained: merge_sha = _merge_commit(repo, tip, tracking) - if merge_sha is None: return _fail(story, "merge.unconfirmed", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) + if merge_sha is None: return _fail(story, "merge.unconfirmed", _carry_refs(prior, tip=tip), **_carry(prior), phase="confirm", tip=tip) if isinstance(merge, Mapping): result = merge.get("result") reported = result.get("merge_sha") if isinstance(result, Mapping) else None - if reported != merge_sha: return _fail(story, "merge.mismatch", _refs(**dict(prior.get("refs", {})), tip=tip, merge=merge_sha), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip, merge=merge_sha) + if reported != merge_sha: return _fail(story, "merge.mismatch", _carry_refs(prior, tip=tip, merge=merge_sha), **_carry(prior), phase="confirm", tip=tip, merge=merge_sha) number = pull_request.get("result", {}).get("number") if isinstance(pull_request, Mapping) and isinstance(pull_request.get("result"), Mapping) else None - refs = _refs(**dict(prior.get("refs", {})), boundary="repository", tip=tip, merge=merge_sha, pr=number) + refs = _carry_refs(prior, boundary="repository", tip=tip, merge=merge_sha, pr=number) return _with(prior, outcome="succeeded", reason="merge.confirmed" if prior.get("pr") else "merge.reused", refs=refs, phase="confirm", tip=tip, merge=merge_sha, merged=True) if prior.get("pr") is True: for item, reason in ((push, "push.failed"), (pull_request, "pr.failed"), (checks, "checks.failed"), (merge, "merge.failed")): - if not _host_ok(item): return _fail(story, reason, _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) - return _fail(story, "merge.unconfirmed", _refs(**dict(prior.get("refs", {})), tip=tip), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase="confirm", tip=tip) + if not _host_ok(item): return _fail(story, reason, _carry_refs(prior, tip=tip), **_carry(prior), phase="confirm", tip=tip) + return _fail(story, "merge.unconfirmed", _carry_refs(prior, tip=tip), **_carry(prior), phase="confirm", tip=tip) except ProfileError: - return _fail(story, "reconcile.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase) - except Exception: # noqa: BLE001 - return _fail(story, "reconcile.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision")}, phase=phase) + return _fail(story, "reconcile.failed", _carry_refs(prior), **_carry(prior), phase=phase) + except OSError: + return _fail(story, "reconcile.failed", _carry_refs(prior), **_carry(prior), phase=phase) @beddel_tool(name="profile_integrate_settle", description="Settle the parent integration boundary", category="profile") def profile_integrate_settle(repo_path: str, story_key: str, change_id: str, phase: str, prior: Mapping[str, Any] | None, fetch: Mapping[str, Any] | None = None) -> dict[str, Any]: + if phase == "finish": + forwarded = _forwarded(prior) + if forwarded is not None: + return forwarded story = story_key if isinstance(story_key, str) and _valid_story(story_key) else None if story is None or not isinstance(change_id, str) or not is_valid_id(change_id) or phase not in ("pointer", "finish"): return _fail(story, "pointer.failed", _refs(change_id=change_id), change_id=change_id, phase=phase) repo = _repository(repo_path) @@ -241,11 +269,11 @@ def profile_integrate_settle(repo_path: str, story_key: str, change_id: str, pha bad_fetch = _fetch_check(fetch, story, change_id, repo, phase) if bad_fetch is not None: return bad_fetch try: - if phase == "pointer" and (prior.get("phase") != "confirm" or prior.get("merged") is not True): return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") - if phase == "finish" and prior.get("phase") != "pointer": return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + if phase == "pointer" and (prior.get("phase") != "confirm" or prior.get("merged") is not True): return _fail(story, "pointer.failed", _carry_refs(prior), **_carry(prior), phase="pointer") + if phase == "finish" and prior.get("phase") != "pointer": return _fail(story, "pointer.failed", _carry_refs(prior), **_carry(prior), phase="finish") parent = _repository(str(prior.get("parent"))) sub_path, merge = prior.get("sub_path"), prior.get("merge") - if not isinstance(sub_path, str) or not relative_path(sub_path) or not isinstance(merge, str): return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "pointer")}, phase=phase) + if not isinstance(sub_path, str) or not relative_path(sub_path) or not isinstance(merge, str): return _fail(story, "pointer.failed", _carry_refs(prior), **_carry(prior), phase=phase) if phase == "pointer": parent_head = resolve_revision(parent, "HEAD") current = _gitlink(parent, "HEAD", sub_path) @@ -253,47 +281,48 @@ def profile_integrate_settle(repo_path: str, story_key: str, change_id: str, pha if not reused and current is not None: try: reused = is_ancestor(repo, merge, resolve_revision(repo, current)) except ProfileError: reused = False - if reused: return _with(prior, phase="pointer", pointer=parent_head, refs=_refs(**dict(prior.get("refs", {})), boundary="parent", pointer=parent_head, sub_path=sub_path)) - if _symbolic_branch(parent) != prior.get("parent_branch"): return _fail(story, "parent.not_current", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") - if changed_paths(repo) - {prior.get("spec_path")}: return _fail(story, "worktree.dirty", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if reused: return _with(prior, phase="pointer", pointer=parent_head, refs=_carry_refs(prior, boundary="parent", pointer=parent_head, sub_path=sub_path)) + if _symbolic_branch(parent) != prior.get("parent_branch"): return _fail(story, "parent.not_current", _carry_refs(prior, boundary="parent", sub_path=sub_path), **_carry(prior), phase="pointer") + if changed_paths(repo) - {prior.get("spec_path")}: return _fail(story, "worktree.dirty", _carry_refs(prior, boundary="parent", sub_path=sub_path), **_carry(prior), phase="pointer") if resolve_revision(repo, "HEAD") != merge: switched = git_run(repo, "switch", "-q", "--detach", merge) - if switched is None or switched.returncode != 0: return _fail(story, "checkout.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") - if resolve_revision(repo, "HEAD") != merge: return _fail(story, "checkout.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if switched is None or switched.returncode != 0: return _fail(story, "checkout.failed", _carry_refs(prior, boundary="parent", sub_path=sub_path), **_carry(prior), phase="pointer") + if resolve_revision(repo, "HEAD") != merge: return _fail(story, "checkout.failed", _carry_refs(prior, boundary="parent", sub_path=sub_path), **_carry(prior), phase="pointer") old = parent_head subject, trailers = pointer_message(story, sub_path, merge) committed = git_run(parent, "commit", "-q", "-m", subject, "-m", trailers, "--only", "--", sub_path, timeout=120) - if committed is None or committed.returncode != 0: return _fail(story, "commit.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer") + if committed is None or committed.returncode != 0: return _fail(story, "commit.failed", _carry_refs(prior, boundary="parent", sub_path=sub_path), **_carry(prior), phase="pointer") pointer = resolve_revision(parent, "HEAD") - if _git_text(git_run(parent, "rev-parse", "HEAD^")) != old or _gitlink(parent, "HEAD", sub_path) != merge: return _fail(story, "commit.changed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=pointer), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer", pointer=pointer) - return _with(prior, phase="pointer", pointer=pointer, refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=pointer)) + if _git_text(git_run(parent, "rev-parse", "HEAD^")) != old or _gitlink(parent, "HEAD", sub_path) != merge: return _fail(story, "commit.changed", _carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=pointer), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "status_path", "status_value", "revision", "tip", "merge")}, phase="pointer", pointer=pointer) + return _with(prior, phase="pointer", pointer=pointer, refs=_carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=pointer)) tracking = _tracking(parent, str(prior.get("remote")), str(prior.get("parent_branch"))) remote_gitlink = _gitlink(parent, tracking, sub_path) published = remote_gitlink == merge if not published and remote_gitlink is not None: try: published = is_ancestor(repo, merge, resolve_revision(repo, remote_gitlink)) except ProfileError: published = False - if not published: return _with(prior, outcome="stopped", reason="pointer.unpublished", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), pointer=prior.get("pointer")) + if not published: return _with(prior, outcome="stopped", reason="pointer.unpublished", phase="finish", refs=_carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), pointer=prior.get("pointer")) status = prior.get("status_path") - if not status: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) + if not status: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) data = read_confined(parent, str(status)) - if data is None: return _fail(story, "status.unmatched", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + if data is None: return _fail(story, "status.unmatched", _carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **_carry(prior), phase="finish") value = str(prior.get("status_value")) - if status_value(data, story) == value: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) + if status_value(data, story) == value: return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=False), pointer=prior.get("pointer")) updated = set_status(data, story, value) - if updated is None: return _fail(story, "status.unmatched", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") + if updated is None: return _fail(story, "status.unmatched", _carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **_carry(prior), phase="finish") target = confined(parent, str(status)) fd, temporary = tempfile.mkstemp(prefix=".beddel-status-", dir=str(target.parent)) try: with os.fdopen(fd, "wb") as handle: handle.write(updated) + os.chmod(temporary, stat.S_IMODE(os.stat(target).st_mode)) os.replace(temporary, target) except OSError: try: os.unlink(temporary) except OSError: pass - return _fail(story, "status.failed", _refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "remote", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase="finish") - return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_refs(**dict(prior.get("refs", {})), boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=True), pointer=prior.get("pointer")) + return _fail(story, "status.failed", _carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer")), **_carry(prior), phase="finish") + return _with(prior, outcome="succeeded", reason="integrate.complete", phase="finish", refs=_carry_refs(prior, boundary="parent", sub_path=sub_path, pointer=prior.get("pointer"), status_written=True), pointer=prior.get("pointer")) except ProfileError: - return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase=phase) - except Exception: # noqa: BLE001 - return _fail(story, "pointer.failed", _refs(**dict(prior.get("refs", {}))), **{k: prior.get(k) for k in ("change_id", "repo", "spec_path", "branch", "base_branch", "parent", "parent_branch", "sub_path", "status_path", "status_value", "revision", "tip", "merge", "pointer")}, phase=phase) + return _fail(story, "pointer.failed", _carry_refs(prior), **_carry(prior), phase=phase) + except OSError: + return _fail(story, "pointer.failed", _carry_refs(prior), **_carry(prior), phase=phase) # fmt: on diff --git a/kits/profile-dev-kit/tests/test_integrate_workflow.py b/kits/profile-dev-kit/tests/test_integrate_workflow.py index f998e0b..b1d5d8f 100644 --- a/kits/profile-dev-kit/tests/test_integrate_workflow.py +++ b/kits/profile-dev-kit/tests/test_integrate_workflow.py @@ -89,11 +89,246 @@ def test_integrate_workflow_parses_and_conditions_pair() -> None: steps = {step.id: step for step in workflow.steps} assert steps["confirm"].if_condition == "$stepResult.land.result.pr == true" assert steps["confirm_reuse"].if_condition == "$stepResult.land.result.pr != true" - assert ( - steps["confirm"].config["timeout"] == 2000 - if "timeout" in steps["confirm"].config - else True - ) + assert steps["checks"].config["timeout"] == 2000 + assert steps["push"].config["timeout"] == 330 + assert steps["pull_request"].config["timeout"] == 400 assert steps["confirm"].execution_strategy.type is StrategyType.FAIL assert steps["confirm_reuse"].execution_strategy.type is StrategyType.FAIL assert steps["confirm"].config["tool"] == "profile_integrate_reconcile" + + +import asyncio +import json +import sys +from typing import Any + +from beddel.domain.executor import WorkflowExecutor +from beddel.domain.kit import parse_kit_manifest +from beddel.domain.models import DefaultDependencies +from beddel.domain.registry import PrimitiveRegistry +from beddel.primitives import register_builtins +from beddel.tools.kits import load_kit + +from ._helpers import commit_all, git, init_repo + +KIT_ROOT = PATH.parents[1] +KITS_ROOT = KIT_ROOT.parent +sys.path.insert(0, str(KITS_ROOT / "tools-git-kit" / "python")) +sys.path.insert(0, str(KITS_ROOT / "tools-github-kit" / "python")) +STORY = "7-6-integrate-workflow-completes-only-after-approval" +BRANCH = "feat/work" + + +def _fixture(tmp_path: Path) -> dict[str, Any]: + root = tmp_path / "case" + root.mkdir() + sub_bare = root / "sub.git" + parent_bare = root / "parent.git" + git(root, "init", "-q", "--bare", "-b", "main", str(sub_bare)) + git(root, "init", "-q", "--bare", "-b", "main", str(parent_bare)) + parent = init_repo(root / "parent") + sub = init_repo(parent / "sub") + for repo in (parent, sub): + git(repo, "config", "user.name", "Fixture") + git(repo, "config", "user.email", "fixture@example.invalid") + (sub / "base.txt").write_text("base\n") + base = commit_all(sub, "base") + git(sub, "remote", "add", "origin", str(sub_bare)) + git(sub, "push", "-q", "origin", "HEAD:refs/heads/main") + (parent / "status.yaml").write_text(f"x: 1\n {STORY}: review # keep\n") + git(parent, "update-index", "--add", "--cacheinfo", f"160000,{base},sub") + commit_all(parent, "parent") + git(parent, "remote", "add", "origin", str(parent_bare)) + git(parent, "push", "-q", "origin", "HEAD:refs/heads/main") + git(parent, "switch", "-C", "main") + git(sub, "switch", "-q", "-c", BRANCH) + (sub / "change.txt").write_text("change\n") + tip = commit_all(sub, "change") + return { + "root": root, + "parent": parent, + "sub": sub, + "base": base, + "tip": tip, + "sub_bare": sub_bare, + } + + +class _Host: + def __init__(self, env: dict[str, Any]) -> None: + self.env = env + self.calls: list[str] = [] + self.fail_checks = False + + async def create(self, **kwargs: Any) -> dict[str, Any]: + self.calls.append("pr") + return {"number": 7, "created": True} + + async def checks(self, **kwargs: Any) -> dict[str, Any]: + self.calls.append("checks") + if self.fail_checks: + raise RuntimeError("checks failed") + return {"status": "passed"} + + async def merge(self, **kwargs: Any) -> dict[str, Any]: + self.calls.append("merge") + if self.fail_checks: + raise RuntimeError("merge blocked after checks failure") + clone = self.env["root"] / f"clone-{len(self.calls)}" + git(self.env["root"], "clone", "-q", str(self.env["sub_bare"]), str(clone)) + git(clone, "fetch", "-q", "origin", f"refs/heads/{BRANCH}") + git(clone, "merge", "-q", "--no-ff", "-m", "Merge", kwargs["expected_head_sha"]) + git(clone, "push", "-q", "origin", "main") + return {"merged": True, "merge_sha": git(clone, "rev-parse", "HEAD")} + + +def _run( + env: dict[str, Any], + host: _Host, + approval: dict[str, Any] | None, + report: dict[str, Any] | None, + monkeypatch: Any, + overrides: dict[str, Any] | None = None, +) -> dict[str, Any]: + from beddel_profile_dev.tools import integrate as module + + monkeypatch.setattr( + module, "profile_read_approval", lambda repo, change: {"approval": approval} + ) + monkeypatch.setattr( + module, "profile_read_report", lambda repo, change: {"report": report} + ) + registry: dict[str, Any] = {} + for name in ("profile-dev-kit", "tools-git-kit", "tools-github-kit"): + registry.update(load_kit(parse_kit_manifest(KITS_ROOT / name / "kit.yaml"))) + registry.update( + { + "github_pr_create_or_reuse": host.create, + "github_pr_checks_wait": host.checks, + "github_pr_merge": host.merge, + } + ) + registry.update(overrides or {}) + primitive = PrimitiveRegistry() + register_builtins(primitive) + executor = WorkflowExecutor( + primitive, deps=DefaultDependencies(tool_registry=registry) + ) + inputs = { + "repo_path": str(env["sub"]), + "story_key": STORY, + "spec_path": "spec.md", + "branch": BRANCH, + "base_branch": "main", + "remote": "origin", + "change_id": "c1", + "parent_path": str(env["parent"]), + "parent_branch": "main", + "status_path": "status.yaml", + } + return asyncio.run(executor.execute(WorkflowParser.parse(PATH.read_text()), inputs)) + + +def _approval(env: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]: + return ( + { + "revision": env["tip"], + "proof": {"commit": env["tip"]}, + "report": "changes/c1/report/1.json", + }, + {"revision": env["tip"], "verdict": "consistent"}, + ) + + +def _records(home: Path) -> list[dict[str, Any]]: + return [ + json.loads(path.read_text()) + for path in sorted(home.rglob("stage-outcomes/*.json")) + ] + + +def test_workflow_happy_path_owner_push_and_complete_rerun( + tmp_path: Path, monkeypatch: Any +) -> None: + env = _fixture(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + (tmp_path / "home").mkdir(exist_ok=True) + host = _Host(env) + approval, report = _approval(env) + first = _run(env, host, approval, report, monkeypatch) + records = _records(tmp_path / "home") + assert [record["reason"] for record in records] == [ + "merge.confirmed", + "pointer.unpublished", + ] + assert first["step_results"]["pointer"]["result"]["pointer"] + git(env["parent"], "push", "-q", "origin", "main") + second = _run(env, host, approval, report, monkeypatch) + assert second["step_results"]["finish"]["result"]["reason"] == "integrate.complete" + assert ( + (env["parent"] / "status.yaml") + .read_text() + .splitlines()[1] + .endswith(": done # keep") + ) + third = _run(env, host, approval, report, monkeypatch) + assert third["step_results"]["finish"]["result"]["refs"]["status_written"] is False + assert host.calls.count("merge") == 1 + + +def test_workflow_absent_approval_has_no_host_calls_or_push( + tmp_path: Path, monkeypatch: Any +) -> None: + env = _fixture(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + (tmp_path / "home").mkdir(exist_ok=True) + host = _Host(env) + _run(env, host, None, None, monkeypatch) + records = _records(tmp_path / "home") + assert [(record["outcome"], record["reason"]) for record in records] == [ + ("refused", "approval.absent") + ] + assert host.calls == [] + assert ( + git(env["sub_bare"], "for-each-ref", "--format=%(refname)") == "refs/heads/main" + ) + + +def test_workflow_checks_failure_has_no_parent_record( + tmp_path: Path, monkeypatch: Any +) -> None: + env = _fixture(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + (tmp_path / "home").mkdir(exist_ok=True) + host = _Host(env) + host.fail_checks = True + approval, report = _approval(env) + _run(env, host, approval, report, monkeypatch) + records = _records(tmp_path / "home") + assert [(record["outcome"], record["reason"]) for record in records] == [ + ("failed", "checks.failed") + ] + assert host.calls == ["pr", "checks", "merge"] + + +def test_workflow_failed_refetch_reuses_merge(tmp_path: Path, monkeypatch: Any) -> None: + env = _fixture(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + (tmp_path / "home").mkdir(exist_ok=True) + host = _Host(env) + approval, report = _approval(env) + registry = load_kit(parse_kit_manifest(KITS_ROOT / "tools-git-kit" / "kit.yaml")) + real_fetch = registry["git_fetch"] + calls = {"count": 0} + + async def flaky_fetch(**kwargs: Any) -> Any: + calls["count"] += 1 + if calls["count"] == 2: + raise RuntimeError("fetch failed") + return await real_fetch(**kwargs) + + _run(env, host, approval, report, monkeypatch, {"git_fetch": flaky_fetch}) + assert _records(tmp_path / "home")[-1]["reason"] == "fetch.failed" + _run(env, host, approval, report, monkeypatch) + assert _records(tmp_path / "home")[-2]["reason"] == "merge.reused" + assert host.calls.count("merge") == 1