From 9c45dbfac26ae6c2232c3aa72cb5524443846489 Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Thu, 12 Nov 2026 13:26:20 -0300 Subject: [PATCH 1/3] feat(profile-dev-kit): deliver workflow chains stages and resumes (Story 7.7) --- kits/profile-dev-kit/README.md | 30 ++ kits/profile-dev-kit/kit.yaml | 13 + .../python/beddel_profile_dev/deliver.py | 62 ++++ .../beddel_profile_dev/tools/deliver.py | 154 ++++++++++ kits/profile-dev-kit/tests/test_approval.py | 36 +++ kits/profile-dev-kit/tests/test_deliver.py | 254 ++++++++++++++++ .../tests/test_deliver_workflow.py | 278 ++++++++++++++++++ kits/profile-dev-kit/tests/test_manifest.py | 3 + kits/profile-dev-kit/tests/test_workflow.py | 2 +- .../workflows/deliver-story.yaml | 139 +++++++++ 10 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 kits/profile-dev-kit/python/beddel_profile_dev/deliver.py create mode 100644 kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py create mode 100644 kits/profile-dev-kit/tests/test_deliver.py create mode 100644 kits/profile-dev-kit/tests/test_deliver_workflow.py create mode 100644 kits/profile-dev-kit/workflows/deliver-story.yaml diff --git a/kits/profile-dev-kit/README.md b/kits/profile-dev-kit/README.md index 53aa6b2..44fd8c0 100644 --- a/kits/profile-dev-kit/README.md +++ b/kits/profile-dev-kit/README.md @@ -519,6 +519,36 @@ 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 ```text beddel run -i ... workflows/integrate-story.yaml ``` + +## Delivery: deliver workflow + +`deliver-story` takes `repo_path`, `story_key`, `stage`, the planning inputs +`story_path`, `guide_path` and `spec_path`, the implementation inputs +`branch`, `base_ref`, `change_id` and `inventory_version`, the Approval +`actor`, and the integration inputs `base_branch`, `remote`, `parent_path`, +`parent_branch` and `status_path`. Set `stage` to `all` to chain plan, +implement, verify-and-fix, Approval and integrate, or select one stage. + +The gate reads the create-only Stage outcomes under the profile records root. +A complete outcome advances the next stage; an incomplete, failed or stopped +outcome ends the run at that boundary. The implement stage has 33 static +nested calls so one unit is reconciled per run without a loop primitive. +Reruns start after the latest complete stage, and a completed Delivery returns +`deliver.complete` without invoking a stage. + +Approval waits for the owner to commit the decision for the latest Report in +HEAD. It passes the unchanged `actor` to `approve-change` exactly once, records +`approval.recorded`, `approval.reused`, `approval.refused` or the owner stop, +and only then allows integrate. The workflow writes one `deliver` outcome with +reasons such as `stage.stopped`, `stage.failed`, `stage.incomplete` and +`deliver.complete`; it never writes an Approval decision or pushes the parent. +Run it from the Change repository root, once per Change: + +```text +beddel run -i ... workflows/deliver-story.yaml +``` + diff --git a/kits/profile-dev-kit/kit.yaml b/kits/profile-dev-kit/kit.yaml index 614a144..7e130fe 100644 --- a/kits/profile-dev-kit/kit.yaml +++ b/kits/profile-dev-kit/kit.yaml @@ -136,6 +136,12 @@ tools: - name: profile_integrate_settle target: "beddel_profile_dev.tools.integrate:profile_integrate_settle" description: "Settle the parent integration boundary" + - name: profile_deliver_gate + target: "beddel_profile_dev.tools.deliver:profile_deliver_gate" + description: "Gate chained Delivery stages from recorded outcomes" + - name: profile_deliver_approval + target: "beddel_profile_dev.tools.deliver:profile_deliver_approval" + description: "Check and settle the Delivery Approval stage" category: profile workflows: @@ -175,6 +181,9 @@ workflows: - 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. + - name: deliver-story + path: workflows/deliver-story.yaml + description: Chains the Delivery stages or runs one selected stage, waits for Approval and records the complete Delivery outcome. targets: python: @@ -234,6 +243,10 @@ targets: target: "beddel_profile_dev.tools.integrate:profile_integrate_reconcile" - name: profile_integrate_settle target: "beddel_profile_dev.tools.integrate:profile_integrate_settle" + - name: profile_deliver_gate + target: "beddel_profile_dev.tools.deliver:profile_deliver_gate" + - name: profile_deliver_approval + target: "beddel_profile_dev.tools.deliver:profile_deliver_approval" typescript: status: unavailable unavailable_reason: > diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/deliver.py b/kits/profile-dev-kit/python/beddel_profile_dev/deliver.py new file mode 100644 index 0000000..6e0e8a8 --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/deliver.py @@ -0,0 +1,62 @@ +"""Pure contracts for the chained profile Delivery workflow.""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any + +from beddel_profile_dev.implement import MAX_UNITS + +__all__ = [ + "COMPLETE", + "IMPLEMENT_SLOTS", + "REQUESTS", + "STAGES", + "complete", + "next_stage", + "resume", +] + +STAGES = ("plan", "implement", "verify-and-fix", "approval", "integrate") +REQUESTS = ("all", *STAGES) +IMPLEMENT_SLOTS = MAX_UNITS + 1 +COMPLETE = { + "plan": ("spec.created", "spec.reconciled", "spec.reused"), + "implement": ("units.complete",), + "verify-and-fix": ("verdict.consistent",), + "approval": ("approval.recorded", "approval.reused"), + "integrate": ("integrate.complete",), +} + + +def complete(stage: object, outcome: object, reason: object) -> bool: + """Return whether a stage outcome is a complete stage boundary.""" + return ( + isinstance(stage, str) + and isinstance(outcome, str) + and outcome == "succeeded" + and reason in COMPLETE.get(stage, ()) + ) + + +def next_stage(stage: str) -> str | None: + """Return the next Delivery stage after ``stage``.""" + try: + index = STAGES.index(stage) + except ValueError: + return None + return STAGES[index + 1] if index + 1 < len(STAGES) else None + + +def resume(records: list[Mapping[str, Any]]) -> tuple[str | None, bool]: + """Choose the next stage from the story's ordered Stage outcomes.""" + if not records: + return "plan", False + latest = records[-1] + stage = latest.get("stage") + outcome = latest.get("outcome") + reason = latest.get("reason") + if complete(stage, outcome, reason): + following = next_stage(stage) if isinstance(stage, str) else None + return (following, False) if following is not None else (None, True) + return (stage if stage in STAGES else "plan"), False diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py new file mode 100644 index 0000000..98a59fd --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py @@ -0,0 +1,154 @@ +"""Gate and Approval-stage tools for chained profile Delivery.""" + +from __future__ import annotations + +import json +import re +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 PROFILE_IMPACT_UNBOUND, ProfileError +from beddel_profile_dev._io import blob_at, git_run, real_repo_root, records_root +from beddel_profile_dev.deliver import REQUESTS, STAGES, complete, next_stage, resume +from beddel_profile_dev.implement import branch_reason +from beddel_profile_dev.integrate import APPROVALS_DIR +from beddel_profile_dev.inventory import is_valid_id +from beddel_profile_dev.tools.approval import profile_read_approval +from beddel_profile_dev.tools.implement import _git_text, _symbolic_branch, _valid_story +from beddel_profile_dev.tools.ratify import valid_actor +from beddel_profile_dev.tools.report import profile_read_report +from beddel_profile_dev.tools.stage_outcome import ( + OUTCOMES, + STAGE_OUTCOME_SCHEMA, + _next_ordinal, +) + +__all__ = ["profile_deliver_approval", "profile_deliver_gate"] +# fmt: off +_RECORD = re.compile(r"([1-9][0-9]*)\.json") +_KEYS = ("request", "resume", "stage", "stage_record", "stage_outcome", "stage_reason", "first_record", "invoked") +_BAD = ProfileError("PROFILE_REPOSITORY_INVALID", "repository or revision invalid") + + +def _num(path: Path) -> int: + match = _RECORD.fullmatch(path.name) + return int(match.group(1)) if match is not None else 0 + + +def _refs(**values: Any) -> dict[str, Any]: + return {key: values.get(key) for key in _KEYS} + + +def _records(root: Path) -> list[dict[str, Any]]: + directory = root / ("stage-" + "outcomes") + if not directory.exists(): return [] + if not directory.is_dir(): raise _BAD + found: list[dict[str, Any]] = [] + for path in sorted(directory.iterdir(), key=_num): + match = _RECORD.fullmatch(path.name) + if match is None: continue + try: data = json.loads(path.read_text(encoding="utf-8")) + except (OSError, ValueError, RecursionError): raise _BAD from None + if (not isinstance(data, dict) or set(data) != {"schema", "record", "stage", "story_key", "outcome", "reason", "refs", "recorded_at"} or data.get("schema") != STAGE_OUTCOME_SCHEMA or data.get("record") != int(match.group(1)) or not isinstance(data.get("stage"), str) or (data.get("story_key") is not None and not _valid_story(data.get("story_key"))) or data.get("outcome") not in OUTCOMES or not isinstance(data.get("reason"), str) or not isinstance(data.get("refs"), dict) or not isinstance(data.get("recorded_at"), str)): + raise _BAD + found.append(data) + return found + + +def _state(repo: Path, story: str | None, request: str, step: str, **values: Any) -> dict[str, Any]: + go, outcome, reason = values.get("go"), values.get("outcome"), values.get("reason") + return {"stage": "deliver", "step": step, "story_key": story, "request": request, "repo": str(repo), "go": go, "mark": values.get("mark"), "first": values.get("first"), "resume": go, "invoked": values.get("invoked", ""), "outcome": outcome, "reason": reason, "refs": values.get("refs") or _refs(request=request, resume=go, stage=step, invoked=values.get("invoked", ""))} + + +def _failed(repo: Path, story: str | None, request: str, step: str, reason: str, **values: Any) -> dict[str, Any]: + return _state(repo, story, request, step, outcome="failed", reason=reason, refs=_refs(request=request, resume=values.get("resume"), stage=values.get("stage", step), stage_record=values.get("stage_record"), stage_outcome=values.get("stage_outcome"), stage_reason=reason, first_record=values.get("first"), invoked=values.get("invoked")), **{key: value for key, value in values.items() if key in {"mark", "first", "invoked", "go"}}) + + +@beddel_tool(name="profile_deliver_gate", description="Gate a Delivery stage from create-only Stage outcomes", category="profile") +def profile_deliver_gate(repo_path: str, story_key: str, request: str, step: str, prior: object) -> dict[str, Any]: + repo = real_repo_root(repo_path) + if not _valid_story(story_key): return _state(repo, None, request, step, outcome="refused", reason="story_key.invalid", refs=_refs(request=request, stage=step, stage_reason="story_key.invalid")) + if request not in REQUESTS: return _state(repo, story_key, request, step, outcome="refused", reason="request.invalid", refs=_refs(request=request, stage=step, stage_reason="request.invalid")) + root = records_root(repo, "records/profile-dev") + g = lambda **values: _state(repo, story_key, request, step, **values) + f = lambda reason, **values: _failed(repo, story_key, request, step, reason, **values) + if step == "start": + try: + records = _records(root) + story_records: list[Mapping[str, Any]] = [record for record in records if record.get("story_key") == story_key and record.get("stage") in STAGES] + mark = _next_ordinal(root) + go, done = (request, False) if request != "all" else resume(story_records) + return g(go=None if done else go, outcome="succeeded" if done else None, reason="deliver.complete" if done else None, mark=mark, first=mark, refs=_refs(request=request, resume=None if done else go, first_record=mark, invoked="")) + except ProfileError: return f("records.unreadable") + if step not in STAGES: return g(outcome="failed", reason="gate.failed", refs=_refs(request=request, stage=step, stage_reason="gate.failed")) + if prior is None: return f("gate.failed") + if not isinstance(prior, Mapping) or any(prior.get(key) != value for key, value in (("stage", "deliver"), ("story_key", story_key), ("request", request), ("repo", str(repo)))): return f("state.mismatch") + if prior.get("outcome") is not None or prior.get("go") != step: return dict(prior) + mark_value = prior.get("mark") + if not isinstance(mark_value, int): return f("state.mismatch") + mark = mark_value + try: records = _records(root) + except ProfileError: return f("records.unreadable", mark=mark) + candidates = [record for record in records if isinstance(record.get("record"), int) and record["record"] >= mark and record.get("stage") == step and record.get("story_key") == story_key] + invoked = ", ".join(filter(None, [str(prior.get("invoked", "")), step])) + if not candidates: return f("stage.unrecorded", mark=mark, first=prior.get("first"), invoked=invoked, go=step, stage=step) + record, outcome, record_reason = candidates[-1], candidates[-1]["outcome"], candidates[-1]["reason"] + refs = _refs(request=request, resume=step, stage=step, stage_record=record["record"], stage_outcome=outcome, stage_reason=record_reason, first_record=prior.get("first"), invoked=invoked) + if complete(step, outcome, record_reason): + following = next_stage(step) + if following is None: return g(outcome="succeeded", reason="deliver.complete", mark=_next_ordinal(root), first=prior.get("first"), invoked=invoked, refs=refs) + if request != "all": return g(outcome="succeeded", reason="stage.complete", mark=_next_ordinal(root), first=prior.get("first"), invoked=invoked, refs=refs) + return g(go=following, mark=_next_ordinal(root), first=prior.get("first"), invoked=invoked, refs={**refs, "resume": following, "stage": following}) + if outcome == "succeeded": return g(outcome="failed", reason="stage.incomplete", mark=mark, first=prior.get("first"), invoked=invoked, refs=refs) + return g(outcome=outcome, reason=f"stage.{outcome}", mark=mark, first=prior.get("first"), invoked=invoked, refs=refs) + + +def _approval(repo: Path, story: str, change: str, branch: str, **values: Any) -> dict[str, Any]: + return {"stage": "approval", "phase": values.get("phase", "check"), "story_key": story, "repo": str(repo), "change_id": change, "branch": branch, "approve": values.get("approve", False), "revision": values.get("revision"), "decision_commit": values.get("decision_commit"), "outcome": values.get("outcome"), "reason": values.get("reason"), "refs": values.get("refs") or {"change_id": change, "branch": branch, "revision": values.get("revision"), "report": None, "decision_path": None, "decision_commit": values.get("decision_commit"), "approval": None, "invoked": values.get("approve", False)}} +def _a(repo: Path, story: str, change: str, branch: str, outcome: str, reason: str, **values: Any) -> dict[str, Any]: + return _approval(repo, story, change, branch, outcome=outcome, reason=reason, **values) +@beddel_tool(name="profile_deliver_approval", description="Reconcile and settle the Approval stage boundary", category="profile") +def profile_deliver_approval(repo_path: str, story_key: str, change_id: str, branch: str, actor: str, phase: str, prior: object) -> dict[str, Any]: + repo = real_repo_root(repo_path) + a = lambda outcome, reason, **values: _a(repo, story_key, change_id, branch, outcome, reason, **values) + if phase not in ("check", "settle"): return a("failed", "approval.invalid") + if not isinstance(prior, Mapping) or prior.get("story_key") != story_key or prior.get("repo") != str(repo): return a("failed", "state.mismatch") + if phase == "check": + if prior.get("stage") != "deliver" or prior.get("go") != "approval": return a("failed", "state.mismatch") + if not is_valid_id(change_id): return a("refused", "change_id.invalid") + if branch_reason(branch, None) is not None: return a("refused", "branch.invalid") + if not valid_actor(actor): return a("refused", "actor.invalid") + if _symbolic_branch(repo) != branch: return a("refused", "branch.not_current") + try: stored = profile_read_approval(str(repo), change_id).get("approval") + except ProfileError as error: + return a("refused" if error.code == PROFILE_IMPACT_UNBOUND else "failed", "report.absent" if error.code == PROFILE_IMPACT_UNBOUND else "approval.invalid") + if stored is not None: + revision = stored.get("revision") if isinstance(stored, Mapping) else None + return a("succeeded", "approval.reused", revision=revision, refs={"change_id": change_id, "branch": branch, "revision": revision, "report": None, "decision_path": None, "decision_commit": None, "approval": "proof.commit", "invoked": False}) + try: + data = profile_read_report(str(repo), change_id) + history, report = data.get("history"), data.get("report") + except ProfileError: return a("failed", "report.unreadable") + if not isinstance(history, list) or not history or not isinstance(report, Mapping): return a("refused", "report.absent") + latest = history[-1] if isinstance(history[-1], Mapping) else {} + match, revision = re.search(r"/(\d+)\.json$", str(latest.get("ref"))), report.get("revision") + if match is None or not isinstance(revision, str): return a("failed", "report.unreadable") + path = f"{APPROVALS_DIR}{change_id}/{match.group(1)}.yaml" + refs = {"change_id": change_id, "branch": branch, "revision": revision, "report": latest.get("ref"), "decision_path": path, "decision_commit": None, "approval": None, "invoked": True} + if blob_at(repo, "HEAD", path) is None: return a("stopped", "approval.awaiting_owner", revision=revision, refs=refs) + commit = _git_text(git_run(repo, "log", "-1", "--format=%H", "--diff-filter=A", "HEAD", "--", path)) + if not commit or not re.fullmatch(r"[0-9a-f]{40}", commit): return a("failed", "decision.unreadable", revision=revision, refs=refs) + refs["decision_commit"] = commit + return _approval(repo, story_key, change_id, branch, approve=True, revision=revision, decision_commit=commit, refs=refs) + if prior.get("stage") != "approval" or prior.get("phase") != "check" or prior.get("change_id") != change_id: return a("failed", "state.mismatch") + if prior.get("outcome") is not None: return dict(prior) + try: stored = profile_read_approval(str(repo), change_id).get("approval") + except ProfileError: return {**dict(prior), "outcome": "failed", "reason": "approval.invalid"} + if stored is None: return {**dict(prior), "outcome": "refused", "reason": "approval.refused"} + if not isinstance(stored, Mapping) or stored.get("revision") != prior.get("revision"): return {**dict(prior), "outcome": "failed", "reason": "approval.invalid"} + refs = dict(prior.get("refs") or {}); refs.update(approval="proof.commit", invoked=True) + return {**dict(prior), "outcome": "succeeded", "reason": "approval.recorded", "refs": refs} +# fmt: on diff --git a/kits/profile-dev-kit/tests/test_approval.py b/kits/profile-dev-kit/tests/test_approval.py index 9724044..1ec85d0 100644 --- a/kits/profile-dev-kit/tests/test_approval.py +++ b/kits/profile-dev-kit/tests/test_approval.py @@ -216,6 +216,7 @@ def test_structural_approval_boundaries() -> None: "approval_decision.py", "_errors.py", "integrate.py", + "deliver.py", }: assert "approv" not in path.read_text().lower() source = (PYTHON_ROOT / "beddel_profile_dev" / "tools" / "approval.py").read_text() @@ -283,6 +284,41 @@ def test_structural_approval_boundaries() -> None: "approval_decision", ) ) + for deliver_source in ( + PYTHON_ROOT / "beddel_profile_dev" / "deliver.py", + PYTHON_ROOT / "beddel_profile_dev" / "tools" / "deliver.py", + ): + deliver_text = deliver_source.read_text() + assert all( + forbidden not in deliver_text + for forbidden in ( + "profile_approve_change", + "profile_record_approval_decision", + "approve-change", + "record-approval-decision", + "write_create_only", + "approval_decision", + ) + ) + approval_workflows = { + path.name + for path in WORKFLOWS_ROOT.glob("*.yaml") + if "approve-change" in path.read_text() + } + assert approval_workflows == {"approve-change.yaml", "deliver-story.yaml"} + deliver = yaml.safe_load((WORKFLOWS_ROOT / "deliver-story.yaml").read_text()) + approval_calls = [ + step + for step in deliver["steps"] + if step.get("config", {}).get("workflow") == "approve-change.yaml" + ] + assert len(approval_calls) == 1 + assert approval_calls[0]["id"] == "approve" + assert approval_calls[0]["primitive"] == "call-agent" + assert all( + "$" not in step.get("config", {}).get("workflow", "") + for step in deliver["steps"] + ) decision_source = ( PYTHON_ROOT / "beddel_profile_dev" / "tools" / "approval_decision.py" ).read_text() diff --git a/kits/profile-dev-kit/tests/test_deliver.py b/kits/profile-dev-kit/tests/test_deliver.py new file mode 100644 index 0000000..3fdba71 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_deliver.py @@ -0,0 +1,254 @@ +"""Unit coverage for chained Delivery gates and Approval reconciliation.""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest +from beddel_profile_dev._errors import PROFILE_IMPACT_UNBOUND, ProfileError +from beddel_profile_dev._io import records_root +from beddel_profile_dev.deliver import ( + IMPLEMENT_SLOTS, + STAGES, + complete, + next_stage, + resume, +) +from beddel_profile_dev.tools.deliver import ( + profile_deliver_approval, + profile_deliver_gate, +) +from beddel_profile_dev.tools.stage_outcome import profile_record_stage_outcome + +from ._helpers import commit_all, git, init_repo + +# fmt: off +STORY = "7-7-deliver-story-workflow-chains-stages-and-resumes" + + +def _repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + monkeypatch.setenv("HOME", str(tmp_path / "home")) + repo = init_repo(tmp_path / "repo") + (repo / "README.md").write_text("fixture\n") + commit_all(repo) + git(repo, "switch", "-q", "-c", "feature/test") + return repo + + +def _record(repo: Path, stage: str, outcome: str, reason: str) -> None: + profile_record_stage_outcome( + str(repo), STORY, stage, outcome, reason, {"stage": stage} + ) + + +def test_contracts_and_resume_boundaries() -> None: + assert STAGES == ("plan", "implement", "verify-and-fix", "approval", "integrate") + assert IMPLEMENT_SLOTS == 33 + assert complete("plan", "succeeded", "spec.created") + assert not complete("plan", "succeeded", "spec.incomplete") + assert next_stage("integrate") is None + assert resume([]) == ("plan", False) + assert resume([{"stage": "implement", "outcome": "succeeded", "reason": "unit.committed"}]) == ("implement", False) + assert resume([{"stage": "integrate", "outcome": "succeeded", "reason": "integrate.complete"}]) == (None, True) + + +def test_gate_advances_from_complete_stage_and_stops_unrecorded( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert start["go"] == "plan" + stopped = profile_deliver_gate(str(repo), STORY, "all", "plan", start) + assert stopped["outcome"] == "failed" + assert stopped["reason"] == "stage.unrecorded" + + _record(repo, "plan", "succeeded", "spec.created") + after_plan = profile_deliver_gate(str(repo), STORY, "all", "plan", start) + assert after_plan["go"] == "implement" + _record(repo, "implement", "succeeded", "units.complete") + after_implement = profile_deliver_gate( + str(repo), STORY, "all", "implement", after_plan + ) + assert after_implement["go"] == "verify-and-fix" + + +def test_gate_rejects_forged_state_and_maps_incomplete_stage( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + forged = dict(start, story_key="other-story") + result = profile_deliver_gate(str(repo), STORY, "all", "plan", forged) + assert result["reason"] == "state.mismatch" + _record(repo, "plan", "succeeded", "spec.created") + _record(repo, "implement", "succeeded", "unit.committed") + result = profile_deliver_gate(str(repo), STORY, "all", "implement", start | {"go": "implement"}) + assert result["reason"] == "stage.incomplete" + + +def test_gate_maps_unreadable_record(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + root = records_root(repo, "records/profile-dev") / "stage-outcomes" + root.mkdir(parents=True) + (root / "1.json").write_text(json.dumps({"record": 1})) + result = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert result["reason"] == "records.unreadable" + + +def test_approval_check_stops_without_committed_decision( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + gate = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + gate.update(go="approval") + monkeypatch.setattr( + "beddel_profile_dev.tools.deliver.profile_read_approval", + lambda *args: {"approval": None}, + ) + monkeypatch.setattr( + "beddel_profile_dev.tools.deliver.profile_read_report", + lambda *args: { + "report": {"revision": "a" * 40}, + "history": [{"ref": "changes/c1/report/3.json"}], + }, + ) + result = profile_deliver_approval( + str(repo), STORY, "c1", git(repo, "branch", "--show-current"), "tester", "check", gate + ) + assert result["outcome"] == "stopped" + assert result["reason"] == "approval.awaiting_owner" + + +def test_approval_settle_reuses_recorded_approval( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + prior: dict[str, Any] = { + "stage": "approval", + "phase": "check", + "story_key": STORY, + "repo": str(repo.resolve()), + "change_id": "c1", + "branch": "main", + "revision": "a" * 40, + "outcome": None, + "refs": {}, + } + monkeypatch.setattr( + "beddel_profile_dev.tools.deliver.profile_read_approval", + lambda *args: {"approval": {"revision": "a" * 40}}, + ) + result = profile_deliver_approval( + str(repo), STORY, "c1", "main", "tester", "settle", prior + ) + assert result["outcome"] == "succeeded" + assert result["reason"] == "approval.recorded" + + +def _gate_for_approval(repo: Path) -> dict[str, Any]: + state = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + state["go"] = "approval" + return state + + +def test_start_reports_complete_delivery_without_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + for stage, reason in (("plan", "spec.created"), ("implement", "units.complete"), ("verify-and-fix", "verdict.consistent"), ("approval", "approval.recorded"), ("integrate", "integrate.complete")): + _record(repo, stage, "succeeded", reason) + result = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert (result["outcome"], result["reason"], result["go"], result["invoked"]) == ("succeeded", "deliver.complete", None, "") + + +def test_approval_check_reuses_recorded_approval(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: {"approval": {"revision": "a" * 40}}) + result = profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", _gate_for_approval(repo)) + assert (result["outcome"], result["reason"], result["refs"]["invoked"]) == ("succeeded", "approval.reused", False) + + +def test_approval_check_finds_committed_decision(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + from beddel_profile_dev.tools.approval_decision import approval_path + + repo = _repo(tmp_path, monkeypatch) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: {"approval": None}) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: {"report": {"revision": "a" * 40}, "history": [{"ref": "changes/c1/report/1.json"}]}) + path = repo / approval_path("c1", 1) + path.parent.mkdir(parents=True) + path.write_text("decision: approved\n") + git(repo, "add", "--", approval_path("c1", 1)) + git(repo, "commit", "-q", "-m", "decision") + result = profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", _gate_for_approval(repo)) + assert result["approve"] is True + assert result["revision"] == "a" * 40 + assert result["refs"]["decision_path"] == approval_path("c1", 1) + assert len(result["decision_commit"]) == 40 + + +def test_approval_check_refusals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + gate = _gate_for_approval(repo) + assert profile_deliver_approval(str(repo), STORY, "bad/id", "feature/test", "tester", "check", gate)["reason"] == "change_id.invalid" + assert profile_deliver_approval(str(repo), STORY, "c1", "bad branch", "tester", "check", gate)["reason"] == "branch.invalid" + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "\x00", "check", gate)["reason"] == "actor.invalid" + assert profile_deliver_approval(str(repo), STORY, "c1", "other", "tester", "check", gate)["reason"] == "branch.not_current" + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: (_ for _ in ()).throw(ProfileError(PROFILE_IMPACT_UNBOUND, "absent"))) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "report.absent" + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: (_ for _ in ()).throw(ProfileError("X", "invalid"))) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "approval.invalid" + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: {"approval": None}) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: (_ for _ in ()).throw(ProfileError("X", "unreadable"))) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "report.unreadable" + + +def test_start_refuses_invalid_request_and_story_key(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + invalid_request = profile_deliver_gate(str(repo), STORY, "bad", "start", {}) + invalid_story = profile_deliver_gate(str(repo), "BAD KEY", "all", "start", {}) + assert (invalid_request["outcome"], invalid_request["reason"]) == ("refused", "request.invalid") + assert (invalid_story["story_key"], invalid_story["reason"]) == (None, "story_key.invalid") + + +def test_gate_maps_unrecorded_incomplete_and_failed_stages(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert profile_deliver_gate(str(repo), STORY, "all", "plan", start)["reason"] == "stage.unrecorded" + _record(repo, "plan", "succeeded", "spec.created") + after_plan = profile_deliver_gate(str(repo), STORY, "all", "plan", start) + _record(repo, "implement", "succeeded", "unit.committed") + assert profile_deliver_gate(str(repo), STORY, "all", "implement", after_plan)["reason"] == "stage.incomplete" + repo2 = _repo(tmp_path / "failed", monkeypatch) + start2 = profile_deliver_gate(str(repo2), STORY, "all", "start", {}) + _record(repo2, "plan", "failed", "agent.failed") + failed = profile_deliver_gate(str(repo2), STORY, "all", "plan", start2) + assert (failed["outcome"], failed["reason"]) == ("failed", "stage.failed") + + +def test_gate_rejects_forged_prior_and_unreadable_records(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert profile_deliver_gate(str(repo), STORY, "all", "plan", None)["reason"] == "gate.failed" + assert profile_deliver_gate(str(repo), STORY, "all", "plan", {**start, "repo": "other"})["reason"] == "state.mismatch" + root = records_root(repo, "records/profile-dev") / "stage-outcomes" + root.mkdir(parents=True) + (root / "1.json").write_text("[]") + assert profile_deliver_gate(str(repo), STORY, "all", "start", {})["reason"] == "records.unreadable" + + +def test_resume_point_from_latest_story_record() -> None: + assert resume([]) == ("plan", False) + assert resume([{"stage": "implement", "outcome": "failed", "reason": "agent.failed"}]) == ("implement", False) + assert resume([{"stage": "implement", "outcome": "succeeded", "reason": "unit.committed"}]) == ("implement", False) + assert resume([{"stage": "verify-and-fix", "outcome": "succeeded", "reason": "verdict.consistent"}]) == ("approval", False) + assert resume([{"stage": "integrate", "outcome": "succeeded", "reason": "integrate.complete"}]) == (None, True) + + +def test_start_resumes_after_latest_complete_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + _record(repo, "plan", "succeeded", "spec.reused") + first = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert first["go"] == "implement" + _record(repo, "implement", "succeeded", "units.complete") + second = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert second["go"] == "verify-and-fix" diff --git a/kits/profile-dev-kit/tests/test_deliver_workflow.py b/kits/profile-dev-kit/tests/test_deliver_workflow.py new file mode 100644 index 0000000..d5cd500 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_deliver_workflow.py @@ -0,0 +1,278 @@ +"""Shape checks for the statically chained Deliver Story workflow.""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import yaml +from beddel.domain.parser import WorkflowParser +from beddel_profile_dev.deliver import IMPLEMENT_SLOTS, STAGES + +# fmt: off +KIT_ROOT = Path(__file__).resolve().parents[1] +WORKFLOW = KIT_ROOT / "workflows" / "deliver-story.yaml" +STATIC = {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} + + +def _document() -> dict[str, Any]: + value = yaml.safe_load(WORKFLOW.read_text()) + assert isinstance(value, dict) + return value + + +def test_deliver_workflow_shape() -> None: + workflow = WorkflowParser.parse(WORKFLOW.read_text()) + assert workflow.id == "deliver-story" + assert workflow.requires_kits == [ + "profile-dev-kit", + "agent-kiro-kit", + "tools-git-kit", + "tools-github-kit", + ] + assert workflow.allowed_tools == [ + "profile_deliver_gate", + "profile_deliver_approval", + "profile_record_stage_outcome", + ] + document = _document() + steps = document["steps"] + implement = [step for step in steps if str(step["id"]).startswith("implement_")] + assert len(implement) == IMPLEMENT_SLOTS + assert [step["id"] for step in implement] == [f"implement_{n}" for n in range(1, 34)] + calls = [ + step["config"]["workflow"] + for step in steps + if step.get("primitive") == "call-agent" + ] + assert set(calls) == STATIC + assert calls.count("approve-change.yaml") == 1 + assert all("$" not in name for name in calls) + assert "agent-exec" in document["description"] + assert "kiro-cli" in document["description"] + + +def test_stage_workflows_state_their_coding_agent() -> None: + for name in ("plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml"): + document = yaml.safe_load((KIT_ROOT / "workflows" / name).read_text()) + assert "Coding agent" in document["description"] + agents = [step for step in document["steps"] if step.get("primitive") == "agent-exec"] + assert agents + assert all(step["config"]["adapter"] == "kiro-cli" for step in agents) + assert all(step["config"]["prompt"].startswith("You are the Coding agent") for step in agents) + assert STAGES == ("plan", "implement", "verify-and-fix", "approval", "integrate") + + +def test_stage_calls_use_declared_inputs() -> None: + document = _document() + expected = { + "plan-story.yaml": {"repo_path", "story_key", "story_path", "guide_path", "spec_path"}, + "implement-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_ref"}, + "verify-and-fix-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_ref", "change_id", "inventory_version"}, + "approve-change.yaml": {"repo_path", "change_id", "revision", "actor", "decision_commit"}, + "integrate-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_branch", "remote", "change_id", "parent_path", "parent_branch", "status_path"}, + } + for step in document["steps"]: + if step.get("primitive") != "call-agent": + continue + name = step["config"]["workflow"] + assert set(step["config"]["inputs"]) == expected[name] + + +import asyncio +import json +import sys + +import pytest +from beddel.domain.executor import WorkflowExecutor +from beddel.domain.kit import parse_kit_manifest +from beddel.domain.models import AgentResult, 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, stub_gate_tools +from .test_integrate_workflow import _fixture, _Host + +sys.path.insert(0, str(KIT_ROOT.parent / "tools-git-kit" / "python")) +sys.path.insert(0, str(KIT_ROOT.parent / "tools-github-kit" / "python")) + +STORY = "7-7-deliver-story-workflow-chains-stages-and-resumes" +BRANCH = "feat/work" + + +def _spec() -> bytes: + return ( + f"---\nstory_key: {STORY}\nrequirements: [FR-13]\n" + "repositories: [.]\ndependencies: []\nsize_cap_bytes: 64000\n" + 'units:\n - id: u1\n paths: [src/delivered.py]\n subject: "feat: deliver"\n---\n\n# Fixture\n' + ).encode() + + +class _CodingAgent: + def __init__(self, repo: Path) -> None: + self.repo = repo + self.calls = 0 + + async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: + self.calls += 1 + if "one plan stage" in prompt: + (self.repo / "specs" / "story.md").write_bytes(_spec()) + elif "one implementation unit" in prompt: + (self.repo / "src" / "delivered.py").write_text("delivered = True\n") + return AgentResult(0, "", [], [], {}, "fake") + + +def _workflow_run( + env: dict[str, Any], + agent: _CodingAgent, + host: _Host, + monkeypatch: pytest.MonkeyPatch, + approval: dict[str, Any] | None, + report: dict[str, Any], + approve_change: Any, + *, + stage: str = "all", + actor: str = "owner", +) -> dict[str, Any]: + from beddel_profile_dev.tools import deliver as deliver_tools + from beddel_profile_dev.tools import integrate as integrate_tools + + monkeypatch.setattr(deliver_tools, "profile_read_approval", lambda *a: {"approval": approval["value"] if isinstance(approval, dict) and "value" in approval else approval}) + monkeypatch.setattr(deliver_tools, "profile_read_report", lambda *a: {"report": report, "history": [{"ref": "changes/c1/report/1.json", "content": report}]}) + monkeypatch.setattr(integrate_tools, "profile_read_approval", lambda *a: {"approval": approval["value"] if isinstance(approval, dict) and "value" in approval else approval}) + monkeypatch.setattr(integrate_tools, "profile_read_report", lambda *a: {"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(KIT_ROOT.parent / name / "kit.yaml"))) + registry.update({ + "profile_approve_change": approve_change, + "github_pr_create_or_reuse": host.create, + "github_pr_checks_wait": host.checks, + "github_pr_merge": host.merge, + }) + async def consistent(*args: Any, **kwargs: Any) -> dict[str, Any]: + return {"stage": "verify-and-fix", "story_key": STORY, "verify": False, "fix": False, "outcome": "succeeded", "reason": "verdict.consistent", "refs": {"stage": "verify"}} + registry["profile_fix_reconcile"] = consistent + registry["profile_fix_settle"] = consistent + primitive = PrimitiveRegistry() + register_builtins(primitive) + dependencies = DefaultDependencies( + agent_registry={"kiro-cli": agent}, + tool_registry=registry, + registry=primitive, + workflow_loader=lambda name: WorkflowParser.parse((KIT_ROOT / "workflows" / name).read_text()), + ) + inputs = { + "repo_path": str(env["sub"]), "story_key": STORY, "stage": stage, + "story_path": "story.md", "guide_path": "guide.md", "spec_path": "specs/story.md", + "branch": BRANCH, "base_ref": env["base"], "change_id": "c1", "inventory_version": "v1", + "actor": actor, "base_branch": "main", "remote": "origin", "parent_path": str(env["parent"]), + "parent_branch": "main", "status_path": "status.yaml", + } + return asyncio.run(WorkflowExecutor(primitive, deps=dependencies).execute(WorkflowParser.parse(WORKFLOW.read_text()), inputs)) + + +def _deliver_records(home: Path) -> list[dict[str, Any]]: + return [ + json.loads(path.read_text()) + for path in sorted(home.rglob("stage-outcomes/*.json"), key=lambda p: int(p.stem)) + ] + + +def test_end_to_end_stops_for_owner_then_resumes_to_integration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env = _fixture(tmp_path) + sub = env["sub"] + (sub / "story.md").write_text("story\n") + (sub / "guide.md").write_text("guide\n") + (sub / "specs").mkdir() + (sub / "src").mkdir() + (env["parent"] / "status.yaml").write_text(f"x: 1\n {STORY}: review # keep\n") + env["tip"] = commit_all(sub, "delivery inputs") + report = {"revision": env["tip"], "verdict": "consistent"} + approval_box: dict[str, Any] = {"value": None} + calls: list[str] = [] + def approve_change(**kwargs: Any) -> dict[str, Any]: + calls.append(kwargs["actor"]) + if kwargs["actor"] == "owner": + approval_box["value"] = {"revision": report["revision"], "proof": {"commit": kwargs["decision_commit"]}} + return {"ok": True} + monkeypatch.chdir(sub) + monkeypatch.setenv("PATH", stub_gate_tools(tmp_path)) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + host = _Host(env) + agent = _CodingAgent(sub) + first = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + assert first["step_results"]["record"]["result"]["reason"] == "stage.stopped" + report["revision"] = git(sub, "rev-parse", "HEAD") + assert calls == [] + decision = sub / ".beddel-profile" / "approvals" / "c1" / "1.yaml" + decision.parent.mkdir(parents=True) + decision.write_text("decision: approved\n") + git(sub, "add", "--", ".beddel-profile/approvals") + git(sub, "commit", "-q", "-m", "owner decision") + env["tip"] = git(sub, "rev-parse", "HEAD") + wrong = _workflow_run(env, agent, host, monkeypatch, approval_box["value"], report, approve_change, actor="wrong") + assert wrong["step_results"]["record"]["result"]["reason"] == "stage.refused" + assert calls == ["wrong"] + approval_box["value"] = None + right = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + assert right["step_results"]["record"]["result"]["reason"] == "stage.stopped" + assert calls == ["wrong", "owner"] + git(env["parent"], "push", "-q", "origin", "main") + resumed = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + assert resumed["step_results"]["record"]["result"]["reason"] == "deliver.complete" + _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + complete_records = _deliver_records(tmp_path / "home") + assert any(record["reason"] == "deliver.complete" and record["refs"]["invoked"] == "" for record in complete_records) + assert calls == ["wrong", "owner"] + records = _deliver_records(tmp_path / "home") + assert records and all(record["stage"] in {"plan", "implement", "verify-and-fix", "approval", "integrate", "deliver"} for record in records) + assert host.calls.count("merge") == 1 + + +def test_single_stage_runs_only_that_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env = _fixture(tmp_path) + sub = env["sub"] + (sub / "story.md").write_text("story\n") + (sub / "guide.md").write_text("guide\n") + (sub / "specs").mkdir() + (sub / "src").mkdir() + env["tip"] = commit_all(sub, "delivery inputs") + monkeypatch.chdir(sub) + monkeypatch.setenv("PATH", stub_gate_tools(tmp_path)) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + agent = _CodingAgent(sub) + host = _Host(env) + report = {"revision": env["tip"], "verdict": "consistent"} + (sub / "specs" / "story.md").write_bytes(_spec()) + result = _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}, stage="implement") + assert result["step_results"]["record"]["result"]["stage"] == "deliver" + records = _deliver_records(tmp_path / "home") + assert {record["stage"] for record in records} == {"implement", "deliver"} + assert records[-1]["stage"] == "deliver" + + +def test_stage_failure_unrecorded_stage_and_fix_bound_do_not_advance(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env = _fixture(tmp_path) + sub = env["sub"] + (sub / "story.md").write_text("story\n") + (sub / "guide.md").write_text("guide\n") + (sub / "specs").mkdir() + (sub / "src").mkdir() + env["tip"] = commit_all(sub, "delivery inputs") + monkeypatch.chdir(sub) + monkeypatch.setenv("PATH", stub_gate_tools(tmp_path)) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + class FailingAgent(_CodingAgent): + async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: + self.calls += 1 + if "one plan stage" in prompt: + (self.repo / "specs" / "story.md").write_bytes(_spec()) + return AgentResult(1, "failed", [], [], {}, "fake") + agent = FailingAgent(sub) + host = _Host(env) + report = {"revision": env["tip"], "verdict": "consistent"} + result = _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) + assert result["step_results"]["record"]["result"]["reason"] == "stage.failed" + assert host.calls == [] diff --git a/kits/profile-dev-kit/tests/test_manifest.py b/kits/profile-dev-kit/tests/test_manifest.py index dcab2f3..58e693f 100644 --- a/kits/profile-dev-kit/tests/test_manifest.py +++ b/kits/profile-dev-kit/tests/test_manifest.py @@ -40,6 +40,8 @@ "profile_integrate_prepare", "profile_integrate_reconcile", "profile_integrate_settle", + "profile_deliver_gate", + "profile_deliver_approval", ] @@ -117,6 +119,7 @@ def test_manifest_declares_all_workflows_and_python_targets() -> None: "verify-and-fix-story", "plan-story", "integrate-story", + "deliver-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 40dbf41..f2cff98 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"))) == 12 + assert len(list(WORKFLOWS_DIR.glob("*.yaml"))) == 13 def test_review_step_outlives_the_largest_accepted_review_budget() -> None: diff --git a/kits/profile-dev-kit/workflows/deliver-story.yaml b/kits/profile-dev-kit/workflows/deliver-story.yaml new file mode 100644 index 0000000..9df29a0 --- /dev/null +++ b/kits/profile-dev-kit/workflows/deliver-story.yaml @@ -0,0 +1,139 @@ +id: deliver-story +name: Deliver Story +description: Entry Delivery workflow that runs plan, implement, verify-and-fix, Approval and integrate in order or runs one selected stage. Plan, implement and fix use a Coding agent through the agent-exec step of each stage workflow with the kiro-cli adapter. Approval waits for the owner's committed decision, runs approve-change once, and integrate requires the recorded Approval. It never decides Approval or pushes the parent. +version: "1.0" +requires_kits: [profile-dev-kit, agent-kiro-kit, tools-git-kit, tools-github-kit] +allowed_tools: [profile_deliver_gate, profile_deliver_approval, profile_record_stage_outcome] +inputs: [repo_path, story_key, stage, story_path, guide_path, spec_path, branch, base_ref, change_id, inventory_version, actor, base_branch, remote, parent_path, parent_branch, status_path] +steps: + - id: start + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: start, prior: {}} + - id: plan + primitive: call-agent + if: "$stepResult.start.result.go == plan" + execution_strategy: {type: skip} + config: + workflow: plan-story.yaml + inputs: {repo_path: "$input.repo_path", story_key: "$input.story_key", story_path: "$input.story_path", guide_path: "$input.guide_path", spec_path: "$input.spec_path"} + - id: after_plan + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: plan, prior: "$stepResult.start.result"} + - &implement_call + id: implement_1 + primitive: call-agent + if: "$stepResult.after_plan.result.go == implement" + execution_strategy: {type: skip} + config: + workflow: implement-story.yaml + inputs: &implement_inputs {repo_path: "$input.repo_path", story_key: "$input.story_key", spec_path: "$input.spec_path", branch: "$input.branch", base_ref: "$input.base_ref"} + - {<<: *implement_call, id: implement_2, if: "$stepResult.implement_1.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_3, if: "$stepResult.implement_2.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_4, if: "$stepResult.implement_3.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_5, if: "$stepResult.implement_4.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_6, if: "$stepResult.implement_5.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_7, if: "$stepResult.implement_6.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_8, if: "$stepResult.implement_7.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_9, if: "$stepResult.implement_8.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_10, if: "$stepResult.implement_9.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_11, if: "$stepResult.implement_10.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_12, if: "$stepResult.implement_11.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_13, if: "$stepResult.implement_12.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_14, if: "$stepResult.implement_13.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_15, if: "$stepResult.implement_14.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_16, if: "$stepResult.implement_15.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_17, if: "$stepResult.implement_16.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_18, if: "$stepResult.implement_17.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_19, if: "$stepResult.implement_18.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_20, if: "$stepResult.implement_19.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_21, if: "$stepResult.implement_20.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_22, if: "$stepResult.implement_21.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_23, if: "$stepResult.implement_22.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_24, if: "$stepResult.implement_23.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_25, if: "$stepResult.implement_24.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_26, if: "$stepResult.implement_25.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_27, if: "$stepResult.implement_26.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_28, if: "$stepResult.implement_27.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_29, if: "$stepResult.implement_28.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_30, if: "$stepResult.implement_29.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_31, if: "$stepResult.implement_30.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_32, if: "$stepResult.implement_31.record.result.outcome == succeeded"} + - {<<: *implement_call, id: implement_33, if: "$stepResult.implement_32.record.result.outcome == succeeded"} + - id: after_implement + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: implement, prior: "$stepResult.after_plan.result"} + - id: verify + primitive: call-agent + if: "$stepResult.after_implement.result.go == verify-and-fix" + execution_strategy: {type: skip} + config: + workflow: verify-and-fix-story.yaml + inputs: {repo_path: "$input.repo_path", story_key: "$input.story_key", spec_path: "$input.spec_path", branch: "$input.branch", base_ref: "$input.base_ref", change_id: "$input.change_id", inventory_version: "$input.inventory_version"} + - id: after_verify + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: verify-and-fix, prior: "$stepResult.after_implement.result"} + - id: approval_check + primitive: tool + if: "$stepResult.after_verify.result.go == approval" + execution_strategy: {type: fail} + config: + tool: profile_deliver_approval + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", change_id: "$input.change_id", branch: "$input.branch", actor: "$input.actor", phase: check, prior: "$stepResult.after_verify.result"} + - id: approve + primitive: call-agent + if: "$stepResult.approval_check.result.approve == true" + execution_strategy: {type: skip} + config: + workflow: approve-change.yaml + inputs: {repo_path: "$input.repo_path", change_id: "$input.change_id", revision: "$stepResult.approval_check.result.revision", actor: "$input.actor", decision_commit: "$stepResult.approval_check.result.decision_commit"} + - id: approval_settle + primitive: tool + if: "$stepResult.after_verify.result.go == approval" + execution_strategy: {type: fail} + config: + tool: profile_deliver_approval + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", change_id: "$input.change_id", branch: "$input.branch", actor: "$input.actor", phase: settle, prior: "$stepResult.approval_check.result"} + - id: record_approval + primitive: tool + if: "$stepResult.after_verify.result.go == approval" + execution_strategy: {type: fail} + config: + tool: profile_record_stage_outcome + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", stage: approval, outcome: "$stepResult.approval_settle.result.outcome", reason: "$stepResult.approval_settle.result.reason", refs: "$stepResult.approval_settle.result.refs"} + - id: after_approval + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: approval, prior: "$stepResult.after_verify.result"} + - id: integrate + primitive: call-agent + if: "$stepResult.after_approval.result.go == integrate" + execution_strategy: {type: skip} + config: + workflow: integrate-story.yaml + inputs: {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"} + - id: after_integrate + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_deliver_gate + arguments: {repo_path: "$input.repo_path", story_key: "$input.story_key", request: "$input.stage", step: integrate, prior: "$stepResult.after_approval.result"} + - id: record + primitive: tool + execution_strategy: {type: fail} + config: + tool: profile_record_stage_outcome + arguments: {repo_path: "$input.repo_path", story_key: "$stepResult.after_integrate.result.story_key", stage: deliver, outcome: "$stepResult.after_integrate.result.outcome", reason: "$stepResult.after_integrate.result.reason", refs: "$stepResult.after_integrate.result.refs"} From cea8606a44ac39b5f45491afdb34978996998168 Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Thu, 12 Nov 2026 13:54:58 -0300 Subject: [PATCH 2/3] fix(profile-dev-kit): address review round 1 for deliver workflow (Story 7.7) --- .../beddel_profile_dev/tools/deliver.py | 18 +- kits/profile-dev-kit/tests/test_approval.py | 2 + kits/profile-dev-kit/tests/test_deliver.py | 92 +++----- .../tests/test_deliver_workflow.py | 220 ++++++++++-------- .../tests/test_stage_outcome.py | 2 +- 5 files changed, 171 insertions(+), 163 deletions(-) diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py index 98a59fd..9cd0329 100644 --- a/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py @@ -11,7 +11,13 @@ from beddel.tools import beddel_tool from beddel_profile_dev._errors import PROFILE_IMPACT_UNBOUND, ProfileError -from beddel_profile_dev._io import blob_at, git_run, real_repo_root, records_root +from beddel_profile_dev._io import ( + git_run, + path_present, + real_repo_root, + records_root, +) +from beddel_profile_dev.config import load_config from beddel_profile_dev.deliver import REQUESTS, STAGES, complete, next_stage, resume from beddel_profile_dev.implement import branch_reason from beddel_profile_dev.integrate import APPROVALS_DIR @@ -43,7 +49,7 @@ def _refs(**values: Any) -> dict[str, Any]: def _records(root: Path) -> list[dict[str, Any]]: - directory = root / ("stage-" + "outcomes") + directory = root / "stage-outcomes" # The gate is the other reader of Stage-outcome records. if not directory.exists(): return [] if not directory.is_dir(): raise _BAD found: list[dict[str, Any]] = [] @@ -72,7 +78,7 @@ def profile_deliver_gate(repo_path: str, story_key: str, request: str, step: str repo = real_repo_root(repo_path) if not _valid_story(story_key): return _state(repo, None, request, step, outcome="refused", reason="story_key.invalid", refs=_refs(request=request, stage=step, stage_reason="story_key.invalid")) if request not in REQUESTS: return _state(repo, story_key, request, step, outcome="refused", reason="request.invalid", refs=_refs(request=request, stage=step, stage_reason="request.invalid")) - root = records_root(repo, "records/profile-dev") + root = records_root(repo, load_config().records_dir) g = lambda **values: _state(repo, story_key, request, step, **values) f = lambda reason, **values: _failed(repo, story_key, request, step, reason, **values) if step == "start": @@ -138,7 +144,11 @@ def profile_deliver_approval(repo_path: str, story_key: str, change_id: str, bra if match is None or not isinstance(revision, str): return a("failed", "report.unreadable") path = f"{APPROVALS_DIR}{change_id}/{match.group(1)}.yaml" refs = {"change_id": change_id, "branch": branch, "revision": revision, "report": latest.get("ref"), "decision_path": path, "decision_commit": None, "approval": None, "invoked": True} - if blob_at(repo, "HEAD", path) is None: return a("stopped", "approval.awaiting_owner", revision=revision, refs=refs) + try: + present = path_present(repo, "HEAD", path) + except ProfileError: + return a("failed", "decision.unreadable", revision=revision, refs=refs) + if not present: return a("stopped", "approval.awaiting_owner", revision=revision, refs=refs) commit = _git_text(git_run(repo, "log", "-1", "--format=%H", "--diff-filter=A", "HEAD", "--", path)) if not commit or not re.fullmatch(r"[0-9a-f]{40}", commit): return a("failed", "decision.unreadable", revision=revision, refs=refs) refs["decision_commit"] = commit diff --git a/kits/profile-dev-kit/tests/test_approval.py b/kits/profile-dev-kit/tests/test_approval.py index 1ec85d0..291d6a6 100644 --- a/kits/profile-dev-kit/tests/test_approval.py +++ b/kits/profile-dev-kit/tests/test_approval.py @@ -282,6 +282,7 @@ def test_structural_approval_boundaries() -> None: "record-approval-decision", "write_create_only", "approval_decision", + "approval.json", ) ) for deliver_source in ( @@ -298,6 +299,7 @@ def test_structural_approval_boundaries() -> None: "record-approval-decision", "write_create_only", "approval_decision", + "approval.json", ) ) approval_workflows = { diff --git a/kits/profile-dev-kit/tests/test_deliver.py b/kits/profile-dev-kit/tests/test_deliver.py index 3fdba71..41f7333 100644 --- a/kits/profile-dev-kit/tests/test_deliver.py +++ b/kits/profile-dev-kit/tests/test_deliver.py @@ -2,20 +2,15 @@ from __future__ import annotations -import json +from dataclasses import replace from pathlib import Path from typing import Any import pytest from beddel_profile_dev._errors import PROFILE_IMPACT_UNBOUND, ProfileError from beddel_profile_dev._io import records_root -from beddel_profile_dev.deliver import ( - IMPLEMENT_SLOTS, - STAGES, - complete, - next_stage, - resume, -) +from beddel_profile_dev.config import load_config +from beddel_profile_dev.deliver import resume from beddel_profile_dev.tools.deliver import ( profile_deliver_approval, profile_deliver_gate, @@ -43,60 +38,6 @@ def _record(repo: Path, stage: str, outcome: str, reason: str) -> None: ) -def test_contracts_and_resume_boundaries() -> None: - assert STAGES == ("plan", "implement", "verify-and-fix", "approval", "integrate") - assert IMPLEMENT_SLOTS == 33 - assert complete("plan", "succeeded", "spec.created") - assert not complete("plan", "succeeded", "spec.incomplete") - assert next_stage("integrate") is None - assert resume([]) == ("plan", False) - assert resume([{"stage": "implement", "outcome": "succeeded", "reason": "unit.committed"}]) == ("implement", False) - assert resume([{"stage": "integrate", "outcome": "succeeded", "reason": "integrate.complete"}]) == (None, True) - - -def test_gate_advances_from_complete_stage_and_stops_unrecorded( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - repo = _repo(tmp_path, monkeypatch) - start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) - assert start["go"] == "plan" - stopped = profile_deliver_gate(str(repo), STORY, "all", "plan", start) - assert stopped["outcome"] == "failed" - assert stopped["reason"] == "stage.unrecorded" - - _record(repo, "plan", "succeeded", "spec.created") - after_plan = profile_deliver_gate(str(repo), STORY, "all", "plan", start) - assert after_plan["go"] == "implement" - _record(repo, "implement", "succeeded", "units.complete") - after_implement = profile_deliver_gate( - str(repo), STORY, "all", "implement", after_plan - ) - assert after_implement["go"] == "verify-and-fix" - - -def test_gate_rejects_forged_state_and_maps_incomplete_stage( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - repo = _repo(tmp_path, monkeypatch) - start = profile_deliver_gate(str(repo), STORY, "all", "start", {}) - forged = dict(start, story_key="other-story") - result = profile_deliver_gate(str(repo), STORY, "all", "plan", forged) - assert result["reason"] == "state.mismatch" - _record(repo, "plan", "succeeded", "spec.created") - _record(repo, "implement", "succeeded", "unit.committed") - result = profile_deliver_gate(str(repo), STORY, "all", "implement", start | {"go": "implement"}) - assert result["reason"] == "stage.incomplete" - - -def test_gate_maps_unreadable_record(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - repo = _repo(tmp_path, monkeypatch) - root = records_root(repo, "records/profile-dev") / "stage-outcomes" - root.mkdir(parents=True) - (root / "1.json").write_text(json.dumps({"record": 1})) - result = profile_deliver_gate(str(repo), STORY, "all", "start", {}) - assert result["reason"] == "records.unreadable" - - def test_approval_check_stops_without_committed_decision( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -119,6 +60,16 @@ def test_approval_check_stops_without_committed_decision( ) assert result["outcome"] == "stopped" assert result["reason"] == "approval.awaiting_owner" + from beddel_profile_dev.tools.approval_decision import approval_path + + path = repo / approval_path("c1", 3) + path.parent.mkdir(parents=True) + path.write_text("decision: approved\n") + git(repo, "add", "--", approval_path("c1", 3)) + staged = profile_deliver_approval( + str(repo), STORY, "c1", git(repo, "branch", "--show-current"), "tester", "check", gate + ) + assert staged["reason"] == "approval.awaiting_owner" def test_approval_settle_reuses_recorded_approval( @@ -153,6 +104,19 @@ def _gate_for_approval(repo: Path) -> dict[str, Any]: return state +def test_gate_uses_configured_records_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = _repo(tmp_path, monkeypatch) + from beddel_profile_dev.tools import deliver as deliver_tools + from beddel_profile_dev.tools import stage_outcome + + config = replace(load_config(), records_dir="records/custom") + monkeypatch.setattr(deliver_tools, "load_config", lambda: config) + monkeypatch.setattr(stage_outcome, "load_config", lambda: config) + _record(repo, "plan", "succeeded", "spec.created") + result = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + assert result["go"] == "implement" + + def test_start_reports_complete_delivery_without_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: repo = _repo(tmp_path, monkeypatch) for stage, reason in (("plan", "spec.created"), ("implement", "units.complete"), ("verify-and-fix", "verdict.consistent"), ("approval", "approval.recorded"), ("integrate", "integrate.complete")): @@ -199,7 +163,9 @@ def test_approval_check_refusals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "approval.invalid" monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: {"approval": None}) monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: (_ for _ in ()).throw(ProfileError("X", "unreadable"))) - assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "report.unreadable" + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: {"report": {"revision": "a" * 40}, "history": [{"ref": "changes/c1/report/3.json"}]}) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.path_present", lambda *args: (_ for _ in ()).throw(ProfileError("X", "git"))) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "decision.unreadable" def test_start_refuses_invalid_request_and_story_key(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: diff --git a/kits/profile-dev-kit/tests/test_deliver_workflow.py b/kits/profile-dev-kit/tests/test_deliver_workflow.py index d5cd500..3de59a7 100644 --- a/kits/profile-dev-kit/tests/test_deliver_workflow.py +++ b/kits/profile-dev-kit/tests/test_deliver_workflow.py @@ -12,44 +12,16 @@ # fmt: off KIT_ROOT = Path(__file__).resolve().parents[1] WORKFLOW = KIT_ROOT / "workflows" / "deliver-story.yaml" -STATIC = {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} - - -def _document() -> dict[str, Any]: - value = yaml.safe_load(WORKFLOW.read_text()) - assert isinstance(value, dict) - return value - def test_deliver_workflow_shape() -> None: - workflow = WorkflowParser.parse(WORKFLOW.read_text()) - assert workflow.id == "deliver-story" - assert workflow.requires_kits == [ - "profile-dev-kit", - "agent-kiro-kit", - "tools-git-kit", - "tools-github-kit", - ] - assert workflow.allowed_tools == [ - "profile_deliver_gate", - "profile_deliver_approval", - "profile_record_stage_outcome", - ] - document = _document() + document = yaml.safe_load(WORKFLOW.read_text()) + assert document["id"] == "deliver-story" + assert document["requires_kits"] == ["profile-dev-kit", "agent-kiro-kit", "tools-git-kit", "tools-github-kit"] steps = document["steps"] - implement = [step for step in steps if str(step["id"]).startswith("implement_")] - assert len(implement) == IMPLEMENT_SLOTS - assert [step["id"] for step in implement] == [f"implement_{n}" for n in range(1, 34)] - calls = [ - step["config"]["workflow"] - for step in steps - if step.get("primitive") == "call-agent" - ] - assert set(calls) == STATIC - assert calls.count("approve-change.yaml") == 1 - assert all("$" not in name for name in calls) - assert "agent-exec" in document["description"] - assert "kiro-cli" in document["description"] + assert len([step for step in steps if str(step["id"]).startswith("implement_")]) == IMPLEMENT_SLOTS + calls = [step["config"]["workflow"] for step in steps if step.get("primitive") == "call-agent"] + assert set(calls) == {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} + assert calls.count("approve-change.yaml") == 1 and all("$" not in call for call in calls) def test_stage_workflows_state_their_coding_agent() -> None: @@ -57,31 +29,19 @@ def test_stage_workflows_state_their_coding_agent() -> None: document = yaml.safe_load((KIT_ROOT / "workflows" / name).read_text()) assert "Coding agent" in document["description"] agents = [step for step in document["steps"] if step.get("primitive") == "agent-exec"] - assert agents - assert all(step["config"]["adapter"] == "kiro-cli" for step in agents) + assert agents and all(step["config"]["adapter"] == "kiro-cli" for step in agents) assert all(step["config"]["prompt"].startswith("You are the Coding agent") for step in agents) assert STAGES == ("plan", "implement", "verify-and-fix", "approval", "integrate") -def test_stage_calls_use_declared_inputs() -> None: - document = _document() - expected = { - "plan-story.yaml": {"repo_path", "story_key", "story_path", "guide_path", "spec_path"}, - "implement-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_ref"}, - "verify-and-fix-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_ref", "change_id", "inventory_version"}, - "approve-change.yaml": {"repo_path", "change_id", "revision", "actor", "decision_commit"}, - "integrate-story.yaml": {"repo_path", "story_key", "spec_path", "branch", "base_branch", "remote", "change_id", "parent_path", "parent_branch", "status_path"}, - } - for step in document["steps"]: - if step.get("primitive") != "call-agent": - continue - name = step["config"]["workflow"] - assert set(step["config"]["inputs"]) == expected[name] +def test_stage_call_names_are_static() -> None: + document = yaml.safe_load(WORKFLOW.read_text()) + calls = [step["config"]["workflow"] for step in document["steps"] if step.get("primitive") == "call-agent"] + assert all(call in {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} for call in calls) import asyncio import json -import sys import pytest from beddel.domain.executor import WorkflowExecutor @@ -94,13 +54,9 @@ def test_stage_calls_use_declared_inputs() -> None: from ._helpers import commit_all, git, stub_gate_tools from .test_integrate_workflow import _fixture, _Host -sys.path.insert(0, str(KIT_ROOT.parent / "tools-git-kit" / "python")) -sys.path.insert(0, str(KIT_ROOT.parent / "tools-github-kit" / "python")) - STORY = "7-7-deliver-story-workflow-chains-stages-and-resumes" BRANCH = "feat/work" - def _spec() -> bytes: return ( f"---\nstory_key: {STORY}\nrequirements: [FR-13]\n" @@ -108,7 +64,6 @@ def _spec() -> bytes: 'units:\n - id: u1\n paths: [src/delivered.py]\n subject: "feat: deliver"\n---\n\n# Fixture\n' ).encode() - class _CodingAgent: def __init__(self, repo: Path) -> None: self.repo = repo @@ -120,9 +75,10 @@ async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: (self.repo / "specs" / "story.md").write_bytes(_spec()) elif "one implementation unit" in prompt: (self.repo / "src" / "delivered.py").write_text("delivered = True\n") + elif "verification fix round" in prompt: + (self.repo / "src" / "delivered.py").write_text(f"round = {self.calls}\n") return AgentResult(0, "", [], [], {}, "fake") - def _workflow_run( env: dict[str, Any], agent: _CodingAgent, @@ -134,10 +90,19 @@ def _workflow_run( *, stage: str = "all", actor: str = "owner", + fix_report: dict[str, Any] | None = None, + loader_error: bool = False, ) -> dict[str, Any]: from beddel_profile_dev.tools import deliver as deliver_tools + from beddel_profile_dev.tools import fix as fix_tools from beddel_profile_dev.tools import integrate as integrate_tools + def current_report(repo: str, change: str) -> dict[str, Any]: + revision = git(Path(repo), "rev-parse", "HEAD") + content = {**(fix_report or report), "revision": revision, "verdict": (fix_report or report).get("verdict", "consistent")} + return {"report": content, "history": [{"ref": f"changes/{change}/report/1.json", "content": content}]} + + monkeypatch.setattr(fix_tools, "profile_read_report", current_report) monkeypatch.setattr(deliver_tools, "profile_read_approval", lambda *a: {"approval": approval["value"] if isinstance(approval, dict) and "value" in approval else approval}) monkeypatch.setattr(deliver_tools, "profile_read_report", lambda *a: {"report": report, "history": [{"ref": "changes/c1/report/1.json", "content": report}]}) monkeypatch.setattr(integrate_tools, "profile_read_approval", lambda *a: {"approval": approval["value"] if isinstance(approval, dict) and "value" in approval else approval}) @@ -151,17 +116,18 @@ def _workflow_run( "github_pr_checks_wait": host.checks, "github_pr_merge": host.merge, }) - async def consistent(*args: Any, **kwargs: Any) -> dict[str, Any]: - return {"stage": "verify-and-fix", "story_key": STORY, "verify": False, "fix": False, "outcome": "succeeded", "reason": "verdict.consistent", "refs": {"stage": "verify"}} - registry["profile_fix_reconcile"] = consistent - registry["profile_fix_settle"] = consistent primitive = PrimitiveRegistry() register_builtins(primitive) + def load_workflow(name: str) -> Any: + if loader_error and name == "plan-story.yaml": + raise RuntimeError("loader failed") + return WorkflowParser.parse((KIT_ROOT / "workflows" / name).read_text()) + dependencies = DefaultDependencies( agent_registry={"kiro-cli": agent}, tool_registry=registry, registry=primitive, - workflow_loader=lambda name: WorkflowParser.parse((KIT_ROOT / "workflows" / name).read_text()), + workflow_loader=load_workflow, ) inputs = { "repo_path": str(env["sub"]), "story_key": STORY, "stage": stage, @@ -172,13 +138,14 @@ async def consistent(*args: Any, **kwargs: Any) -> dict[str, Any]: } return asyncio.run(WorkflowExecutor(primitive, deps=dependencies).execute(WorkflowParser.parse(WORKFLOW.read_text()), inputs)) - def _deliver_records(home: Path) -> list[dict[str, Any]]: return [ json.loads(path.read_text()) for path in sorted(home.rglob("stage-outcomes/*.json"), key=lambda p: int(p.stem)) ] +def _tuples(records: list[dict[str, Any]]) -> list[tuple[str, str, str]]: + return [(record["stage"], record["outcome"], record["reason"]) for record in records] def test_end_to_end_stops_for_owner_then_resumes_to_integration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: env = _fixture(tmp_path) @@ -202,8 +169,20 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: monkeypatch.setenv("HOME", str(tmp_path / "home")) host = _Host(env) agent = _CodingAgent(sub) - first = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) - assert first["step_results"]["record"]["result"]["reason"] == "stage.stopped" + from .test_verify_fix_workflow import _install_review + + _install_review(monkeypatch) + _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + phase_one = _deliver_records(tmp_path / "home") + assert _tuples(phase_one) == [ + ("plan", "succeeded", "spec.created"), + ("implement", "succeeded", "unit.committed"), + ("implement", "succeeded", "units.complete"), + ("verify-and-fix", "succeeded", "verdict.consistent"), + ("approval", "stopped", "approval.awaiting_owner"), + ("deliver", "stopped", "stage.stopped"), + ] + calls_after_phase_one = agent.calls report["revision"] = git(sub, "rev-parse", "HEAD") assert calls == [] decision = sub / ".beddel-profile" / "approvals" / "c1" / "1.yaml" @@ -212,67 +191,118 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: git(sub, "add", "--", ".beddel-profile/approvals") git(sub, "commit", "-q", "-m", "owner decision") env["tip"] = git(sub, "rev-parse", "HEAD") - wrong = _workflow_run(env, agent, host, monkeypatch, approval_box["value"], report, approve_change, actor="wrong") - assert wrong["step_results"]["record"]["result"]["reason"] == "stage.refused" + _workflow_run(env, agent, host, monkeypatch, approval_box["value"], report, approve_change, actor="wrong") + wrong_records = _deliver_records(tmp_path / "home") + assert _tuples(wrong_records)[-2:] == [("approval", "refused", "approval.refused"), ("deliver", "refused", "stage.refused")] assert calls == ["wrong"] approval_box["value"] = None - right = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) - assert right["step_results"]["record"]["result"]["reason"] == "stage.stopped" + _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + right_records = _deliver_records(tmp_path / "home") + assert _tuples(right_records)[-4:] == [ + ("approval", "succeeded", "approval.recorded"), + ("integrate", "succeeded", "merge.confirmed"), + ("integrate", "stopped", "pointer.unpublished"), + ("deliver", "stopped", "stage.stopped"), + ] assert calls == ["wrong", "owner"] + assert agent.calls == calls_after_phase_one git(env["parent"], "push", "-q", "origin", "main") - resumed = _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) - assert resumed["step_results"]["record"]["result"]["reason"] == "deliver.complete" + _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) + resumed_records = _deliver_records(tmp_path / "home") + assert _tuples(resumed_records)[-3:] == [ + ("integrate", "succeeded", "merge.reused"), + ("integrate", "succeeded", "integrate.complete"), + ("deliver", "succeeded", "deliver.complete"), + ] + assert agent.calls == calls_after_phase_one + assert host.calls.count("merge") == 1 _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) complete_records = _deliver_records(tmp_path / "home") - assert any(record["reason"] == "deliver.complete" and record["refs"]["invoked"] == "" for record in complete_records) + assert _tuples(complete_records)[-1:] == [("deliver", "succeeded", "deliver.complete")] + assert agent.calls == calls_after_phase_one assert calls == ["wrong", "owner"] - records = _deliver_records(tmp_path / "home") - assert records and all(record["stage"] in {"plan", "implement", "verify-and-fix", "approval", "integrate", "deliver"} for record in records) - assert host.calls.count("merge") == 1 - -def test_single_stage_runs_only_that_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: +def _stage_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[dict[str, Any], Path]: env = _fixture(tmp_path) sub = env["sub"] - (sub / "story.md").write_text("story\n") - (sub / "guide.md").write_text("guide\n") + for name in ("story.md", "guide.md"): + (sub / name).write_text("story\n") (sub / "specs").mkdir() (sub / "src").mkdir() env["tip"] = commit_all(sub, "delivery inputs") monkeypatch.chdir(sub) monkeypatch.setenv("PATH", stub_gate_tools(tmp_path)) monkeypatch.setenv("HOME", str(tmp_path / "home")) + return env, sub + +def test_single_stage_runs_only_that_stage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env, sub = _stage_env(tmp_path, monkeypatch) agent = _CodingAgent(sub) host = _Host(env) report = {"revision": env["tip"], "verdict": "consistent"} (sub / "specs" / "story.md").write_bytes(_spec()) result = _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}, stage="implement") assert result["step_results"]["record"]["result"]["stage"] == "deliver" + assert result["step_results"]["record"]["result"]["outcome"] == "succeeded" + assert result["step_results"]["record"]["result"]["reason"] == "stage.complete" records = _deliver_records(tmp_path / "home") - assert {record["stage"] for record in records} == {"implement", "deliver"} assert records[-1]["stage"] == "deliver" - + assert records[-1]["refs"]["invoked"] == "implement" + + (tmp_path / "plan").mkdir() + plan_env = _fixture(tmp_path / "plan") + plan_sub = plan_env["sub"] + (plan_sub / "story.md").write_text("story\n") + (plan_sub / "guide.md").write_text("guide\n") + (plan_sub / "specs").mkdir() + commit_all(plan_sub, "planning inputs") + monkeypatch.chdir(plan_sub) + monkeypatch.setenv("HOME", str(tmp_path / "plan-home")) + plan_agent = _CodingAgent(plan_sub) + plan_host = _Host(plan_env) + plan_result = _workflow_run(plan_env, plan_agent, plan_host, monkeypatch, None, {"revision": plan_env["tip"], "verdict": "consistent"}, lambda **kwargs: {"ok": True}, stage="plan") + assert plan_result["step_results"]["record"]["result"]["reason"] == "stage.complete" def test_stage_failure_unrecorded_stage_and_fix_bound_do_not_advance(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - env = _fixture(tmp_path) - sub = env["sub"] - (sub / "story.md").write_text("story\n") - (sub / "guide.md").write_text("guide\n") - (sub / "specs").mkdir() - (sub / "src").mkdir() - env["tip"] = commit_all(sub, "delivery inputs") - monkeypatch.chdir(sub) - monkeypatch.setenv("PATH", stub_gate_tools(tmp_path)) - monkeypatch.setenv("HOME", str(tmp_path / "home")) + env, sub = _stage_env(tmp_path, monkeypatch) + from beddel.domain.errors import AgentError + from beddel.error_codes import AGENT_EXECUTION_FAILED + class FailingAgent(_CodingAgent): async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: self.calls += 1 if "one plan stage" in prompt: (self.repo / "specs" / "story.md").write_bytes(_spec()) - return AgentResult(1, "failed", [], [], {}, "fake") + return AgentResult(0, "", [], [], {}, "fake") + raise AgentError(code=AGENT_EXECUTION_FAILED, message="agent failed") agent = FailingAgent(sub) host = _Host(env) report = {"revision": env["tip"], "verdict": "consistent"} - result = _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) - assert result["step_results"]["record"]["result"]["reason"] == "stage.failed" + _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) + records = _deliver_records(tmp_path / "home") + assert _tuples(records)[-2:] == [("implement", "failed", "agent.failed"), ("deliver", "failed", "stage.failed")] + first_calls = agent.calls + _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) + rerun_records = _deliver_records(tmp_path / "home") + assert _tuples(rerun_records)[-2:] == [("implement", "failed", "agent.failed"), ("deliver", "failed", "stage.failed")] + assert agent.calls == first_calls + 1 assert host.calls == [] + +def test_fix_bound_stops_before_approval(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env, sub = _stage_env(tmp_path, monkeypatch) + agent = _CodingAgent(sub) + report = {"revision": env["tip"], "verdict": "consistent"} + pending = {"verdict": "has pendency", "items": {"divergences": {"status": "present", "value": [{"finding_index": 0}]}, "pendencies": {"status": "present", "value": [{"finding_index": 0, "reason": "finding_blocking"}]}}} + _workflow_run(env, agent, _Host(env), monkeypatch, None, report, lambda **kwargs: {"ok": True}, fix_report=pending) + records = _deliver_records(tmp_path / "home") + assert _tuples(records)[-2:] == [("verify-and-fix", "stopped", "fix.bound_reached"), ("deliver", "stopped", "stage.stopped")] + assert not any(record["stage"] == "approval" for record in records) + +def test_loader_failure_stops_at_plan_boundary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + env, sub = _stage_env(tmp_path, monkeypatch) + agent = _CodingAgent(sub) + _workflow_run(env, agent, _Host(env), monkeypatch, None, {"revision": env["tip"], "verdict": "consistent"}, lambda **kwargs: {"ok": True}, loader_error=True) + records = _deliver_records(tmp_path / "home") + assert _tuples(records)[-1] == ("deliver", "failed", "stage.unrecorded") + assert records[-1]["refs"]["stage"] == "plan" + assert agent.calls == 0 diff --git a/kits/profile-dev-kit/tests/test_stage_outcome.py b/kits/profile-dev-kit/tests/test_stage_outcome.py index 235b258..3b2acfd 100644 --- a/kits/profile-dev-kit/tests/test_stage_outcome.py +++ b/kits/profile-dev-kit/tests/test_stage_outcome.py @@ -191,4 +191,4 @@ def test_stage_outcomes_term_is_isolated_to_its_tool() -> None: for path in module_root.rglob("*.py") if "stage-outcomes" in path.read_text() } - assert matches == {"stage_outcome.py"} + assert matches == {"stage_outcome.py", "deliver.py"} From 990f7c447135ee637be598f57272d1f74893e2a0 Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Thu, 12 Nov 2026 14:09:56 -0300 Subject: [PATCH 3/3] fix(profile-dev-kit): address review round 2 for deliver workflow (Story 7.7) --- kits/profile-dev-kit/README.md | 28 +++------ kits/profile-dev-kit/kit.yaml | 2 + .../beddel_profile_dev/tools/deliver.py | 7 ++- kits/profile-dev-kit/tests/test_deliver.py | 29 ++++++++- .../tests/test_deliver_workflow.py | 63 +++++++++---------- kits/profile-dev-kit/tests/test_manifest.py | 2 + 6 files changed, 74 insertions(+), 57 deletions(-) diff --git a/kits/profile-dev-kit/README.md b/kits/profile-dev-kit/README.md index 44fd8c0..3769c01 100644 --- a/kits/profile-dev-kit/README.md +++ b/kits/profile-dev-kit/README.md @@ -526,27 +526,13 @@ beddel run -i ... workflows/integrate-story.yaml ## Delivery: deliver workflow -`deliver-story` takes `repo_path`, `story_key`, `stage`, the planning inputs -`story_path`, `guide_path` and `spec_path`, the implementation inputs -`branch`, `base_ref`, `change_id` and `inventory_version`, the Approval -`actor`, and the integration inputs `base_branch`, `remote`, `parent_path`, -`parent_branch` and `status_path`. Set `stage` to `all` to chain plan, -implement, verify-and-fix, Approval and integrate, or select one stage. - -The gate reads the create-only Stage outcomes under the profile records root. -A complete outcome advances the next stage; an incomplete, failed or stopped -outcome ends the run at that boundary. The implement stage has 33 static -nested calls so one unit is reconciled per run without a loop primitive. -Reruns start after the latest complete stage, and a completed Delivery returns -`deliver.complete` without invoking a stage. - -Approval waits for the owner to commit the decision for the latest Report in -HEAD. It passes the unchanged `actor` to `approve-change` exactly once, records -`approval.recorded`, `approval.reused`, `approval.refused` or the owner stop, -and only then allows integrate. The workflow writes one `deliver` outcome with -reasons such as `stage.stopped`, `stage.failed`, `stage.incomplete` and -`deliver.complete`; it never writes an Approval decision or pushes the parent. -Run it from the Change repository root, once per Change: +`deliver-story` chains five stages or runs one. It waits for committed Approval +and records outcomes. + +Input `stage`: `all`, `plan`, `implement`, `verify-and-fix`, `approval` or +`integrate`. `all` resumes after the story's latest complete Stage outcome; +the owner commits the Approval decision, then a rerun runs `approve-change` +once. Each run ends with a `deliver` record. One Change per run. ```text beddel run -i ... workflows/deliver-story.yaml diff --git a/kits/profile-dev-kit/kit.yaml b/kits/profile-dev-kit/kit.yaml index 7e130fe..f738e00 100644 --- a/kits/profile-dev-kit/kit.yaml +++ b/kits/profile-dev-kit/kit.yaml @@ -136,9 +136,11 @@ tools: - name: profile_integrate_settle target: "beddel_profile_dev.tools.integrate:profile_integrate_settle" description: "Settle the parent integration boundary" + category: profile - name: profile_deliver_gate target: "beddel_profile_dev.tools.deliver:profile_deliver_gate" description: "Gate chained Delivery stages from recorded outcomes" + category: profile - name: profile_deliver_approval target: "beddel_profile_dev.tools.deliver:profile_deliver_approval" description: "Check and settle the Delivery Approval stage" diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py index 9cd0329..1d4cbc5 100644 --- a/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/deliver.py @@ -92,7 +92,10 @@ def profile_deliver_gate(repo_path: str, story_key: str, request: str, step: str if step not in STAGES: return g(outcome="failed", reason="gate.failed", refs=_refs(request=request, stage=step, stage_reason="gate.failed")) if prior is None: return f("gate.failed") if not isinstance(prior, Mapping) or any(prior.get(key) != value for key, value in (("stage", "deliver"), ("story_key", story_key), ("request", request), ("repo", str(repo)))): return f("state.mismatch") - if prior.get("outcome") is not None or prior.get("go") != step: return dict(prior) + if prior.get("outcome") is not None or prior.get("go") != step: + if step == "integrate" and prior.get("outcome") is None: + return f("gate.failed") + return dict(prior) mark_value = prior.get("mark") if not isinstance(mark_value, int): return f("state.mismatch") mark = mark_value @@ -123,6 +126,8 @@ def profile_deliver_approval(repo_path: str, story_key: str, change_id: str, bra if phase not in ("check", "settle"): return a("failed", "approval.invalid") if not isinstance(prior, Mapping) or prior.get("story_key") != story_key or prior.get("repo") != str(repo): return a("failed", "state.mismatch") if phase == "check": + if prior.get("outcome") is not None: + return dict(prior) if prior.get("stage") != "deliver" or prior.get("go") != "approval": return a("failed", "state.mismatch") if not is_valid_id(change_id): return a("refused", "change_id.invalid") if branch_reason(branch, None) is not None: return a("refused", "branch.invalid") diff --git a/kits/profile-dev-kit/tests/test_deliver.py b/kits/profile-dev-kit/tests/test_deliver.py index 41f7333..0ffddb1 100644 --- a/kits/profile-dev-kit/tests/test_deliver.py +++ b/kits/profile-dev-kit/tests/test_deliver.py @@ -163,8 +163,12 @@ def test_approval_check_refusals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "approval.invalid" monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_approval", lambda *args: {"approval": None}) monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: (_ for _ in ()).throw(ProfileError("X", "unreadable"))) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "report.unreadable" + monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: {"report": {"revision": "a" * 40}, "history": []}) + assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "report.absent" monkeypatch.setattr("beddel_profile_dev.tools.deliver.profile_read_report", lambda *args: {"report": {"revision": "a" * 40}, "history": [{"ref": "changes/c1/report/3.json"}]}) - monkeypatch.setattr("beddel_profile_dev.tools.deliver.path_present", lambda *args: (_ for _ in ()).throw(ProfileError("X", "git"))) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.path_present", lambda *args: True) + monkeypatch.setattr("beddel_profile_dev.tools.deliver.git_run", lambda *args, **kwargs: type("Git", (), {"returncode": 1, "stdout": b""})()) assert profile_deliver_approval(str(repo), STORY, "c1", "feature/test", "tester", "check", gate)["reason"] == "decision.unreadable" @@ -218,3 +222,26 @@ def test_start_resumes_after_latest_complete_stage(tmp_path: Path, monkeypatch: _record(repo, "implement", "succeeded", "units.complete") second = profile_deliver_gate(str(repo), STORY, "all", "start", {}) assert second["go"] == "verify-and-fix" + + +def test_approval_check_forwards_terminal_prior( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + prior = _gate_for_approval(repo) + prior["outcome"] = "stopped" + prior["reason"] = "approval.awaiting_owner" + result = profile_deliver_approval( + str(repo), STORY, "c1", "feature/test", "tester", "check", prior + ) + assert result == prior + + +def test_integrate_gate_rejects_non_terminal_prior( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path, monkeypatch) + prior = profile_deliver_gate(str(repo), STORY, "all", "start", {}) + prior["go"] = "approval" + result = profile_deliver_gate(str(repo), STORY, "all", "integrate", prior) + assert (result["outcome"], result["reason"]) == ("failed", "gate.failed") diff --git a/kits/profile-dev-kit/tests/test_deliver_workflow.py b/kits/profile-dev-kit/tests/test_deliver_workflow.py index 3de59a7..fb091cc 100644 --- a/kits/profile-dev-kit/tests/test_deliver_workflow.py +++ b/kits/profile-dev-kit/tests/test_deliver_workflow.py @@ -7,7 +7,7 @@ import yaml from beddel.domain.parser import WorkflowParser -from beddel_profile_dev.deliver import IMPLEMENT_SLOTS, STAGES +from beddel_profile_dev.deliver import IMPLEMENT_SLOTS # fmt: off KIT_ROOT = Path(__file__).resolve().parents[1] @@ -21,8 +21,17 @@ def test_deliver_workflow_shape() -> None: assert len([step for step in steps if str(step["id"]).startswith("implement_")]) == IMPLEMENT_SLOTS calls = [step["config"]["workflow"] for step in steps if step.get("primitive") == "call-agent"] assert set(calls) == {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} - assert calls.count("approve-change.yaml") == 1 and all("$" not in call for call in calls) - + assert all(step.get("primitive") != "agent-exec" for step in steps) + expected_ids = ["start", "plan", "after_plan", *[f"implement_{n}" for n in range(1, 34)], "after_implement", "verify", "after_verify", "approval_check", "approve", "approval_settle", "record_approval", "after_approval", "integrate", "after_integrate", "record"] + assert [step["id"] for step in steps] == expected_ids + expected_if = {"plan": "$stepResult.start.result.go == plan", "implement_1": "$stepResult.after_plan.result.go == implement", "verify": "$stepResult.after_implement.result.go == verify-and-fix", "approval_check": "$stepResult.after_verify.result.go == approval", "approve": "$stepResult.approval_check.result.approve == true", "approval_settle": "$stepResult.after_verify.result.go == approval", "record_approval": "$stepResult.after_verify.result.go == approval", "integrate": "$stepResult.after_approval.result.go == integrate"} + for number in range(2, 34): expected_if[f"implement_{number}"] = f"$stepResult.implement_{number - 1}.record.result.outcome == succeeded" + assert {step["id"]: step.get("if") for step in steps if step.get("if")} == expected_if + assert all(step["execution_strategy"]["type"] == ("skip" if step.get("primitive") == "call-agent" else "fail") for step in steps) + for step in steps: + if step.get("primitive") == "call-agent": + target = yaml.safe_load((KIT_ROOT / "workflows" / step["config"]["workflow"]).read_text()) + assert set(step["config"]["inputs"]) == set(target["inputs"]) def test_stage_workflows_state_their_coding_agent() -> None: for name in ("plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml"): @@ -31,13 +40,6 @@ def test_stage_workflows_state_their_coding_agent() -> None: agents = [step for step in document["steps"] if step.get("primitive") == "agent-exec"] assert agents and all(step["config"]["adapter"] == "kiro-cli" for step in agents) assert all(step["config"]["prompt"].startswith("You are the Coding agent") for step in agents) - assert STAGES == ("plan", "implement", "verify-and-fix", "approval", "integrate") - - -def test_stage_call_names_are_static() -> None: - document = yaml.safe_load(WORKFLOW.read_text()) - calls = [step["config"]["workflow"] for step in document["steps"] if step.get("primitive") == "call-agent"] - assert all(call in {"plan-story.yaml", "implement-story.yaml", "verify-and-fix-story.yaml", "approve-change.yaml", "integrate-story.yaml"} for call in calls) import asyncio @@ -148,18 +150,18 @@ def _tuples(records: list[dict[str, Any]]) -> list[tuple[str, str, str]]: return [(record["stage"], record["outcome"], record["reason"]) for record in records] def test_end_to_end_stops_for_owner_then_resumes_to_integration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - env = _fixture(tmp_path) - sub = env["sub"] - (sub / "story.md").write_text("story\n") - (sub / "guide.md").write_text("guide\n") - (sub / "specs").mkdir() - (sub / "src").mkdir() + env, sub = _stage_env(tmp_path, monkeypatch) (env["parent"] / "status.yaml").write_text(f"x: 1\n {STORY}: review # keep\n") - env["tip"] = commit_all(sub, "delivery inputs") report = {"revision": env["tip"], "verdict": "consistent"} approval_box: dict[str, Any] = {"value": None} calls: list[str] = [] def approve_change(**kwargs: Any) -> dict[str, Any]: + expected_commit = git(sub, "log", "-1", "--format=%H", "--diff-filter=A", "HEAD", "--", ".beddel-profile/approvals/c1/1.yaml") + assert kwargs["revision"] == report["revision"] + assert kwargs["decision_commit"] == expected_commit + assert kwargs["change_id"] == "c1" + assert kwargs["repo_path"] == str(sub) + assert kwargs["actor"] in {"wrong", "owner"} calls.append(kwargs["actor"]) if kwargs["actor"] == "owner": approval_box["value"] = {"revision": report["revision"], "proof": {"commit": kwargs["decision_commit"]}} @@ -183,6 +185,7 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: ("deliver", "stopped", "stage.stopped"), ] calls_after_phase_one = agent.calls + assert calls_after_phase_one == 2 report["revision"] = git(sub, "rev-parse", "HEAD") assert calls == [] decision = sub / ".beddel-profile" / "approvals" / "c1" / "1.yaml" @@ -193,12 +196,12 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: env["tip"] = git(sub, "rev-parse", "HEAD") _workflow_run(env, agent, host, monkeypatch, approval_box["value"], report, approve_change, actor="wrong") wrong_records = _deliver_records(tmp_path / "home") - assert _tuples(wrong_records)[-2:] == [("approval", "refused", "approval.refused"), ("deliver", "refused", "stage.refused")] + assert _tuples(wrong_records)[len(phase_one):] == [("approval", "refused", "approval.refused"), ("deliver", "refused", "stage.refused")] assert calls == ["wrong"] approval_box["value"] = None _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) right_records = _deliver_records(tmp_path / "home") - assert _tuples(right_records)[-4:] == [ + assert _tuples(right_records)[len(wrong_records):] == [ ("approval", "succeeded", "approval.recorded"), ("integrate", "succeeded", "merge.confirmed"), ("integrate", "stopped", "pointer.unpublished"), @@ -209,7 +212,7 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: git(env["parent"], "push", "-q", "origin", "main") _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) resumed_records = _deliver_records(tmp_path / "home") - assert _tuples(resumed_records)[-3:] == [ + assert _tuples(resumed_records)[len(right_records):] == [ ("integrate", "succeeded", "merge.reused"), ("integrate", "succeeded", "integrate.complete"), ("deliver", "succeeded", "deliver.complete"), @@ -218,9 +221,8 @@ def approve_change(**kwargs: Any) -> dict[str, Any]: assert host.calls.count("merge") == 1 _workflow_run(env, agent, host, monkeypatch, approval_box, report, approve_change) complete_records = _deliver_records(tmp_path / "home") - assert _tuples(complete_records)[-1:] == [("deliver", "succeeded", "deliver.complete")] + assert _tuples(complete_records)[len(resumed_records):] == [("deliver", "succeeded", "deliver.complete")] assert agent.calls == calls_after_phase_one - assert calls == ["wrong", "owner"] def _stage_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[dict[str, Any], Path]: env = _fixture(tmp_path) @@ -242,25 +244,16 @@ def test_single_stage_runs_only_that_stage(tmp_path: Path, monkeypatch: pytest.M report = {"revision": env["tip"], "verdict": "consistent"} (sub / "specs" / "story.md").write_bytes(_spec()) result = _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}, stage="implement") - assert result["step_results"]["record"]["result"]["stage"] == "deliver" assert result["step_results"]["record"]["result"]["outcome"] == "succeeded" assert result["step_results"]["record"]["result"]["reason"] == "stage.complete" records = _deliver_records(tmp_path / "home") - assert records[-1]["stage"] == "deliver" assert records[-1]["refs"]["invoked"] == "implement" (tmp_path / "plan").mkdir() - plan_env = _fixture(tmp_path / "plan") - plan_sub = plan_env["sub"] - (plan_sub / "story.md").write_text("story\n") - (plan_sub / "guide.md").write_text("guide\n") - (plan_sub / "specs").mkdir() - commit_all(plan_sub, "planning inputs") + plan_env, plan_sub = _stage_env(tmp_path / "plan", monkeypatch) monkeypatch.chdir(plan_sub) - monkeypatch.setenv("HOME", str(tmp_path / "plan-home")) plan_agent = _CodingAgent(plan_sub) - plan_host = _Host(plan_env) - plan_result = _workflow_run(plan_env, plan_agent, plan_host, monkeypatch, None, {"revision": plan_env["tip"], "verdict": "consistent"}, lambda **kwargs: {"ok": True}, stage="plan") + plan_result = _workflow_run(plan_env, plan_agent, _Host(plan_env), monkeypatch, None, {"revision": plan_env["tip"], "verdict": "consistent"}, lambda **kwargs: {"ok": True}, stage="plan") assert plan_result["step_results"]["record"]["result"]["reason"] == "stage.complete" def test_stage_failure_unrecorded_stage_and_fix_bound_do_not_advance(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: @@ -281,10 +274,12 @@ async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) records = _deliver_records(tmp_path / "home") assert _tuples(records)[-2:] == [("implement", "failed", "agent.failed"), ("deliver", "failed", "stage.failed")] + first_record_count = len(records) first_calls = agent.calls _workflow_run(env, agent, host, monkeypatch, None, report, lambda **kwargs: {"ok": True}) rerun_records = _deliver_records(tmp_path / "home") - assert _tuples(rerun_records)[-2:] == [("implement", "failed", "agent.failed"), ("deliver", "failed", "stage.failed")] + assert _tuples(rerun_records)[first_record_count:] == [("implement", "failed", "agent.failed"), ("deliver", "failed", "stage.failed")] + assert "plan" not in [record["stage"] for record in rerun_records[first_record_count:]] assert agent.calls == first_calls + 1 assert host.calls == [] diff --git a/kits/profile-dev-kit/tests/test_manifest.py b/kits/profile-dev-kit/tests/test_manifest.py index 58e693f..ba03678 100644 --- a/kits/profile-dev-kit/tests/test_manifest.py +++ b/kits/profile-dev-kit/tests/test_manifest.py @@ -121,6 +121,8 @@ def test_manifest_declares_all_workflows_and_python_targets() -> None: "integrate-story", "deliver-story", ] + for tool in manifest["tools"]: + assert tool["category"] == "profile" declared = {t["name"] for t in manifest["tools"]} targeted = {t["name"] for t in manifest["targets"]["python"]["tools"]} assert manifest["targets"]["python"]["dependencies"] == []