A DB stage=done task with 0 active jobs flapped in Plane between `Awaiting Deploy` and `Monitoring after Deploy` instead of holding `Done` (verified live on ORCH-061, task 47): the three deploy-phase setters were terminal-blind, so any stale/duplicate/unknown caller under the bot token re-stamped an intermediate status over the terminal Done, forever. - New leaf src/deploy_status_guard.py (pure, never-raise, config-gated): decide() -> ALLOW | CONVERGE_DONE | SUPPRESS on the entry of set_issue_awaiting_deploy / set_issue_deploying / set_issue_monitoring. A deploy-phase status is legitimate iff the task is non-terminal OR (done AND post-deploy window active); otherwise done converges to Done idempotently, cancelled is suppressed (FR-2, D1/D2). - D3: move post_deploy.arm_monitor ABOVE the terminal-sync block in advance_stage so window_active is True when the legitimate first Monitoring is set (the task is already DB-done by then); a re-drive after the window closes converges to Done. - D4: run_post_deploy_monitor no-ops without a status PATCH / re-queue when the task became cancelled mid-window (zombie-tick guard, FR-3). - D5: additive `reason` kwarg on the three setters + one structured log line per verdict (work_item/caller/target/db_stage/window_active/verdict); new read-only db.get_task_by_work_item_id; post_deploy.window_active helper. - Flags deploy_status_guard_enabled (kill-switch -> 1:1) / deploy_status_guard_repos (CSV; empty = self-hosting only). STAGE_TRANSITIONS / QG_CHECKS / check_* / machine-verdict keys / DB schema untouched (reads existing tasks.stage). Tests: TC-01..TC-12 across 5 new test modules + config flags; updated the reason-kwarg assertions in test_deploy_terminal_sync / test_deploy_approve. Full regress green (1413). Docs: CHANGELOG, CLAUDE.md, docs/architecture/README.md (status -> реализовано), .env.example. Refs: ORCH-094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
175 lines
7.2 KiB
Python
175 lines
7.2 KiB
Python
"""ORCH-036 TC-04/05/06: the manual-approve gate for the executable self-deploy.
|
|
|
|
Contract (AC-5, AC-12):
|
|
* TC-04 — ``deploy_require_manual_approve`` defaults to True in settings.
|
|
* TC-05 — flag true + NO human approve -> the prod hook is NEVER called; the
|
|
deploy-staging -> deploy edge only advances the STAGE and requests an approve
|
|
(Phase A). ``initiate_deploy`` / ssh subprocess must not be touched.
|
|
* TC-06 — flag true + a human Approved -> the prod hook is launched EXACTLY once
|
|
(Phase B), idempotent on a repeated Approved (the ``initiated`` marker guards).
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
_test_db = os.path.join(tempfile.gettempdir(), "test_orch_deploy_approve.db")
|
|
os.environ["ORCH_DB_PATH"] = _test_db
|
|
os.environ["ORCH_REPOS_DIR"] = tempfile.gettempdir()
|
|
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
|
|
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
|
|
|
|
from unittest.mock import MagicMock # noqa: E402
|
|
|
|
import src.db as _db # noqa: E402
|
|
from src.db import init_db, get_db # noqa: E402
|
|
from src import stage_engine # noqa: E402
|
|
from src import self_deploy # noqa: E402
|
|
from src.stage_engine import advance_stage # noqa: E402
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fresh_db(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(_db.settings, "db_path", _test_db)
|
|
if os.path.exists(_test_db):
|
|
os.unlink(_test_db)
|
|
init_db()
|
|
# Isolate the sentinel state dirs to a per-test tmp dir.
|
|
monkeypatch.setattr(self_deploy.settings, "repos_dir", str(tmp_path))
|
|
monkeypatch.setattr(self_deploy.settings, "host_repos_dir", str(tmp_path))
|
|
yield
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def silence_side_effects(monkeypatch):
|
|
for name in (
|
|
"notify_stage_change", "notify_qg_failure", "notify_approve_requested",
|
|
"send_telegram", "plane_notify_stage", "plane_notify_qg", "plane_add_comment",
|
|
"set_issue_in_review", "set_issue_needs_input", "set_issue_in_progress",
|
|
"set_issue_blocked", "set_issue_done",
|
|
# ORCH-066 status setters.
|
|
"set_issue_analysis", "set_issue_awaiting_deploy", "set_issue_deploying",
|
|
"set_issue_monitoring",
|
|
):
|
|
monkeypatch.setattr(stage_engine, name, MagicMock())
|
|
|
|
|
|
def _make_task(stage, repo="orchestrator", branch="feature/ORCH-036-x", wi="ORCH-036"):
|
|
conn = get_db()
|
|
cur = conn.execute(
|
|
"INSERT INTO tasks (plane_id, work_item_id, repo, branch, stage) "
|
|
"VALUES (?, ?, ?, ?, ?)",
|
|
(f"plane-{wi}", wi, repo, branch, stage),
|
|
)
|
|
task_id = cur.lastrowid
|
|
conn.commit()
|
|
conn.close()
|
|
return task_id
|
|
|
|
|
|
def _stage(task_id):
|
|
conn = get_db()
|
|
row = conn.execute("SELECT stage FROM tasks WHERE id=?", (task_id,)).fetchone()
|
|
conn.close()
|
|
return row[0]
|
|
|
|
|
|
def _jobs():
|
|
conn = get_db()
|
|
rows = conn.execute("SELECT agent, repo, task_id FROM jobs ORDER BY id").fetchall()
|
|
conn.close()
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
def _pass(*a, **k):
|
|
return (True, "ok")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TC-04: default flag value
|
|
# ---------------------------------------------------------------------------
|
|
def test_tc04_manual_approve_default_true():
|
|
"""The fresh, un-overridden settings default must be True (safe-by-default)."""
|
|
from src.config import Settings
|
|
assert Settings().deploy_require_manual_approve is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TC-05: flag true, no approve -> prod hook NOT called (Phase A only)
|
|
# ---------------------------------------------------------------------------
|
|
def test_tc05_no_approve_does_not_call_prod_hook(monkeypatch):
|
|
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
|
|
monkeypatch.setattr(
|
|
stage_engine, "QG_CHECKS",
|
|
{**stage_engine.QG_CHECKS,
|
|
"check_staging_status": _pass,
|
|
"check_security_gate": _pass,
|
|
"check_branch_mergeable": _pass,
|
|
"check_staging_image_fresh": _pass},
|
|
)
|
|
# Spy: the deploy launcher must never run on the staging->deploy edge.
|
|
initiate = MagicMock()
|
|
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
|
|
ssh_run = MagicMock()
|
|
monkeypatch.setattr(self_deploy.subprocess, "run", ssh_run)
|
|
|
|
task_id = _make_task("deploy-staging")
|
|
res = advance_stage(
|
|
task_id, "deploy-staging", "orchestrator", "ORCH-036",
|
|
"feature/ORCH-036-x", finished_agent="deployer",
|
|
)
|
|
|
|
# Phase A: advanced the STAGE to deploy, but requested approve — no prod hook.
|
|
assert res.advanced is True
|
|
assert res.to_stage == "deploy"
|
|
assert _stage(task_id) == "deploy"
|
|
assert res.note == "self-deploy-approval-pending"
|
|
initiate.assert_not_called()
|
|
ssh_run.assert_not_called()
|
|
# No deployer job: the human Approved (Phase B) is what triggers the deploy.
|
|
assert _jobs() == []
|
|
# The restart-safe approve-requested marker was written.
|
|
assert self_deploy.has_marker("orchestrator", "ORCH-036", self_deploy.APPROVE_REQUESTED)
|
|
# ORCH-066 AC-6/AC-13: Phase A indicates `Awaiting Deploy`, NOT `In Review`.
|
|
# ORCH-094: the caller now tags the reason (FR-4 observability).
|
|
stage_engine.set_issue_awaiting_deploy.assert_called_once_with("ORCH-036", reason="phase_a")
|
|
stage_engine.set_issue_in_review.assert_not_called()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TC-06: flag true + Approved -> prod hook called exactly once (idempotent)
|
|
# ---------------------------------------------------------------------------
|
|
def test_tc06_approved_calls_prod_hook_exactly_once(monkeypatch):
|
|
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
|
|
monkeypatch.setattr(stage_engine.settings, "deploy_ssh_host", "mva154")
|
|
# Real initiate_deploy, but the ssh subprocess is mocked (rc=0 -> dispatched).
|
|
ssh_run = MagicMock(return_value=MagicMock(returncode=0, stdout="", stderr=""))
|
|
monkeypatch.setattr(self_deploy.subprocess, "run", ssh_run)
|
|
|
|
task_id = _make_task("deploy") # already on deploy, awaiting Confirm Deploy
|
|
|
|
# ORCH-059: Phase B is now triggered by the dedicated "Confirm Deploy" status
|
|
# (confirm_deploy=True), NOT by a plain Approved. 1st Confirm Deploy ->
|
|
# Phase B initiates the detached deploy.
|
|
res1 = advance_stage(
|
|
task_id, "deploy", "orchestrator", "ORCH-036",
|
|
"feature/ORCH-036-x", finished_agent=None, confirm_deploy=True,
|
|
)
|
|
assert res1.note == "self-deploy-initiated"
|
|
assert ssh_run.call_count == 1
|
|
# The finalizer was enqueued.
|
|
assert any(j["agent"] == "deploy-finalizer" for j in _jobs())
|
|
assert self_deploy.has_marker("orchestrator", "ORCH-036", self_deploy.INITIATED)
|
|
# ORCH-066 AC-7: Phase B indicates `Deploying` on a successful initiate.
|
|
# ORCH-094: the caller now tags the reason (FR-4 observability).
|
|
stage_engine.set_issue_deploying.assert_called_once_with("ORCH-036", reason="phase_b")
|
|
|
|
# 2nd (duplicate) Confirm Deploy -> idempotent no-op, hook NOT called again.
|
|
res2 = advance_stage(
|
|
task_id, "deploy", "orchestrator", "ORCH-036",
|
|
"feature/ORCH-036-x", finished_agent=None, confirm_deploy=True,
|
|
)
|
|
assert res2.note == "self-deploy-already-initiated"
|
|
assert ssh_run.call_count == 1 # still exactly one prod deploy
|