Compare commits

..

34 Commits

Author SHA1 Message Date
stream
c90c01b919 fix(tests): integ fixtures — to_analyse always present (066), full status keys, security_gate registered (022)
All checks were successful
CI / test (pull_request) Successful in 20s
2026-06-08 06:41:52 +00:00
stream
2ec6873e33 integ: merge ORCH-068 reconciler livelock fix
# Conflicts:
#	docs/architecture/README.md
#	src/reconciler.py
2026-06-08 06:36:29 +00:00
stream
cac6539698 integ: merge ORCH-066 plane status model
# Conflicts:
#	CHANGELOG.md
#	docs/architecture/README.md
#	src/plane_sync.py
#	src/webhooks/plane.py
2026-06-08 06:34:37 +00:00
stream
af7472df05 integ: merge ORCH-059 confirm-deploy
# Conflicts:
#	CHANGELOG.md
#	docs/architecture/README.md
2026-06-08 06:32:53 +00:00
stream
995ba0af71 integ: merge ORCH-022 security/secret-scanning 2026-06-08 06:32:13 +00:00
772ccab013 docs(history): CRITICAL postmortem — phantom merge (deploy without main-merge), see ORCH-071 2026-06-08 09:20:22 +03:00
post-deploy-monitor
06271b0bfb docs(ORCH-021): post-deploy HEALTHY/NONE for ORCH-068
All checks were successful
CI / test (push) Successful in 17s
CI / test (pull_request) Successful in 18s
2026-06-08 05:49:27 +00:00
101bd1c512 docs(history): lesson — Confirm Deploy dead trigger (ORCH-066 regression, see ORCH-070) 2026-06-08 08:38:30 +03:00
deploy-finalizer
aa4161fc78 deploy(ORCH-036): finalize SUCCESS for ORCH-068
All checks were successful
CI / test (push) Successful in 19s
CI / test (pull_request) Successful in 20s
2026-06-08 05:34:23 +00:00
6bbd530caa tester(ET): auto-commit from tester run_id=351
All checks were successful
CI / test (push) Successful in 19s
CI / test (pull_request) Successful in 21s
2026-06-08 05:18:46 +00:00
4b03f213f7 reviewer(ET): auto-commit from reviewer run_id=349 2026-06-08 05:18:46 +00:00
1d72c44587 fix(reconciler): stop F-2 livelock spam on synced terminal tasks + cache TTL
Reconciler F-2 spammed Telegram "<wi> разблокирована" every ~120s for a
fully-synchronized Done task (incident ET-002, 191+ msgs/night) after the
ORCH-066 Plane status model merge. Two stacked defects (defense in depth):

- D1 (selection): actionable states were told apart by bare UUID, so a Done
  issue aliased onto the approved UUID entered the approved branch. Now
  terminal states are excluded by Plane state GROUP (completed/cancelled),
  a project-independent discriminator robust to UUID aliasing; per-issue
  check with a logical-key fallback when the group is unavailable.
  get_project_states caches {uuid -> group} from the same /states/ fetch;
  new sibling accessor get_project_state_groups.
- D2 (notification): _note_unblock fired unconditionally after _dispatch.
  Now it only fires on a confirmed state change (stage before/after _dispatch;
  task-appears for the start case) — handlers' contracts untouched.
- TR-3: in-memory dedup guard {issue_id -> last unblocked state} as a backstop.
- TR-4: _STATES_CACHE lived for the whole process lifetime, so a new Plane
  status was invisible without a restart. Added TTL ORCH_PLANE_STATES_TTL_S
  (default 300s; 0 = previous lifetime cache) reusing reload_project_states();
  a failed refresh serves the stale-but-correct set, not enduro defaults.

STAGE_TRANSITIONS / QG_CHECKS / DB schema / handle_* contracts / F-1 / F-3
unchanged; never-raise preserved; self-hosting tick never restarts prod.
Observability: skipped_terminal_total / deduped_total in /queue reconcile block.

Tests: tests/test_reconciler_plane.py (TC-01..TC-10),
tests/test_plane_states_cache.py (TC-11/TC-12).

Refs: ORCH-068

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-08 05:18:46 +00:00
0605309602 architect(ET): auto-commit from architect run_id=347 2026-06-08 05:18:46 +00:00
044894cbe9 analyst(ET): auto-commit from analyst run_id=346 2026-06-08 05:18:46 +00:00
cb11137a77 docs: init ORCH-068 business request 2026-06-08 05:18:46 +00:00
48b54051e5 docs(ORCH-068): add staging gate log (staging_status: SUCCESS) 2026-06-08 05:18:24 +00:00
post-deploy-monitor
9f4d79baee docs(ORCH-021): post-deploy HEALTHY/NONE for ORCH-059
All checks were successful
CI / test (push) Successful in 18s
CI / test (pull_request) Successful in 19s
2026-06-07 19:44:39 +00:00
deploy-finalizer
7cdef6d377 deploy(ORCH-036): finalize SUCCESS for ORCH-059
All checks were successful
CI / test (push) Successful in 18s
CI / test (pull_request) Successful in 18s
2026-06-07 19:29:34 +00:00
post-deploy-monitor
0cbb7ef0bb docs(ORCH-021): post-deploy HEALTHY/NONE for ORCH-022
All checks were successful
CI / test (push) Successful in 18s
CI / test (pull_request) Successful in 18s
2026-06-07 19:24:29 +00:00
ca41d9210b tester(ET): auto-commit from tester run_id=337
All checks were successful
CI / test (push) Successful in 20s
CI / test (pull_request) Successful in 17s
2026-06-07 19:20:41 +00:00
48943fe10a reviewer(ET): auto-commit from reviewer run_id=336 2026-06-07 19:20:41 +00:00
86fe8dd509 feat(deploy): dedicated "Confirm Deploy" status triggers prod deploy
Split the overloaded `Approved` Plane status: it served BOTH as the human BRD
gate on `analysis` AND as the silent Phase B prod-deploy trigger on `deploy`
(ORCH-036), so a routine approve could launch a self-hosting prod restart.

ORCH-059 introduces a dedicated logical status `confirm_deploy` ("Confirm
Deploy") that triggers ONLY Phase B on `deploy`; `Approved` stays purely a
pipeline gate.

- plane_sync: map "Confirm Deploy" -> "confirm_deploy" in _PLANE_NAME_TO_KEY;
  intentionally absent from _DEFAULT_STATES => fail-closed (no UUID -> .get
  yields None, no KeyError, no blind deploy).
- webhooks/plane: handle_issue_updated routes "Confirm Deploy" (fail-closed
  .get) to new handle_confirm_deploy (guarded to stage=="deploy") ->
  _try_advance_stage(confirm_deploy=True).
- stage_engine: advance_stage gains keyword-only confirm_deploy=False; Phase B
  block returns early for deploy+finished_agent is None but only initiates the
  deploy when confirm_deploy=True; a plain Approved is a deterministic no-op
  (returns before check_deploy_status -> no false БАГ-8 rollback).
- Phase A CTA now asks the operator for "Confirm Deploy", not "Approved".

Contracts unchanged: STAGE_TRANSITIONS, QG_CHECKS, check_deploy_status, hook
exit codes, Phases A/C, merge-gate, DB schema. Conditional like ORCH-35/36
(self-hosting only). Docs updated (CLAUDE.md, architecture/README.md, CHANGELOG).

Refs: ORCH-059

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 19:20:41 +00:00
dd07b58165 architect(ET): auto-commit from architect run_id=334 2026-06-07 19:20:41 +00:00
b67a61ecef analyst(ET): auto-commit from analyst run_id=333 2026-06-07 19:20:41 +00:00
8fcb867dcf docs: init ORCH-059 business request 2026-06-07 19:20:41 +00:00
deploy-finalizer
e07ee9e574 deploy(ORCH-036): finalize SUCCESS for ORCH-022
All checks were successful
CI / test (push) Successful in 17s
CI / test (pull_request) Successful in 17s
2026-06-07 18:42:29 +00:00
8cdb9f194a tester(ET): auto-commit from tester run_id=331
All checks were successful
CI / test (push) Successful in 19s
CI / test (pull_request) Successful in 19s
2026-06-07 18:04:50 +00:00
cb3bdd9c7a reviewer(ET): auto-commit from reviewer run_id=330 2026-06-07 18:04:50 +00:00
Dev
04233cb3c8 test(ORCH-022): isolate TC-17 worktree under tmp_path (fix CI PermissionError on /repos/_wt)
TC-17 seeded 17-security-report.md via get_worktree_path() which resolves to
settings.worktrees_dir (default /repos/_wt) -> the test wrote into the real shared
host worktree path. In CI that dir is owned by another user -> PermissionError.

Monkeypatch git_worktree.settings.worktrees_dir to tmp_path/_wt (same pattern as
test_git_worktree.py / test_merge_gate.py). Prod logic untouched.
2026-06-07 18:04:50 +00:00
stream
85ecf50926 ci: re-run after gitea restart (ORCH-022 flaky CI) 2026-06-07 18:04:50 +00:00
30b6187c73 feat(security): security-gate (gitleaks secret-scan + pip-audit) before merge
Add a deterministic (no-LLM) security sub-gate on the deploy-staging -> deploy
edge, run FIRST (before merge-gate ORCH-043 and image-freshness ORCH-058) so it
fails cheaply before any expensive rebase/rebuild, and scans origin/main..HEAD
before rebase so a task is never blamed for a CVE introduced by an updated main.

Why: the autonomous pipeline merged branches into main with no check for a leaked
secret or a vulnerable dependency. For the self-hosting orchestrator (one shared
prod instance serving every project from a shared DB) a single leak/CVE landed in
the prod of all projects (CLAUDE.md self-hosting, section 8).

- New leaf src/security_gate.py (never-raise): gitleaks (offline, fail-closed on
  tool error => secrets guarantee is unconditional) + pip-audit (best-effort;
  unreachable CVE feed degrades fail-open + loud warning by default, strict via
  security_dep_audit_fail_closed). Verdict lives ONLY in 17-security-report.md
  YAML frontmatter (write -> read-back single source of truth); FAIL is
  authoritative; missing/broken frontmatter => fail-closed.
- check_security_gate thin wrapper registered in QG_CHECKS (lazy import, no cycle).
- _handle_security_gate wired FIRST in advance_stage deploy-staging block: FAIL ->
  rollback to development + developer-retry (cap MAX_DEVELOPER_RETRIES); task_desc
  carries verbatim findings (ORCH-046 pattern). No merge-lease release (runs before
  lease acquire). Self-hosting safe: only reads/scans/writes, never deploys.
- Conditional rollout (security_gate_enabled + security_gate_repos; empty scope ->
  self-hosting only). 6 new ORCH_SECURITY_* settings.
- Infra: pinned gitleaks Go binary in Dockerfile (+curl/ca-certificates), pip-audit
  in requirements.txt, versioned .gitleaks.toml at repo root.
- STAGE_TRANSITIONS and DB schema unchanged.

Docs: docs/architecture/README.md (marked realized), CLAUDE.md (artifact 17),
CHANGELOG.md. Tests: test_security_gate.py, test_qg_security.py,
test_stage_engine_security_gate.py + updated registry/edge snapshots.

Refs: ORCH-022

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 18:04:50 +00:00
44db94e462 architect(ET): auto-commit from architect run_id=327 2026-06-07 18:04:50 +00:00
4f24f96169 analyst(ET): auto-commit from analyst run_id=326 2026-06-07 18:04:50 +00:00
2d20da295e docs: init ORCH-022 business request 2026-06-07 18:04:50 +00:00
70 changed files with 6006 additions and 54 deletions

View File

@@ -117,6 +117,15 @@ ORCH_RECONCILE_GRACE_OVERRIDES_JSON=
ORCH_RECONCILE_NOTIFY_UNBLOCK=true
ORCH_RECONCILE_SKIP_BLOCKED_ENABLED=true
# ORCH-068: TTL (seconds) for the per-project Plane states cache (plane_sync
# _STATES_CACHE). Historically the cache lived for the whole process lifetime,
# so a status added to Plane after start was invisible until a restart
# ("stale set -> no pipeline action"). With a TTL the entry self-heals by
# re-fetching /states/ once it expires (reuses reload_project_states()).
# >0 -> re-fetch after this many seconds (default 300 = 5 min);
# 0 -> disable TTL -> strictly the previous lifetime cache (back-compat).
ORCH_PLANE_STATES_TTL_S=300
# ORCH-065: job-reaper + proactive merge-lease reclaim. A background daemon thread
# (src/job_reaper.py, started LAST in main.lifespan after requeue_running_jobs) reaps
# zombie 'running' jobs whose monitor/process died before writing the terminal status
@@ -146,6 +155,27 @@ ORCH_REAPER_MAX_RUNNING_S=3600
ORCH_REAPER_FINALIZE_GRACE_S=300
ORCH_LEASE_RECLAIM_ENABLED=true
# ORCH-022: security-gate (secret-scanning + dependency audit) on the
# deploy-staging -> deploy edge, run FIRST among the edge sub-gates. Deterministic
# (no LLM): gitleaks (offline secret-scan, pinned Go binary in the image) + pip-audit
# (OSV/PyPI CVE audit). Verdict in the versioned 17-security-report.md frontmatter;
# FAIL -> rollback to development + developer-retry (cap 3). See ADR-001.
# GATE_ENABLED -> global kill-switch; false -> pipeline 1:1 as before ORCH-022.
# GATE_REPOS -> CSV of repos where the gate is REAL; empty -> only self-hosting.
# DEP_BLOCK_SEVERITY -> CVE severity that BLOCKS (CRITICAL>HIGH>MEDIUM>LOW); below /
# UNKNOWN -> warning only (anti-loop).
# SCAN_TIMEOUT_S -> per external scanner call timeout.
# DEP_AUDIT_FAIL_CLOSED -> strict mode: unreachable CVE feed -> FAIL instead of the
# default fail-open + warning (anti-loop). Default false.
# SECRETS_BLOCK -> a found secret blocks (always true by default; the offline
# secrets guarantee is unconditional).
ORCH_SECURITY_GATE_ENABLED=true
ORCH_SECURITY_GATE_REPOS=
ORCH_SECURITY_DEP_BLOCK_SEVERITY=HIGH
ORCH_SECURITY_SCAN_TIMEOUT_S=300
ORCH_SECURITY_DEP_AUDIT_FAIL_CLOSED=false
ORCH_SECURITY_SECRETS_BLOCK=true
# ORCH-021: post-deploy production monitoring + degradation reaction. After the
# terminal deploy->done transition for an applicable repo, a reserved-agent job
# `post-deploy-monitor` (no LLM, modelled on deploy-finalizer) probes prod over a

38
.gitleaks.toml Normal file
View File

@@ -0,0 +1,38 @@
# gitleaks config — ORCH-022 security-gate (secret-scanning).
#
# Versioned in the repo root (07-infra I-4 / BR-13): rules + an allowlist of
# known-safe matches are reviewed as code. The security-gate (src/security_gate.py)
# passes this file via `--config` when present. gitleaks runs OFFLINE (local rules)
# so the "a secret always blocks" guarantee (BR-2) never depends on the network.
#
# Strategy: extend the built-in ruleset (broad coverage, maintained upstream) and
# only ADD a narrow allowlist for placeholders / fixtures that are intentionally
# fake (e.g. .env.example dummy values, test fixtures). Keep the allowlist tight —
# an over-broad allowlist silently re-opens the leak it was meant to bless.
title = "orchestrator gitleaks config"
[extend]
# Start from gitleaks' maintained default ruleset.
useDefault = true
[allowlist]
description = "Known-safe, intentionally non-secret matches (placeholders + fixtures)."
# Files that legitimately contain placeholder/dummy secret-shaped values:
# * .env.example — the committed canon of env vars with DUMMY values (CLAUDE.md §8;
# real secrets live only in the host .env / .env.staging, never in git).
# * tests/ — fixtures may embed fake tokens to exercise the scanner itself (TC-03).
# * .gitleaks.toml — this file (avoid self-matching example patterns below).
paths = [
'''(^|/)\.env\.example$''',
'''(^|/)tests/''',
'''(^|/)\.gitleaks\.toml$''',
]
# Generic placeholder tokens used in docs / examples that are NOT real secrets.
regexes = [
'''(?i)(your[-_]?(token|key|secret|password)[-_]?here)''',
'''(?i)(changeme|dummy|example|placeholder|xxxxx+)''',
'''(?i)<[a-z0-9_-]+>''',
]

File diff suppressed because one or more lines are too long

View File

@@ -47,10 +47,10 @@ created → analysis → architecture → development → review → testing →
- ADR per work-item: `docs/work-items/<plane-id>/06-adr/ADR-NNN-slug.md`
- Global ADR (сквозные решения): `docs/architecture/adr/adr-NNNN-slug.md`
- Work items: `docs/work-items/<plane-id>/`
- Машинные вердикты Quality Gate — строго YAML-frontmatter (`verdict:`, `deploy_status:`, `staging_status:`), никогда проза
- Машинные вердикты Quality Gate — строго YAML-frontmatter (`verdict:`, `deploy_status:`, `staging_status:`, `security_status:`), никогда проза
## Артефакты задачи (`docs/work-items/<plane-id>/`)
`00-business-request.md`, `01-brd.md`, `02-trz.md`, `03-acceptance-criteria.md`, `04-test-plan.yaml`, `06-adr/ADR-NNN-slug.md`, `07-infra-requirements.md`, `08-data-requirements.md`, `10-tech-risks.md`, `12-review.md`, `13-test-report.md`, `14-deploy-log.md`, `15-staging-log.md`, `16-post-deploy-log.md` (post-deploy наблюдение, ORCH-021).
`00-business-request.md`, `01-brd.md`, `02-trz.md`, `03-acceptance-criteria.md`, `04-test-plan.yaml`, `06-adr/ADR-NNN-slug.md`, `07-infra-requirements.md`, `08-data-requirements.md`, `10-tech-risks.md`, `12-review.md`, `13-test-report.md`, `14-deploy-log.md`, `15-staging-log.md`, `16-post-deploy-log.md` (post-deploy наблюдение, ORCH-021), `17-security-report.md` (security-гейт: `security_status:`/secrets/deps, ORCH-022).
## Правила для агентов
1. Перед любым действием прочесть этот файл и `docs/architecture/README.md`.
@@ -67,6 +67,10 @@ created → analysis → architecture → development → review → testing →
- **НЕ перезапускать / не ронять прод-контейнер** `orchestrator` в рамках задачи — встанет конвейер всех проектов.
- Любой деплой/рестарт self = групповой риск. Детали и топология — `docs/operations/INFRA.md`.
- Стадия `deploy-staging` (порт 8501) — обязательная страховка перед прод-деплоем орка.
- Прод-деплой орка запускается ТОЛЬКО переводом задачи на стадии `deploy` в выделенный
Plane-статус **«Confirm Deploy»** (ORCH-059). Статус `Approved` — человеческий гейт
конвейера и прод-деплой НЕ запускает (на `deploy` — no-op). Это разделяет «одобрить
артефакт» и «выкатить в прод», чтобы привычный approve не ронял прод случайным кликом.
---
*Паспорт проекта orchestrator. Поддерживается агентами при каждой доработке. Изолирован: описывает только этот проект (канон per-repo, см. ORCH-9).*

View File

@@ -8,9 +8,28 @@ FROM python:3.12-slim
ARG GIT_SHA=""
LABEL org.opencontainers.image.revision=$GIT_SHA
WORKDIR /app
RUN apt-get update -qq && apt-get install -y -qq openssh-client git && rm -rf /var/lib/apt/lists/*
RUN apt-get update -qq && apt-get install -y -qq openssh-client git curl ca-certificates && rm -rf /var/lib/apt/lists/*
# git operations run as root over bind-mounted /repos (may be owned by host uid) -> trust it.
RUN git config --system --add safe.directory '*'
# ORCH-022: pinned gitleaks static Go binary for the offline secret-scan sub-gate
# (07-infra I-1). Baked into the image (NOT a pip package): the gate runs INSIDE the
# orchestrator container over a per-task worktree. Pinned release => deterministic
# rules; gitleaks needs no network so the "a secret always blocks" guarantee (BR-2)
# is independent of internet access. Multi-arch aware (amd64/arm64).
ARG GITLEAKS_VERSION=8.18.4
RUN set -eux; \
arch="$(dpkg --print-architecture)"; \
case "$arch" in \
amd64) gl_arch="x64" ;; \
arm64) gl_arch="arm64" ;; \
*) echo "unsupported arch: $arch" >&2; exit 1 ;; \
esac; \
curl -fsSL -o /tmp/gitleaks.tar.gz \
"https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_${gl_arch}.tar.gz"; \
tar -xzf /tmp/gitleaks.tar.gz -C /usr/local/bin gitleaks; \
chmod +x /usr/local/bin/gitleaks; \
rm -f /tmp/gitleaks.tar.gz; \
gitleaks version
# ORCH-58: compose runs the container as uid:gid 1000:1000 (ORCH-40), but the base
# image has no passwd entry for uid 1000 -> ssh/whoami fail with
# "No user exists for uid 1000" (rc=255), breaking the detached self-deploy ssh

View File

@@ -14,7 +14,7 @@
- **Job-reaper** (`src/job_reaper.py`, ORCH-065 — [adr-0011](adr/adr-0011-job-reaper-lease-reclaim.md)) — фоновый daemon-поток (каркас `reconciler`), стартует/останавливается в `main.lifespan` (после `reconciler.start()` / перед `worker.stop()`). Детектирует «мёртвый» `running`-job **без рестарта** процесса (Tier-1 мёртвый `jobs.pid` после `reaper_dead_ticks` тиков; Tier-2 `agent_runs.exit_code` записан, а job ещё `running`; Tier-3 backstop `reaper_max_running_s`) и приводит строку к корректному статусу через те же контракты (`_try_advance_stage`/`_finalize_job`, gate-driven; exit≠0/неизвестно → `attempts<max``queued`, иначе `failed`+Telegram). Атомарный reap-claim (guard `status='running'`) совместим со стартовым `requeue_running_jobs`. Тот же поток периодически делает проактивный реклейм stale/dead merge-lease (см. ниже). never-raise; kill-switch `ORCH_REAPER_ENABLED`; снимок в `GET /queue` (блок `reaper`).
- **Reconciler** (`src/reconciler.py`, ORCH-053 — реализовано, [adr-0007](adr/adr-0007-reconciler.md)) — фоновый daemon-поток (паттерн `queue_worker`), стартует/останавливается в `main.lifespan` (после `worker.start()` / перед `worker.stop()`). Реконсилирует рассинхрон «источник истины ≠ стадия задачи» при потерянном webhook. F-1 gate-side (продвигает застрявшую стадию по локальной БД через штатный `advance_stage(..., finished_agent=None)`), F-2 plane-side (опрос Plane API → `handle_*` из `plane.py`), F-3 (БД-fallback `sha→branch` в `handle_ci_status`). Источник истины — гейт/Plane, не событие; идемпотентность (active-job guard + atomic-claim + grace); kill-switch `ORCH_RECONCILE_ENABLED`. `analysis` F-1 не трогает (человеческий гейт). F-1 также пропускает escalated (retry≥лимита) и Blocked/Needs-Input задачи (ORCH-060). Наблюдаемость — блок `reconcile` в `GET /queue`.
- **Project Registry** (`src/projects.py`, ORCH-6) — Plane project id → repo + prefix; фильтрация вебхуков по проекту.
- **Plane Sync** (`src/plane_sync.py`) — синхронизация статусов/комментариев в Plane.
- **Plane Sync** (`src/plane_sync.py`) — синхронизация статусов/комментариев в Plane. Резолв статусов проекта `get_project_states` (ORCH-10) кэширует `{logical_key→uuid}` per-project; **ORCH-068** добавляет в кэш-запись `{uuid→group}` (для терминал-исключения F-2) и **TTL** `ORCH_PLANE_STATES_TTL_S` (дефолт 300с; `0` → прежний lifetime-кэш) — устаревший набор статусов самозалечивается без рестарта процесса через существующий `reload_project_states()` (баг кэша после появления нового Plane-статуса). Форма возврата `get_project_states` неизменна (обратная совместимость).
## Конвейер и Quality Gates
@@ -36,7 +36,7 @@ created → analysis → architecture → development → review → testing →
| deploy | — | `check_deploy_status` | 14-deploy-log.md (`deploy_status:`) |
| done | — | — | — |
**Реестр QG** (`QG_CHECKS`): check_analysis_approved, check_analysis_complete, check_architecture_done, check_ci_green, check_review_approved, check_tests_passed, check_reviewer_verdict, check_tests_local, check_deploy_status, check_staging_status, check_branch_mergeable (ORCH-043), check_staging_image_fresh (ORCH-058).
**Реестр QG** (`QG_CHECKS`): check_analysis_approved, check_analysis_complete, check_architecture_done, check_ci_green, check_review_approved, check_tests_passed, check_reviewer_verdict, check_tests_local, check_deploy_status, check_staging_status, check_branch_mergeable (ORCH-043), check_staging_image_fresh (ORCH-058), check_security_gate (ORCH-022).
**Канон гейтов:** машинные вердикты читаются ТОЛЬКО из YAML-frontmatter, никогда из прозы. Лог-файлы мержатся в `origin/main` отдельным PR; гейт читает из `origin/main`.
@@ -70,21 +70,25 @@ Self-hosting зацикливался на `deploy-staging`: `scripts/staging_ch
а `deploy_status: SUCCESS` означает доказанный health-ok, не декларацию LLM. Три фазы
(детерминированно, без LLM в критическом пути self-restart):
- **Фаза A (вход в `deploy`)** — при `deploy_require_manual_approve=true` вместо запуска
прод-deployer выставляется approval-pending статус Plane + запрос approve
(Plane-коммент + Telegram). Перехват в `advance_stage` ПОСЛЕ `check_staging_status`
и merge-gate.
- **Фаза B (Plane → `Approved`)** — `advance_stage(deploy, finished_agent=None)`
прод-deployer выставляется approval-pending статус Plane + запрос перевести задачу
в статус **«Confirm Deploy»** (ORCH-059; Plane-коммент + Telegram). Перехват в
`advance_stage` ПОСЛЕ `check_staging_status` и merge-gate.
- **Фаза B (Plane → `Confirm Deploy`, ORCH-059)** —
`advance_stage(deploy, finished_agent=None, confirm_deploy=True)`
запускает **detached host-процесс** (ssh + setsid → хук с прод-параметрами +
build-once retag `SOURCE_IMAGE`) и ставит детерминированный **finalizer-job**;
маркер `initiated` — идемпотентность. Возврат БЕЗ advance (вердикта ещё нет).
Обычный `Approved` на `deploy` (`confirm_deploy=False`) — детерминированный no-op
(не деплоит и не откатывает).
- **Фаза C (finalizer)** — новый контейнер после рестарта читает sentinel `result`
(exit-code хука), маппит `0→SUCCESS / иначе→FAILED`, пишет `14-deploy-log.md`,
вызывает `advance_stage(deploy, finished_agent="deployer")` → существующие контракты:
`SUCCESS → done`, `FAILED → откат БАГ-8 на development`.
Approve = смена статуса Plane на `Approved` (status-only verdict model; комментарии
не управляют конвейером). На старте — обязательный ручной approve (флаг `true`); полный
авто — отдельная задача (ORCH-54). Условность как ORCH-35: реально для `orchestrator`,
Триггер прод-деплоя = смена статуса Plane на `Confirm Deploy` (ORCH-059; status-only
verdict model; комментарии не управляют конвейером). `Approved` остаётся исключительно
человеческим гейтом конвейера и прод-деплой не запускает. На старте — обязательный
ручной approve (флаг `true`); полный авто — отдельная задача (ORCH-54). Условность как ORCH-35: реально для `orchestrator`,
прочие репо — прежний синхронный ssh-деплой агентом. Контракты не меняются:
`STAGE_TRANSITIONS`, реестр QG, `check_deploy_status`/`_parse_deploy_status`, БАГ-8,
terminal-sync, merge-gate, exit-code-контракт хука. Restart-safe состояние —
@@ -92,6 +96,31 @@ sentinel-файлы (`<repos_dir>/.deploy-state-<repo>/<wi>/`), без мигр
Подробнее: [adr-0007](adr/adr-0007-executable-self-deploy.md), детально —
`docs/work-items/ORCH-036/06-adr/ADR-001-executable-self-deploy.md`.
#### Выделенный статус-триггер прод-деплоя «Confirm Deploy» (ORCH-059 — реализовано)
Перегрузка: один Plane-статус `Approved` служил И человеческим гейтом BRD на
`analysis` (`check_analysis_approved`), И триггером Фазы B прод-деплоя на `deploy`
— привычный жест approve молча запускал прод-рестарт (групповой self-hosting
риск). ORCH-059 разделяет жесты: вводится отдельный логический статус
`confirm_deploy` («Confirm Deploy»), который триггерит **ТОЛЬКО** Фазу B на
`deploy`; `Approved` остаётся исключительно гейтом конвейера.
- `_PLANE_NAME_TO_KEY` += `"Confirm Deploy" → "confirm_deploy"`; в
`_DEFAULT_STATES` ключ НЕ добавляется (нет UUID для enduro/fallback) →
**fail-closed**: нет статуса → нет деплоя, без `KeyError` (доступ через `.get`).
- `handle_issue_updated` маршрутизирует `Confirm Deploy``handle_confirm_deploy`
(гард `stage=="deploy"`) → `_try_advance_stage(..., confirm_deploy=True)`.
- `advance_stage` получает kwarg `confirm_deploy: bool=False`; блок Фазы B
(`deploy`+`finished_agent is None`+self-hosting) деплоит ТОЛЬКО при
`confirm_deploy=True`, иначе (обычный `Approved`) — **no-op** (`check_deploy_status`
не запускается → нет ложного отката БАГ-8).
- CTA Фазы A (`_handle_self_deploy_phase_a`) просит «Confirm Deploy», не «Approved».
- Условность как ORCH-35/36 (только `orchestrator`); Фазы A/C, `STAGE_TRANSITIONS`,
`QG_CHECKS`, `check_deploy_status`, merge-gate, схема БД — без изменений.
- Эксплуатация: в Plane-проекте ORCH создать статус «Confirm Deploy» + сброс кэша
состояний (`docs/work-items/ORCH-059/07-infra-requirements.md`).
Детально — `docs/work-items/ORCH-059/06-adr/ADR-001-confirm-deploy-status.md`
(уточняет/триггер Фазы B относительно adr-0007).
### Post-deploy наблюдение прода + реакция на деградацию (ORCH-021 — реализовано)
Конвейер заканчивался на `deploy → done` и **забывал про прод**: «успех» = health-check
в момент рестарта (~60с). Класс «зелёный деплой, красный прод» (прецедент ET-8 —
@@ -155,6 +184,38 @@ helper `validated_revision` питает и штамп A, и `EXPECTED_REVISION`
образа, без миграций). Подробнее: [adr-0008](adr/adr-0008-staging-image-provenance.md),
детально — `docs/work-items/ORCH-058/06-adr/ADR-001-staging-image-provenance.md`.
### Security-гейт: secret-scanning + dependency audit перед мержем (ORCH-022 — реализовано)
Автономный конвейер вливал ветку в `main` без проверки на утёкший секрет (ключ/токен/пароль/
приватный ключ) и уязвимую зависимость (CVE); для self-hosting один секрет/CVE через одну
задачу уезжал в общий прод всех проектов (CLAUDE.md §8). ORCH-022 вводит детерминированный
(без LLM) **security-гейт как под-гейт ребра `deploy-staging → deploy`**, рядом с merge-gate
(ORCH-043) и image-freshness (ORCH-058), исполняемый **ПЕРВЫМ** среди edge-под-гейтов
(ДО merge-gate). Паттерн соседей: leaf `src/security_gate.py` (never-raise) + тонкая обёртка
`check_security_gate` в `QG_CHECKS` + врезка `_handle_security_gate` в `advance_stage`.
`STAGE_TRANSITIONS` и схема БД — **без изменений**.
- **Secret-scanning (`gitleaks`, offline):** скан `origin/main..HEAD`; любой секрет вне
аллоулиста `.gitleaks.toml` → вклад в FAIL. Offline → гарантия «секрет всегда блокирует»
не зависит от сети (безусловна).
- **Dependency audit (`pip-audit`, OSV/PyPI):** severity ≥ `security_dep_block_severity`
(дефолт `HIGH`) → FAIL; ниже / UNKNOWN → warning. Недоступность фида → **fail-open +
громкий warning** (анти-петля ORCH-061; флаг `security_dep_audit_fail_closed` для строгого
режима). best-effort при доступности фида.
- **ПЕРВЫМ, ДО merge-gate:** дёшево фейлить до дорогих rebase/rebuild; скан ветки ДО rebase
не «обвиняет» задачу в CVE из обновившегося `main`; до захвата merge-lease → при FAIL lease
освобождать не нужно.
- **Артефакт `17-security-report.md`** (YAML-frontmatter `security_status`/`secrets_found`/
`deps_blocking`/`deps_warning`/`deps_audit_degraded`); вердикт читается ТОЛЬКО из
frontmatter (гейт пишет → читает обратно через `parse_security_status` → возвращает: единый
источник истины), negative-токен авторитетен, битый/нет → fail-closed.
- **FAIL → откат на `development`** + developer-retry (общий `_developer_retry_count`, cap 3,
затем `set_issue_blocked` + Telegram); `task_desc` несёт дословные находки (ORCH-046).
- **Условность как ORCH-35/43/58:** `security_gate_enabled` + `security_gate_repos` (пусто →
только self-hosting); never-raise; таймаут `security_scan_timeout_s`; гейт не деплоит/не
рестартит прод. v1 — Python-only; SAST/мульти-стек — follow-up (BR-14).
Подробнее: [adr-0012](adr/adr-0012-security-gate.md), детально —
`docs/work-items/ORCH-022/06-adr/ADR-001-security-gate.md`.
### Reconciler: реконсиляция потерянных webhook (ORCH-053 — реализовано)
Конвейер продвигается только входящими webhook; потерянное событие (502 на ребилде,
нет ретраев у Plane/Gitea, неразрезолвленный `sha→branch`) → задача застревает молча
@@ -174,11 +235,21 @@ helper `validated_revision` питает и штамп A, и `EXPECTED_REVISION`
retry-count проверяется первым (дёшево, локальный SQL).
- **F-2 plane-side:** опрос Plane API per-project → `handle_status_start` /
`handle_verdict` из `webhooks/plane.py` (логика не дублируется).
**ORCH-068 (livelock-fix):** (1) задачи в **терминальной группе** Plane
(`state.group ∈ {completed, cancelled}`, fallback — логические ключи
`done`/`cancelled`) исключаются из actionable-выборки per-issue — проектно-независимо,
устойчиво к UUID-алиасингу после переименований статусов (ORCH-066); (2) `_note_unblock`
(лог + Telegram + `unblocked_total`) вызывается ТОЛЬКО при **подтверждённом state change**
(сравнение стадии задачи до/после `_dispatch`; no-op dispatch → тишина), плюс in-memory
дедуп по `issue_id→state`. Восстанавливает инвариант silence-when-in-sync (AC-9/AC-10).
Детали — `docs/work-items/ORCH-068/06-adr/ADR-001-reconciler-terminal-exclusion-and-cache-ttl.md`.
- **F-3:** усиление `sha→branch` в `handle_ci_status` (БД-fallback по единственной
development-задаче repo; неоднозначность → не резолвим).
- **F-4 observability:** при разблокировке — лог-строка `reconciler: <wi> <stage>
разблокирована (потерян webhook)` + Telegram (`reconcile_notify_unblock`); снимок
состояния в `GET /queue` (блок `reconcile`).
состояния в `GET /queue` (блок `reconcile`). **ORCH-068** добавляет в снимок
счётчики `skipped_terminal_total` (исключённые терминалы) и `deduped_total`
(подавленные повторные нотификации).
Реализация: `src/reconciler.py` (daemon-поток по образцу `queue_worker`), стартует в
`main.lifespan` **после** `worker.start()`, останавливается в `finally` **перед**
@@ -346,4 +417,7 @@ Monitoring after Deploy → Done
Схема БД, потоки данных, resilience-слой, детали Dockerfile — [internals.md](internals.md).
---
*Актуально на 2026-06-07. Обновлять при изменении src/stages.py, src/qg/checks.py, src/main.py. Статусы доработок: ORCH-036 (исполняемый самодеплой `deploy`, adr-0007) — реализовано; ORCH-043 (merge-gate, adr-0006) — design, ветка feature/ORCH-043; ORCH-053 (reconciler, adr-0007, src/reconciler.py) — реализовано; ORCH-060 (F-1 skip escalated/Blocked/Needs-Input, `docs/work-items/ORCH-060/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-060 (Guard 1 `developer_retry_count>=MAX_DEVELOPER_RETRIES` + Guard 2 `plane_sync.fetch_issue_state` Blocked/Needs-Input, флаг `ORCH_RECONCILE_SKIP_BLOCKED_ENABLED`); ORCH-058 (провенанс staging-образа: check_staging_image_fresh + staging_check свежего образа + хук-guard, adr-0008) — реализовано в ветке feature/ORCH-058 (обновлять также при изменении src/image_freshness.py, scripts/orchestrator-deploy-hook.sh, Dockerfile); ORCH-061 (толерантность staging-вердикта к инфра-FAIL C9a/C9b, adr-0009, `docs/work-items/ORCH-061/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-061 (обновлять также при изменении src/staging_verdict.py, scripts/staging_check.py, флаг staging_infra_tolerance_enabled); ORCH-021 (post-deploy наблюдение прода + реакция на деградацию, adr-0010, `docs/work-items/ORCH-021/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-021-post-deploy-rollback (reserved-agent job `post-deploy-monitor`: арм в src/stage_engine.py блок `next_stage == "done"`, тик `run_post_deploy_monitor` + перехват в src/agents/launcher.py ДО _spawn; чистая логика src/post_deploy.py never-raise; флаги `post_deploy_*` в src/config.py; блок `post_deploy` в `/queue`; артефакт 16-post-deploy-log.md; self-hosting всегда ALERT_ONLY — тик не рестартит прод; обновлять также при изменении src/post_deploy.py / арм-блока / launcher-перехвата); ORCH-065 (job-reaper + проактивный реклейм merge-lease + идемпотентная финализация merge, adr-0011, `docs/work-items/ORCH-065/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-065 (новый daemon-поток src/job_reaper.py + старт/стоп в src/main.py lifespan; колонка `jobs.pid` через _ensure_column + проставление в src/agents/launcher.py `_spawn`; функции реклейма lease `pid_alive`/`reclaim_stale_lease` + guard `pr_already_merged` в src/merge_gate.py (консультируется merge-актором — промпт `.openclaw/agents/deployer.md`); флаги `reaper_*`/`lease_reclaim_*` в src/config.py; блок `reaper` в `/queue`; обновлять также при изменении этих мест); ORCH-022 (security-гейт: secret-scanning gitleaks + dependency audit pip-audit как под-гейт ребра `deploy-staging → deploy` ПЕРВЫМ, adr-0012, `docs/work-items/ORCH-022/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-022-security-secret-scanning (leaf src/security_gate.py never-raise + check_security_gate в src/qg/checks.py `QG_CHECKS` + врезка _handle_security_gate в src/stage_engine.py блок `current_stage == "deploy-staging"` ПЕРВОЙ; флаги `security_*` в src/config.py; gitleaks (pinned) в Dockerfile, pip-audit в requirements.txt, `.gitleaks.toml` в корне; артефакт 17-security-report.md; обновлять также при изменении этих мест).*
*Актуально на 2026-06-07. Обновлять при изменении src/stages.py, src/qg/checks.py, src/main.py. Статусы доработок: ORCH-036 (исполняемый самодеплой `deploy`, adr-0007) — реализовано; ORCH-043 (merge-gate, adr-0006) — design, ветка feature/ORCH-043; ORCH-053 (reconciler, adr-0007, src/reconciler.py) — реализовано; ORCH-060 (F-1 skip escalated/Blocked/Needs-Input, `docs/work-items/ORCH-060/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-060 (Guard 1 `developer_retry_count>=MAX_DEVELOPER_RETRIES` + Guard 2 `plane_sync.fetch_issue_state` Blocked/Needs-Input, флаг `ORCH_RECONCILE_SKIP_BLOCKED_ENABLED`); ORCH-058 (провенанс staging-образа: check_staging_image_fresh + staging_check свежего образа + хук-guard, adr-0008) — реализовано в ветке feature/ORCH-058 (обновлять также при изменении src/image_freshness.py, scripts/orchestrator-deploy-hook.sh, Dockerfile); ORCH-061 (толерантность staging-вердикта к инфра-FAIL C9a/C9b, adr-0009, `docs/work-items/ORCH-061/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-061 (обновлять также при изменении src/staging_verdict.py, scripts/staging_check.py, флаг staging_infra_tolerance_enabled); ORCH-021 (post-deploy наблюдение прода + реакция на деградацию, adr-0010, `docs/work-items/ORCH-021/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-021-post-deploy-rollback (reserved-agent job `post-deploy-monitor`: арм в src/stage_engine.py блок `next_stage == "done"`, тик `run_post_deploy_monitor` + перехват в src/agents/launcher.py ДО _spawn; чистая логика src/post_deploy.py never-raise; флаги `post_deploy_*` в src/config.py; блок `post_deploy` в `/queue`; артефакт 16-post-deploy-log.md; self-hosting всегда ALERT_ONLY — тик не рестартит прод; обновлять также при изменении src/post_deploy.py / арм-блока / launcher-перехвата); ORCH-065 (job-reaper + проактивный реклейм merge-lease + идемпотентная финализация merge, adr-0011, `docs/work-items/ORCH-065/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-065 (новый daemon-поток src/job_reaper.py + старт/стоп в src/main.py lifespan; колонка `jobs.pid` через _ensure_column + проставление в src/agents/launcher.py `_spawn`; функции реклейма lease `pid_alive`/`reclaim_stale_lease` + guard `pr_already_merged` в src/merge_gate.py (консультируется merge-актором — промпт `.openclaw/agents/deployer.md`); флаги `reaper_*`/`lease_reclaim_*` в src/config.py; блок `reaper` в `/queue`; обновлять также при изменении этих мест); ORCH-059 (выделенный статус-триггер прод-деплоя «Confirm Deploy», ADR `docs/work-items/ORCH-059/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-059 (маппинг `"Confirm Deploy"→"confirm_deploy"` в src/plane_sync.py `_PLANE_NAME_TO_KEY`, НЕ в `_DEFAULT_STATES` = fail-closed; ветка `handle_confirm_deploy` + fail-closed `.get("confirm_deploy")` в src/webhooks/plane.py `handle_issue_updated`; keyword-only `confirm_deploy` в src/stage_engine.py `advance_stage` — Фаза B деплоит ТОЛЬКО при `confirm_deploy=True`, иначе `Approved`-на-`deploy` = no-op; CTA Фазы A просит «Confirm Deploy»; эксплуатация — статус доски «Confirm Deploy» в Plane-проекте ORCH, `docs/work-items/ORCH-059/07-infra-requirements.md`).*
*Актуально на 2026-06-07. Обновлять при изменении src/stages.py, src/qg/checks.py, src/main.py. Статусы доработок: ORCH-036 (исполняемый самодеплой `deploy`, adr-0007) — реализовано; ORCH-043 (merge-gate, adr-0006) — design, ветка feature/ORCH-043; ORCH-053 (reconciler, adr-0007, src/reconciler.py) — реализовано; ORCH-060 (F-1 skip escalated/Blocked/Needs-Input, `docs/work-items/ORCH-060/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-060 (Guard 1 `developer_retry_count>=MAX_DEVELOPER_RETRIES` + Guard 2 `plane_sync.fetch_issue_state` Blocked/Needs-Input, флаг `ORCH_RECONCILE_SKIP_BLOCKED_ENABLED`); ORCH-058 (провенанс staging-образа: check_staging_image_fresh + staging_check свежего образа + хук-guard, adr-0008) — реализовано в ветке feature/ORCH-058 (обновлять также при изменении src/image_freshness.py, scripts/orchestrator-deploy-hook.sh, Dockerfile); ORCH-061 (толерантность staging-вердикта к инфра-FAIL C9a/C9b, adr-0009, `docs/work-items/ORCH-061/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-061 (обновлять также при изменении src/staging_verdict.py, scripts/staging_check.py, флаг staging_infra_tolerance_enabled); ORCH-021 (post-deploy наблюдение прода + реакция на деградацию, adr-0010, `docs/work-items/ORCH-021/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-021-post-deploy-rollback (reserved-agent job `post-deploy-monitor`: арм в src/stage_engine.py блок `next_stage == "done"`, тик `run_post_deploy_monitor` + перехват в src/agents/launcher.py ДО _spawn; чистая логика src/post_deploy.py never-raise; флаги `post_deploy_*` в src/config.py; блок `post_deploy` в `/queue`; артефакт 16-post-deploy-log.md; self-hosting всегда ALERT_ONLY — тик не рестартит прод; обновлять также при изменении src/post_deploy.py / арм-блока / launcher-перехвата); ORCH-065 (job-reaper + проактивный реклейм merge-lease + идемпотентная финализация merge, adr-0011, `docs/work-items/ORCH-065/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-065 (новый daemon-поток src/job_reaper.py + старт/стоп в src/main.py lifespan; колонка `jobs.pid` через _ensure_column + проставление в src/agents/launcher.py `_spawn`; функции реклейма lease `pid_alive`/`reclaim_stale_lease` + guard `pr_already_merged` в src/merge_gate.py (консультируется merge-актором — промпт `.openclaw/agents/deployer.md`); флаги `reaper_*`/`lease_reclaim_*` в src/config.py; блок `reaper` в `/queue`; обновлять также при изменении этих мест); ORCH-066 (осмысленная статусная модель Plane — слой B, `docs/work-items/ORCH-066/06-adr/ADR-001-plane-status-model.md`) — реализовано в ветке feature/ORCH-066-plane (только Plane-индикация: новые ключи `to_analyse`/`analysis`/`code_review`/`awaiting_deploy`/`deploying`/`monitoring` в `_PLANE_NAME_TO_KEY`/`_DEFAULT_STATES` + project-relative `_STATE_ALIAS_FALLBACK` в get_project_states + `_STAGE_TO_STATE_KEY` analysis/review + 5 новых `set_issue_*` в src/plane_sync.py; триггер `in_progress`→`to_analyse` и `set_issue_analysis` в src/webhooks/plane.py; Phase A→Awaiting Deploy / Phase B→Deploying / terminal-sync split monitoring↔done / post-deploy monitor HEALTHY→Done DEGRADED→Blocked в src/stage_engine.py; F-2 триггер `to_analyse` + Guard 2 skip-set с вычитанием base_working в src/reconciler.py; `STAGE_TRANSITIONS`/QG/схема БД НЕ трогаются; без kill-switch — раскат гейтится созданием 6 Plane-статусов оператором, `docs/work-items/ORCH-066/07-infra-requirements.md`; обновлять при изменении этих мест).*
*Актуально на 2026-06-07. Обновлять при изменении src/stages.py, src/qg/checks.py, src/main.py. Статусы доработок: ORCH-036 (исполняемый самодеплой `deploy`, adr-0007) — реализовано; ORCH-043 (merge-gate, adr-0006) — design, ветка feature/ORCH-043; ORCH-053 (reconciler, adr-0007, src/reconciler.py) — реализовано; ORCH-060 (F-1 skip escalated/Blocked/Needs-Input, `docs/work-items/ORCH-060/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-060 (Guard 1 `developer_retry_count>=MAX_DEVELOPER_RETRIES` + Guard 2 `plane_sync.fetch_issue_state` Blocked/Needs-Input, флаг `ORCH_RECONCILE_SKIP_BLOCKED_ENABLED`); ORCH-058 (провенанс staging-образа: check_staging_image_fresh + staging_check свежего образа + хук-guard, adr-0008) — реализовано в ветке feature/ORCH-058 (обновлять также при изменении src/image_freshness.py, scripts/orchestrator-deploy-hook.sh, Dockerfile); ORCH-061 (толерантность staging-вердикта к инфра-FAIL C9a/C9b, adr-0009, `docs/work-items/ORCH-061/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-061 (обновлять также при изменении src/staging_verdict.py, scripts/staging_check.py, флаг staging_infra_tolerance_enabled); ORCH-021 (post-deploy наблюдение прода + реакция на деградацию, adr-0010, `docs/work-items/ORCH-021/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-021-post-deploy-rollback (reserved-agent job `post-deploy-monitor`: арм в src/stage_engine.py блок `next_stage == "done"`, тик `run_post_deploy_monitor` + перехват в src/agents/launcher.py ДО _spawn; чистая логика src/post_deploy.py never-raise; флаги `post_deploy_*` в src/config.py; блок `post_deploy` в `/queue`; артефакт 16-post-deploy-log.md; self-hosting всегда ALERT_ONLY — тик не рестартит прод; обновлять также при изменении src/post_deploy.py / арм-блока / launcher-перехвата); ORCH-065 (job-reaper + проактивный реклейм merge-lease + идемпотентная финализация merge, adr-0011, `docs/work-items/ORCH-065/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-065 (новый daemon-поток src/job_reaper.py + старт/стоп в src/main.py lifespan; колонка `jobs.pid` через _ensure_column + проставление в src/agents/launcher.py `_spawn`; функции реклейма lease `pid_alive`/`reclaim_stale_lease` + guard `pr_already_merged` в src/merge_gate.py (консультируется merge-актором — промпт `.openclaw/agents/deployer.md`); флаги `reaper_*`/`lease_reclaim_*` в src/config.py; блок `reaper` в `/queue`; обновлять также при изменении этих мест); ORCH-068 (livelock-fix reconciler F-2: терминал-исключение по группе состояния + `_note_unblock` только при подтверждённом state change + дедуп; TTL `_STATES_CACHE`, `docs/work-items/ORCH-068/06-adr/ADR-001`) — реализовано в ветке feature/ORCH-068 (D1 терминал-гард по группе `_is_terminal_state` + `get_project_state_groups` в src/plane_sync.py; D2 сравнение стадии до/после `_dispatch` + дедуп-словарь в src/reconciler.py; TTL-запись `_STATES_CACHE` + флаг `plane_states_ttl_s` в src/config.py; счётчики `skipped_terminal_total`/`deduped_total` в `/queue`; обновлять также при изменении src/reconciler.py F-2, src/plane_sync.py `get_project_states`/`get_project_state_groups`/`_STATES_CACHE`).*

View File

@@ -69,6 +69,15 @@ grace + `max_concurrency=1`); never-raise на единицу работы; ти
задачи. Инварианты adr-0007 сохранены (схема/реестры не меняются, never-raise,
тишина при пропуске).
- **ORCH-068** (`docs/work-items/ORCH-068/06-adr/ADR-001-reconciler-terminal-exclusion-and-cache-ttl.md`):
фикс livelock F-2 (спам `_note_unblock` по синхронизированной done-задаче после
ORCH-066). F-2 исключает терминалы по **группе состояния** (`completed`/`cancelled`,
fallback — ключи `done`/`cancelled`) проектно-независимо; `_note_unblock` — только при
подтверждённом state change (сравнение стадии до/после `_dispatch`) + in-memory дедуп;
`_STATES_CACHE` получает TTL (`ORCH_PLANE_STATES_TTL_S`, дефолт 300с, `0`=lifetime).
Инварианты adr-0007 сохранены (источник истины — Plane; реестры/схема/`handle_*`/F-1/F-3
не меняются; never-raise; kill-switch'и).
## Связи
adr-0002 (очередь / `available_at`, single-process-singleton), adr-0003 (условный
гейт — образец условности/флагов раската), adr-0006 (merge-gate как под-гейт ребра

View File

@@ -0,0 +1,63 @@
# adr-0012: Security-гейт — secret-scanning + dependency audit перед мержем
- **Статус:** proposed
- **Дата:** 2026-06-07
- **Задача:** ORCH-022
- **Детальный ADR:** `docs/work-items/ORCH-022/06-adr/ADR-001-security-gate.md`
## Контекст
Оркестратор автономен: `developer` пишет код без человека-фильтра. Перед слиянием ветки в
`main` нет проверки на утёкший секрет (ключ/токен/пароль/приватный ключ) и уязвимую
зависимость (CVE). Для self-hosting один общий прод-инстанс обслуживает все проекты с общей
БД — секрет/CVE через одну задачу попадает в прод всех (CLAUDE.md §self-hosting, §8). Фактический
мерж PR в `main` делает `deployer` в начале стадии `deploy`.
## Решение
Детерминированный (без LLM) **security-гейт как под-гейт ребра `deploy-staging → deploy`**,
рядом с merge-gate (ORCH-043) и image-freshness (ORCH-058), исполняемый **ПЕРВЫМ** среди
edge-под-гейтов (ДО merge-gate). `STAGE_TRANSITIONS` не меняется; в `QG_CHECKS` добавлен
`check_security_gate`. Паттерн — как у соседей: leaf-модуль `src/security_gate.py`
(never-raise) + тонкая обёртка в `QG_CHECKS` + врезка `_handle_security_gate` в `advance_stage`.
- **Secret-scanning (`gitleaks`, offline):** скан `origin/main..HEAD`; любой секрет вне
аллоулиста (`.gitleaks.toml`) → вклад в FAIL. Offline → гарантия «секрет всегда блокирует»
не зависит от сети.
- **Dependency audit (`pip-audit`, OSV/PyPI):** severity ≥ `security_dep_block_severity`
(дефолт `HIGH`) → FAIL; ниже / UNKNOWN → warning. Недоступность фида → **fail-open +
громкий warning** (анти-петля; флаг `security_dep_audit_fail_closed` для строгого режима).
- **ПЕРВЫМ на ребре, ДО merge-gate:** дёшево фейлить до дорогих rebase/rebuild; скан ветки
ДО rebase не «обвиняет» задачу в CVE, притащенной обновившимся `main` (анти-петля
ORCH-061); до захвата merge-lease → при FAIL lease освобождать не нужно.
- **Артефакт `17-security-report.md`** с YAML-frontmatter (`security_status`,
`secrets_found`, `deps_blocking`, `deps_warning`, `deps_audit_degraded`); вердикт читается
ТОЛЬКО из frontmatter (канон), negative-токен авторитетен; битый/нет → fail-closed.
- **FAIL → откат на `development`** + developer-retry (общий `_developer_retry_count`, cap 3,
затем `set_issue_blocked` + Telegram); `task_desc` несёт дословные находки (ORCH-046).
- **Условность (как ORCH-35/43/58):** `security_gate_enabled` + `security_gate_repos`; пусто
→ реально только self-hosting (`orchestrator`), прочие репо — no-op pass.
- **never-raise**, таймаут `security_scan_timeout_s`, гейт не деплоит/не рестартит прод.
## Альтернативы
- **Вариант R (review-стадия):** diff может разойтись с мержем в `main`; merge-edge — последняя
страховка. Отклонено.
- **Вариант C (CI-job через `check_ci_green`):** пороги/severity/аллоулист/артефакт плохо
выражаются статусом коммита; коуплинг с раннером. Отклонено для v1 (точка расширения).
- **Новая стадия `security`:** «пустая» стадия без агента не имеет триггера (как в ORCH-043).
Отклонено.
- **fail-closed dep-audit / аудит после rebase:** ложные откаты → петля. Отклонено.
- **Новая колонка retry в БД:** не нужна (переиспользуем `_developer_retry_count`).
## Последствия
- Класс «тихо влитый секрет/CVE» закрыт: секреты — безусловно (offline), CVE — best-effort при
доступности фида. Самоприменение CLAUDE.md §8 без человека.
- Плата: ещё один «скрытый» под-гейт ребра (нет в `STAGE_TRANSITIONS`); внешние инструменты
(gitleaks в образе, pip-audit в зависимостях); время скана на каждом прогоне (ограничено
таймаутом); v1 — Python-only (SAST/мульти-стек — follow-up WI).
- Сквозное изменение (новый QG + edge-под-гейт) → `arch:major-change`; прод-деплой ORCH-022 —
строго через staging-гейт (8501), без рестарта прод-контейнера.
## Связи
adr-0006 (merge-gate — паттерн edge-под-гейта/отката), adr-0008 (image-freshness —
условность/never-raise/fail-closed), adr-0003 (условный гейт / `is_self_hosting_repo`),
adr-0009 (анти-петля ложных FAIL, ORCH-061), ORCH-046 (дословный reason в `task_desc`),
ORCH-9/15 (мульти-стек — будущая зависимость), ORCH-2 (worktree-изоляция).

View File

@@ -0,0 +1,33 @@
# Lessons Learned — 2026-06-08: статус `Confirm Deploy` не триггерит Phase B (мёртвый триггер)
## Контекст
ORCH-066 ввела новую статусную модель Plane, включая человекочитаемый статус **`Confirm Deploy`** для прод-деплойного approve-gate (self-deploy Phase B). Орк сам выставляет задачу в `Awaiting Deploy` / `Confirm Deploy` через `set_issue_awaiting_deploy()` и т.п.
## Инцидент (2026-06-08, первый реальный прод-self-deploy — ORCH-068)
Слава нажал статус **`Confirm Deploy`** в Plane, ожидая запуск прод-деплоя. Орк ответил `no pipeline action` и НИЧЕГО не запустил. Прод-деплой стартовал только после ручного перевода в **`Approved`**.
## Root cause
Диспетчер статусов `handle_issue_status` (`src/webhooks/plane.py` ~158-166) слушает РОВНО три состояния:
```python
if new_state == proj_states["to_analyse"]: await handle_status_start(...)
elif new_state == proj_states["approved"]: await handle_verdict(..., approved=True)
elif new_state == proj_states["rejected"]: await handle_verdict(..., approved=False)
else: logger.info("... no pipeline action")
```
Phase B (прод-деплой) триггерится в `_try_advance_stage` (`src/stage_engine.py` ~215-224) при `current_stage == "deploy" and finished_agent is None` — то есть ТОЛЬКО когда пришёл вебхук `Approved`. Статус `Confirm Deploy` в эту тройку НЕ входит → ветка `else` → no-op.
**ORCH-066 добавила статус как МЕТКУ (запись), но не подключила обратный путь (чтение/триггер).** Классическая дыра: протестировали, что орк правильно СТАВИТ статус, но не протестировали, что нажатие этого статуса человеком РЕАЛЬНО запускает действие.
## Почему не поймали тестирование/ревью
1. **Не в scope ORCH-068.** ORCH-068 чинит reconciler (BRD §6 N1-N3 явно: не трогать диспетчер статусов / Phase B). Тестер прогнал TC-01..13 — все про reconciler/terminal-статусы. Ревьюер смотрел diff reconciler.py/plane_sync.py. Корректно — это дефект ORCH-066, не 068.
2. **Дыра ORCH-066.** Её тесты, видимо, проверяли запись статусов, а не обратный триггер.
3. **Staging не покрывает прод-путь.** Phase A (staging-деплой) автоматический, ручной `Confirm Deploy` живёт ТОЛЬКО на прод-пути, который на staging не гоняется. Поэтому всплыло лишь на первом реальном прод-деплое.
## Уроки
1. **Тестировать обратный путь статусов, не только запись.** Для каждого статуса, который человек может нажать, нужен тест «нажатие → ожидаемое pipeline-действие». Запись (орк ставит статус) и чтение (орк реагирует на статус) — два разных контракта.
2. **Прод-only пути (ручной Confirm Deploy) нуждаются в явном тесте/чеклисте.** Staging их не ловит by design. Любой approve-gate, доступный человеку, обязан иметь регресс-тест на триггер.
3. **Новый статус = подключить В ОБЕ стороны.** При добавлении статуса в модель — сразу проверить, что диспетчер `handle_issue_status` его слушает (если он actionable), а не только что орк его выставляет.
4. **UX-консистентность:** статус, названный действием («Confirm Deploy»), обязан выполнять это действие. Иначе оператор жмёт интуитивную кнопку, а система молчит → потеря доверия к автономности.
## Фикс
Заведена ORCH-070: подключить `Confirm Deploy` (или его actionable-эквивалент) к триггеру Phase B в `handle_issue_status`, + регресс-тест на обратный путь статусов прод-деплоя. Source-of-truth и существующий `Approved`-путь не ломать (обратная совместимость).

View File

@@ -0,0 +1,47 @@
# Lessons Learned — 2026-06-08: «Фантомный merge» — прод деплоится, но код не сливается в main
## Severity: CRITICAL (потеря целостности main, накопительная потеря кода между задачами)
## Резюме
Self-deploy (Phase B) собирал прод-образ из ВЕТКИ задачи и рапортовал `finalize SUCCESS` + `post-deploy HEALTHY`, но git-merge ветки в `main` НЕ происходил. PR оставался `open`. Следующая задача срезала свою ветку от устаревшего main → теряла код незалитых предшественников. Накопительно потеряны в main: **ORCH-022, ORCH-059, ORCH-066, ORCH-068** (PR#67/68/69/70 — все open, merged=False). Последний реально слитый — ORCH-065 (PR#66).
## Как обнаружено
Симптом: ORCH-067 переведён в `To Analyse`, но конвейер не стартовал (`no pipeline action`). Причина — прод слушал старый триггер `in_progress`, а не `to_analyse` (ORCH-066). При разборе выяснилось: код ORCH-066 не в проде, хотя он «деплоился».
Решающее наблюдение оператора (Слава): «спам ET-002 начался СРАЗУ после деплоя 66 → значит код деплоился». Это вскрыло механизм: код 66 БЫЛ в проде 22:1705:32, потом стёрт деплоем 068 (срезан от старого main без 66).
## Доказательная база (как подтверждали — воспроизводимый метод)
1. **PR-статус (Gitea API):** PR#67(022)/68(059)/69(066)/70(068) = open, merged=False. PR#66(065) = merged=True (последний честный).
2. **md5-сверка файлов прод vs origin/main vs ветка:**
- `src/reconciler.py`, `src/plane_sync.py`: prod md5 == ветка ORCH-068 != main → прод = снимок ветки 068, НЕ main.
- `src/webhooks/plane.py`: prod == main == ветка-068 (ветка 068 этот файл не трогала → видна старая база без to_analyse).
3. **git merge-base:** ветка ORCH-068 срезана от `bb03350` (ORCH-065), не от кода 066. История ветки-068 по 066 содержит только `docs staging`, кода (`to_analyse`) нет.
4. **Таймлайн логов:** деплой 22:17 (ветка-066, сломанный reconciler) → спам ET-002 начался; деплой 05:32 (ветка-068, база 065 без 66) → спам прекратился (0 после 05:33). Подтверждает: прод-образ = снимок ВЕТКИ, меняется при каждом деплое, теряет незалитое.
## Root cause (гипотеза → нужен код-аудит self_deploy/merge_gate)
Self-deploy Phase B инициирует прод-деплой из worktree ветки (BUILD-ONCE из validated commit). Шаг git-merge ветки в main:
- ЛИБО не вызывается на self-hosting пути (Phase B уходит в detached host-процесс, finalizer пишет SUCCESS-маркеры, но merge отдельно и молча скипается/падает),
- ЛИБО регресс фикса ORCH-065 (idempotent merge / merge-lease reclaim): guard `pr_already_merged` или lease-reclaim ошибочно считает PR уже слитым / не докатывает merge после рестарта контейнера (а Phase B ИМЕННО рестартит контейнер → процесс, державший merge-lease, умирает до завершения merge).
Симптоматически ORCH-065 был последним успешным merge — деградация началась СРАЗУ после него или из-за взаимодействия его механики с self-deploy-рестартом.
## Почему конвейер не заметил
- `finalize SUCCESS` и `post-deploy HEALTHY` маркеры пишутся НЕЗАВИСИМО от факта merge. Пайплайн считает задачу done по этим маркерам, git-состояние main не верифицируется.
- Прод здоров (образ из ветки рабочий) → health-check зелёный → нет сигнала о проблеме.
- Дыра видна только при сравнении main с прод ИЛИ когда следующая задача теряет код предыдущей (что и случилось с 67).
## Уроки
1. **Деплой ОБЯЗАН верифицировать, что код реально в main ПОСЛЕ деплоя.** finalize SUCCESS без проверки `git merge-base origin/main == deployed_commit` (или PR.merged==true) — фальшивый зелёный. Добавить post-merge верификацию: deployed SHA должен быть предком origin/main.
2. **Маркер «deployed» != «merged».** Нельзя считать задачу завершённой по staging/post-deploy-маркерам, если PR не закрыт merge. Гейт: задача → done ТОЛЬКО при PR.merged==true.
3. **Self-deploy рестартит контейнер → любой держатель merge-lease/незавершённый git-шаг умирает.** Merge ДОЛЖЕН завершиться и быть подтверждён ДО рестарта прод-контейнера, либо merge выносится в шаг, переживающий рестарт (как requeue_running_jobs, но для merge-в-main).
4. **Срез ветки от main делает целостность main критичной.** Если main отстаёт — каждая новая задача наследует дыру. main = единственный источник для новых веток, его рассинхрон с прод = накопительная потеря.
5. **Метод диагностики (сохранить как runbook):** при подозрении на рассинхрон — (a) Gitea API PR list merged-флаги, (b) md5 prod-файлов vs `git show origin/main:<file>`, (c) merge-base ветки vs main, (d) таймлайн деплой-логов. Эти 4 проверки однозначно локализуют фантом.
## Действия
- Восстановление main: интеграционная ветка `integ/restore-main-2026-06-08` — последовательный merge 022→059→066→068 (docs union-resolved, reconciler-конфликт 066⊕068 разрешён: каркас 068 livelock-fix + триггер to_analyse 066), полный pytest, затем merge в main + передеплой.
- Заведён критбаг ORCH-071: «фантомный merge — self-deploy без верификации merge в main» (root-fix: post-deploy verify + done-гейт по PR.merged + merge до рестарта).
- ORCH-070 (Confirm Deploy trigger) частично ДУБЛИРУЕТ ORCH-059 (handle_confirm_deploy уже написан в 059) — после долива 059 пересмотреть scope 070 (остаётся только display-слой статусов Monitoring after Deploy).
## Связанные
- ORCH-065 (последний честный merge; подозрение на регресс его merge-механики)
- ORCH-066/068 (потерянный код), ORCH-059 (Confirm Deploy trigger, тоже потерян)
- Урок 2026-06-08 confirm-deploy-deadtrigger (симптом того же корня)

View File

@@ -0,0 +1,7 @@
# Business Request: [★ высокий] Security-гейт: secret-scanning + аудит зависимостей перед мержем
Work Item ID: ORCH-022
## Description
TBD

View File

@@ -0,0 +1,150 @@
# 01 — BRD: Security-гейт (secret-scanning + аудит зависимостей перед мержем)
Work Item: **ORCH-022**
Приоритет: **★ высокий**
Источник: предложение Стрим, одобрено Славой (2026-06-04).
Стадия: analysis.
---
## 1. Бизнес-проблема
Оркестратор — автономная мульти-агентная система: агенты (`developer`) пишут код
**без человека-фильтра по умолчанию**. Перед мержем в `main` сейчас нет проверки на:
- **утёкший секрет** — закоммиченный API-ключ / токен / пароль / приватный ключ;
- **дырявую зависимость** — пакет с известной CVE;
- (опционально) **базовую уязвимость кода** — типовой SAST-паттерн.
Для автономной системы это критично: ошибку, которую в обычной команде «выловили бы
глазами на ревью», здесь поймать некому. Утёкший в `git`-историю ключ или уязвимая
зависимость может уехать в прод и обслуживать **все** проекты (общий инстанс,
self-hosting).
### Прецеденты / связки
- **PR #18** (`check_ci_green`: красный CI → возврат на `development`) — задаёт целевой
паттерн поведения красного гейта. Security-гейт должен вести себя так же.
- **Управление секретами** (CLAUDE.md §8): секреты живут только в `.env`/`.env.staging`
на хосте, канон — `.env.example`. Гейт — это автоматический страж этого правила.
---
## 2. Цель
Ввести **security-гейт перед слиянием ветки задачи в `main`**, который детерминированно
(без LLM) проверяет diff/ветку на секреты и уязвимые зависимости и **блокирует
продвижение** при нарушении порогов: красный security-гейт → **возврат на `development`**
(developer-retry, как красный CI / merge-gate), задача **не уезжает в прод**.
### Бизнес-ценность
- Структурно невозможно «тихо» влить секрет или известную CVE в прод автономной системы.
- Самоприменение правила CLAUDE.md §8 (секреты не в гит) без участия человека.
- Расширяет уже выстроенную линию автономных страховок (CI-гейт, merge-gate ORCH-043,
staging-провенанс ORCH-058, post-deploy ORCH-021).
---
## 3. Объём (Scope)
### 3.1 В объёме (v1) — **предположение по умолчанию (A1)**
1. **Secret-scanning** — обязательный минимум гейта. Поиск закоммиченных секретов
в ветке задачи / её diff относительно `main`.
2. **Dependency audit** — аудит зависимостей проекта на известные CVE.
3. **Машиночитаемый артефакт-вердикт** security-гейта (YAML-frontmatter — канон гейтов).
4. **Поведение красного гейта** = откат на `development` + developer-retry (cap
`MAX_DEVELOPER_RETRIES = 3`), наблюдаемость (Telegram + Plane-коммент).
5. **Условный раскат** (kill-switch + scope репозиториев), **never-raise**,
self-hosting (`orchestrator`) — первым.
### 3.2 Вне объёма (v1) — **предположение (A2), отдельные WI**
- **SAST (semgrep)** — вынесен в follow-up WI: шумнее, требует policy-тюнинга правил;
гейт проектируется с точкой расширения под него, но в v1 не включается.
- **Полноценный мульти-стек** (JS/npm, Android) — см. A3 ниже; в v1 целевой стек —
Python (сам оркестратор). Связь с ORCH-9/15 фиксируется как зависимость на будущее.
- Ретроспективное сканирование уже существующей истории `main` (гейт смотрит вперёд —
ветку перед мержем, не чистит прошлое).
- Управление аллоулистом ложных срабатываний через UI/Plane (в v1 — файл в репозитории).
### 3.3 Зафиксированные предположения по умолчанию
> ⚠️ Интерактивный опрос Owner на стадии анализа не дал ответа; ниже —
> **дефолты по конвенциям проекта**. Любой из них Owner/архитектор может переопределить
> (для A4 предусмотрены конфиг-флаги порогов).
- **A1 (объём сканеров v1):** secret-scanning + dependency-audit. SAST отложен.
- **A2 (SAST):** отложен в отдельный WI; гейт оставляет точку расширения.
- **A3 (стек):** **Python-only сначала**, реально только для self-hosting
(`is_self_hosting_repo` / scope-CSV), как ORCH-35/43/58. Прочие репо — no-op pass.
Мульти-стек (детект стека по репо) — отдельный WI.
- **A4 (пороги):** **секреты — всегда блок**; **зависимости — блок на HIGH/CRITICAL,
warning на MEDIUM/LOW**. Пороги вынесены в конфиг (переопределяемы без редеплоя кода).
---
## 4. Заинтересованные стороны
| Роль | Интерес |
|------|---------|
| Owner (Слава) | Прод-безопасность автономного конвейера; контроль порогов и раската. |
| Стрим | Инициатор; снижение риска утечки/уязвимости в автономном режиме. |
| Агент `developer` | Получает понятную причину красного гейта → быстрый фикс. |
| Агент `reviewer` | Гейт снимает с него непосильную задачу «глазами ловить ключи». |
| Все проекты на инстансе | Общий прод не должен получить секрет/CVE через одну задачу. |
---
## 5. Бизнес-требования
| ID | Требование | Приоритет |
|----|-----------|-----------|
| BR-1 | Перед слиянием ветки задачи в `main` обязателен security-гейт (секреты + аудит зависимостей). | MUST |
| BR-2 | Найден секрет (порог A4) → гейт **красный** → откат на `development`, в прод не уходит. | MUST |
| BR-3 | Уязвимость зависимости уровня блокировки (порог A4) → гейт **красный** → откат на `development`. | MUST |
| BR-4 | Уязвимость ниже порога блокировки → **warning**, продвижение не блокируется, но фиксируется в артефакте. | MUST |
| BR-5 | Красный гейт ведёт себя как красный CI / merge-gate: откат на `development` + developer-retry (cap 3), затем эскалация (Telegram + Plane Blocked). | MUST |
| BR-6 | Вердикт гейта — **машиночитаемый** (YAML-frontmatter артефакта), читается гейтом ТОЛЬКО из frontmatter (канон проекта), не из прозы. | MUST |
| BR-7 | Гейт **детерминированный, без LLM** в критическом пути (как merge-gate / image-freshness). | MUST |
| BR-8 | Гейт **never-raise**: внутренняя ошибка не роняет `advance_stage` и не вешает конвейер всех проектов. | MUST |
| BR-9 | Условный раскат: глобальный kill-switch + scope-CSV репозиториев; пусто → реально только self-hosting (`orchestrator`), прочие репо — no-op pass. | MUST |
| BR-10 | Пороги блокировки конфигурируемы (env-флаги, без редеплоя кода). | SHOULD |
| BR-11 | Наблюдаемость: причина блокировки видна (Telegram + Plane-коммент + артефакт); проход — без шума. | MUST |
| BR-12 | Документация (CLAUDE.md «Артефакты задачи», `docs/architecture/README.md` таблица гейтов, CHANGELOG, ADR) обновлена в том же PR. | MUST |
| BR-13 | Аллоулист ложных срабатываний (заведомо-безопасные совпадения, напр. в `.env.example`, фикстуры тестов) поддерживается версионируемым файлом в репозитории. | SHOULD |
| BR-14 | Точка расширения под SAST и мульти-стек заложена, но в v1 не активна (A2/A3). | SHOULD |
---
## 6. Ограничения и риски (бизнес-уровень)
- **Self-hosting:** гейт исполняется внутри инстанса, который правит сам себя. Запрет на
рестарт/падение прод-контейнера в рамках задачи (CLAUDE.md §self-hosting) сохраняется —
гейт ничего не деплоит и не рестартит, только читает/сканирует.
- **Ложные срабатывания** (false positives) могут зациклить откат `→ development`
(прецедент ORCH-061 со staging-петлёй). Митигировано: cap retry=3 + аллоулист (BR-13)
+ конфигурируемые пороги (BR-10) + kill-switch (BR-9).
- **Внешние БД уязвимостей** (CVE-фиды) — сетевая зависимость; недоступность фида не
должна давать ложный красный (см. AC: degrade-поведение при недоступности фида —
решение порога «fail-open vs fail-closed для аудита» закрепляется в acceptance + ADR).
- **Стоимость/время** сканирования добавляется к каждому прогону задачи — должно быть
ограничено таймаутом (как merge-retest).
---
## 7. Критерий успеха (бизнес)
Ветка с подсаженным тестовым секретом и/или зависимостью с известной CRITICAL-CVE
**не может** дойти до `main`/прода: гейт краснеет, задача откатывается на `development`
с понятной причиной. Чистая ветка проходит гейт без задержек и без шума. Для не-self
репозиториев конвейер не меняется (no-op). Прод-контейнер не рестартится гейтом.
---
## 8. Открытые вопросы (для архитектора / Owner)
1. **Размещение гейта** (решение архитектора): (а) на стадии `review`, либо (б) отдельный
под-гейт перед мержем на ребре `deploy-staging → deploy` (где уже живёт merge-gate
ORCH-043 / image-freshness ORCH-058). Требование BRD — «перед слиянием в `main`»;
обе опции его удовлетворяют. См. 02-trz §4.
2. **Где запускается сканер**: новый job в `.gitea/workflows/ci.yml` (тогда вердикт может
течь через существующий `check_ci_green`) **или** отдельный QG-чек/под-гейт в `src/qg`.
Решение — архитектор (02-trz фиксирует требования к обоим путям).
3. **Аудит зависимостей при недоступном CVE-фиде:** fail-open (warning) или fail-closed
(блок)? Дефолт-предложение — **fail-open с громким warning** (не плодить ложные
завороты), закрепить в ADR.
4. **Выбор конкретных инструментов** (gitleaks vs trufflehog; pip-audit vs trivy) —
технологическое решение архитектора; BRD фиксирует только функцию.

View File

@@ -0,0 +1,175 @@
# 02 — ТЗ: Security-гейт (secret-scanning + dependency audit)
Work Item: **ORCH-022** · Стадия: analysis · См. `01-brd.md`, `03-acceptance-criteria.md`.
> **Граница ответственности аналитика.** Ниже — *функциональные требования и точки
> касания* кода. Выбор размещения гейта в пайплайне, конкретных инструментов и схемы
> модулей — **решение архитектора** (см. §4 и `01-brd.md` §8). ТЗ фиксирует требования к
> любому из допустимых вариантов и инварианты, которые нельзя нарушать.
---
## 1. Контекст кода (как есть)
- **Стадии:** `src/stages.py::STAGE_TRANSITIONS` — линейный конвейер
`… review → testing → deploy-staging → deploy → done`. Фактический merge ветки в
`main` делает агент `deployer` **в начале стадии `deploy`** (CLAUDE/README).
- **Quality Gates:** `src/qg/checks.py` — реестр `QG_CHECKS` (имя → функция), сигнатуры
диспетчеризуются в `src/stage_engine.py::_run_qg`.
- **Существующий паттерн «красный гейт → возврат developer»:**
`check_ci_green` (PR #18) и rollback-ветки в
`stage_engine._handle_qg_failure_rollbacks` (откат на `development`, developer-retry,
cap `MAX_DEVELOPER_RETRIES = 3`, затем `set_issue_blocked` + Telegram).
- **Эталонный паттерн детерминированного под-гейта на ребре** (без LLM, never-raise,
условный раскат, откат на `development`):
- merge-gate **ORCH-043**`src/merge_gate.py` + `check_branch_mergeable` +
`stage_engine._handle_merge_gate` (ребро `deploy-staging → deploy`);
- image-freshness **ORCH-058**`src/image_freshness.py` + `_check_staging_image_fresh`
+ `stage_engine._handle_image_freshness` (то же ребро).
Оба: leaf-модуль с чистой логикой (never-raise) + тонкая обёртка в `QG_CHECKS` +
врезка-обработчик в `advance_stage`, kill-switch `*_enabled` + scope `*_repos`,
реально только для self-hosting при пустом scope.
- **CI:** `.gitea/workflows/ci.yml` — один job `test` (pytest) на `self-hosted` раннере,
push в `feature/**` и PR в `main`. `check_ci_green` читает комбинированный статус
коммита из Gitea API.
- **Артефакты задачи** нумерованы до `16-post-deploy-log.md`.
- **Зависимости Python:** `requirements.txt` (корень репо).
---
## 2. Функциональные требования к реализации
### FR-1. Secret-scanning ветки перед мержем
- Сканировать ветку задачи / её diff относительно `origin/main` на секреты
(ключи, токены, пароли, приватные ключи).
- **Любой** подтверждённый секрет (не из аллоулиста) → вердикт **FAIL** (порог A4: секреты
всегда блокируют).
- Инструмент (gitleaks / trufflehog) — выбор архитектора. Должен запускаться offline-/
детерминированно (без LLM) и иметь конфиг правил/аллоулиста в репозитории.
### FR-2. Dependency audit
- Аудит зависимостей целевого стека на известные CVE. Для Python — манифест
`requirements.txt` (инструмент pip-audit / trivy — выбор архитектора).
- Классификация по severity. **Порог блокировки (A4, конфигурируемо BR-10):**
- `CRITICAL`, `HIGH` → вклад в **FAIL**;
- `MEDIUM`, `LOW`**warning** (фиксируется в артефакте, не блокирует).
- Недоступность CVE-фида: degrade-поведение по решению ADR (дефолт-предложение —
fail-open + громкий warning, чтобы не плодить ложные завороты). Поведение должно быть
детерминированным и протестированным.
### FR-3. Машиночитаемый артефакт-вердикт
- Гейт порождает артефакт security-отчёта с **YAML-frontmatter**, напр.:
```
---
security_status: PASS # PASS | FAIL
secrets_found: 0
deps_blocking: 0 # число уязвимостей уровня блокировки
deps_warning: 2
---
```
Имя артефакта — предложение: **`17-security-report.md`** (следующий свободный номер;
финализирует архитектор). Тело — человекочитаемый список находок.
- Вердикт читается гейтом **ТОЛЬКО из frontmatter** (канон проекта: «машинные вердикты —
строго YAML-frontmatter, никогда проза»), по образцу `_parse_deploy_status` /
`_parse_staging_status` / `check_reviewer_verdict`. Negative-токен (FAIL) авторитетен.
- Отсутствие/битый frontmatter → `(False, reason)` (fail-closed на чтении вердикта,
как у существующих парсеров).
### FR-4. Поведение красного гейта (откат)
- `security_status: FAIL` → откат на `development` + enqueue `developer`, по образцу
`_handle_qg_failure_rollbacks` (merge-gate-ветка — точный шаблон):
- cap `MAX_DEVELOPER_RETRIES` (3); при исчерпании — `set_issue_blocked` + Telegram-алерт;
- `task_desc` для developer несёт **дословную причину** (какие секреты/CVE), по образцу
ORCH-046 (встраивание must-fix в `task_desc`), а не только ссылку на артефакт;
- Plane-коммент + `notify_qg_failure` (наблюдаемость BR-11).
### FR-5. Условный раскат (как ORCH-35/43/58)
- Глобальный kill-switch `security_gate_enabled` (env `ORCH_SECURITY_GATE_ENABLED`,
дефолт по согласованию; рекомендуется `true` с safety-net, как у соседних фич).
- Scope `security_gate_repos` (CSV); пусто → реально только `is_self_hosting_repo(repo)`
(`orchestrator`). Прочие репо → `(True, "security-gate N/A for <repo>")` (мгновенный pass).
- Отдельные пороги-флаги (A4/BR-10): напр. `security_dep_block_severity`
(`HIGH` по умолчанию), при желании `security_secrets_block` (`true`).
### FR-6. never-raise
- Любая внутренняя ошибка гейта (сбой сканера, отсутствие бинаря, таймаут) →
`(False, "<reason>")` **без** проброса исключения в `advance_stage`. Контракт —
как у `check_branch_mergeable` (внешний + внутренний guard).
- Таймаут сканирования ограничен (по образцу `merge_retest_timeout_s`).
### FR-7. Наблюдаемость
- Блокировка → Telegram + Plane-коммент (BR-11). Проход → лог-строка, без шумных
нотификаций (по образцу merge-gate pass).
- Желательно: краткий снимок в `GET /queue` (опционально, по образцу блоков `reconcile`/
`reaper`/`post_deploy`) — на усмотрение архитектора.
---
## 3. Задействованные модули `src/` (точки касания)
| Модуль | Изменение |
|--------|-----------|
| `src/security_gate.py` (**новый leaf-модуль**) | Чистая логика гейта: запуск сканеров, классификация по severity, применение порогов/аллоулиста, формирование вердикта + парсер frontmatter. **never-raise.** По образцу `src/merge_gate.py` / `src/image_freshness.py` / `src/post_deploy.py`. |
| `src/qg/checks.py` | Новый чек `check_security_gate` (тонкая обёртка над `security_gate`, ленивый импорт во избежание циклов) + регистрация в `QG_CHECKS`. Условность (kill-switch/scope/self-hosting) — как `check_branch_mergeable` / `_check_staging_image_fresh`. |
| `src/stage_engine.py` | Врезка-обработчик `_handle_security_gate(...)` по образцу `_handle_merge_gate` / `_handle_image_freshness`: вызов в `advance_stage` на выбранном архитектором ребре; FAIL → откат на `development` (FR-4); never-raise. **`STAGE_TRANSITIONS` НЕ меняется**, если выбран вариант «под-гейт ребра». |
| `src/config.py` | Новые настройки: `security_gate_enabled`, `security_gate_repos`, `security_dep_block_severity`, `security_scan_timeout_s` (+ при необходимости пути к бинарям/конфигам сканеров). С docstring-комментариями по образцу ORCH-043/058. |
| `.gitea/workflows/ci.yml` | **Если** архитектор выберет CI-путь: новый job `security` (secret-scan + dep-audit), влияющий на комбинированный статус коммита (тогда срабатывает `check_ci_green`-паттерн PR #18). Иначе — не трогается. |
| `requirements.txt` / Dockerfile | Установка выбранных сканеров (если они Python-пакеты — в `requirements.txt`; если бинари — в Dockerfile/раннер). |
| Конфиг сканера + аллоулист | Версионируемые файлы в репозитории (напр. `.gitleaks.toml` / аллоулист) — BR-13. |
| `.openclaw/agents/developer.md` | (Если нужно) краткая инструкция developer'у про устранение security-находок при заворотах. |
> Если выбран вариант «гейт на стадии `review`» — врезка делается в соответствующую
> ветку `advance_stage`/обработчик ревью вместо ребра `deploy-staging → deploy`.
---
## 4. Размещение в пайплайне — варианты для архитектора
Требование BRD: **«перед слиянием ветки в `main`»**. Допустимы (выбор + обоснование — в ADR):
- **Вариант R (review):** security-проверка на стадии `review` (раньше отлов, дешевле
откат — задача ещё близко к development). Минус: дальше по конвейеру `main` может уйти
вперёд (но это закрывает merge-gate).
- **Вариант M (merge-edge, рекомендуемый к рассмотрению):** под-гейт на ребре
`deploy-staging → deploy`, рядом с merge-gate (ORCH-043) и image-freshness (ORCH-058) —
непосредственно перед фактическим мержем `deployer`'ом. Плюс: единое место «последней
страховки перед main», переиспользование готового паттерна врезки/отката/lease.
- **Вариант C (CI-job):** добавить job в `ci.yml`; вердикт течёт через `check_ci_green`.
Плюс: меньше нового кода в движке. Минус: пороги/severity-логика и артефакт-вердикт
сложнее выразить только статусом коммита.
ТЗ не предписывает вариант; реализация обязана сохранить инварианты §6.
---
## 5. Изменения API
- Новых HTTP-endpoint'ов **не требуется**.
- Допустимо (опционально, FR-7): расширить ответ `GET /queue` блоком `security`
(counts/last_run) — по образцу блоков `reconcile`/`reaper`/`post_deploy`. Не обязательно.
## 6. Изменения схемы БД
- **Не требуется.** Состояние гейта — артефакт-файл + (при необходимости) sentinel-файлы,
по образцу merge-lease / deploy-state / post-deploy-state. Миграций БД нет.
- Если архитектор сочтёт нужным считать security-retry отдельно от developer-retry —
предпочесть подсчёт по `jobs`/`agent_runs` (как `_developer_retry_count` /
`_merge_defer_count`), без новых колонок.
## 7. Инварианты (НЕ нарушать)
1. `STAGE_TRANSITIONS` и реестр `QG_CHECKS` остаются консистентными; при варианте
«под-гейт ребра» — `STAGE_TRANSITIONS` не меняется (триггер — то же событие стадии).
2. Машинный вердикт — только из YAML-frontmatter, не из прозы.
3. never-raise: гейт никогда не пробрасывает исключение в `advance_stage`.
4. Условность как ORCH-35/43/58: не-self репо при пустом scope не затрагиваются (no-op).
5. Гейт **не деплоит и не рестартит** прод-контейнер (self-hosting safety).
6. Откат и retry-счётчик developer не ломаются (cap=3, затем эскалация).
7. Документация (CLAUDE.md, README, CHANGELOG, ADR) обновлена в том же PR (BR-12).
## 8. Артефакты pipeline, создаваемые/обновляемые
- **Новый:** `docs/work-items/ORCH-022/17-security-report.md` (имя финализирует архитектор)
с `security_status:`-frontmatter (FR-3) — порождается гейтом per-task.
- **ADR:** `docs/work-items/ORCH-022/06-adr/ADR-001-<slug>.md` (решение: размещение,
инструменты, degrade-поведение фида, пороги). При сквозном влиянии — global ADR в
`docs/architecture/adr/`.
- **Обновить:** `CLAUDE.md` (раздел «Артефакты задачи» — добавить 17-…),
`docs/architecture/README.md` (таблица гейтов + реестр `QG_CHECKS` + новый раздел),
`CHANGELOG.md`, `.env.example` (новые `ORCH_SECURITY_*`).

View File

@@ -0,0 +1,140 @@
# 03 — Критерии приёмки: Security-гейт (ORCH-022)
Формат: каждый критерий имеет чёткое условие **PASS/FAIL**. Привязка к
`01-brd.md` (BR-*) и `02-trz.md` (FR-*).
---
## A. Secret-scanning (FR-1, BR-1/BR-2)
### AC-1 — Подсаженный секрет блокирует гейт
- **PASS:** ветка с тестовым секретом (напр. фиктивный AWS-ключ формата `AKIA…` вне
аллоулиста) → `security_status: FAIL`; гейт возвращает `(False, reason)`, причина
называет секрет/файл.
- **FAIL:** секрет не обнаружен ИЛИ гейт зелёный при наличии секрета.
### AC-2 — Чистая ветка проходит
- **PASS:** ветка без секретов → `security_status: PASS`; `secrets_found: 0`;
гейт возвращает `(True, …)`.
- **FAIL:** ложное срабатывание (FAIL на чистой ветке).
### AC-3 — Аллоулист подавляет заведомо-безопасное (BR-13)
- **PASS:** совпадение, явно занесённое в версионируемый аллоулист (напр. плейсхолдер в
`.env.example` / фикстура теста), **не** даёт FAIL.
- **FAIL:** аллоулист игнорируется и даёт ложный FAIL.
---
## B. Dependency audit (FR-2, BR-3/BR-4)
### AC-4 — CVE уровня блокировки краснит гейт
- **PASS:** зависимость с известной `CRITICAL`/`HIGH` CVE (при пороге по умолчанию
`HIGH`) → вклад в `security_status: FAIL`; `deps_blocking >= 1`.
- **FAIL:** блокирующая уязвимость не приводит к FAIL.
### AC-5 — Низкая severity = warning, не блок
- **PASS:** только `MEDIUM`/`LOW` уязвимости → `security_status: PASS`, при этом
`deps_warning >= 1` и находки перечислены в теле артефакта.
- **FAIL:** `MEDIUM`/`LOW` блокирует продвижение.
### AC-6 — Порог блокировки конфигурируем (BR-10)
- **PASS:** при `ORCH_SECURITY_DEP_BLOCK_SEVERITY=CRITICAL` та же `HIGH`-уязвимость
становится warning (не блок); при `=HIGH` — блок. Поведение детерминированно
определяется флагом.
- **FAIL:** флаг не влияет на классификацию.
### AC-7 — Degrade при недоступном CVE-фиде
- **PASS:** недоступность фида обрабатывается по решению ADR детерминированно и
протестированно (дефолт: fail-open + громкий warning, гейт не краснеет ложно).
- **FAIL:** недоступность фида даёт неконтролируемый красный/исключение.
---
## C. Вердикт и артефакт (FR-3, BR-6)
### AC-8 — Машинный вердикт только из frontmatter
- **PASS:** вердикт читается ТОЛЬКО из YAML-frontmatter `17-security-report.md`; проза с
«PASS»/«FAIL» в теле не влияет на решение. Negative-токен (FAIL) авторитетен.
- **FAIL:** вердикт извлекается из тела/прозы.
### AC-9 — Битый/отсутствующий frontmatter → fail-closed на чтении
- **PASS:** нет frontmatter / битый YAML / нет поля `security_status``(False, reason)`
(как `_parse_deploy_status`/`check_reviewer_verdict`).
- **FAIL:** битый артефакт трактуется как PASS.
### AC-10 — Артефакт создаётся с корректными полями
- **PASS:** после прогона существует `17-security-report.md` с валидным frontmatter
(`security_status`, `secrets_found`, `deps_blocking`, `deps_warning`) и телом-списком.
- **FAIL:** артефакт не создан/без машинных полей.
---
## D. Откат и retry (FR-4, BR-5)
### AC-11 — Красный гейт → откат на development + developer-retry
- **PASS:** `FAIL` → стадия задачи становится `development`, enqueue `developer`,
Plane-коммент + `notify_qg_failure`; счётчик developer-retry растёт.
- **FAIL:** при FAIL задача продвигается дальше / не откатывается.
### AC-12 — task_desc несёт дословную причину (ORCH-046-паттерн)
- **PASS:** `task_desc` для перезапущенного developer содержит конкретику находок
(какие секреты/CVE), а не только ссылку на артефакт.
- **FAIL:** developer получает только ссылку без сути.
### AC-13 — Cap retry и эскалация
- **PASS:** после `MAX_DEVELOPER_RETRIES` (3) безуспешных фиксов — `set_issue_blocked` +
Telegram-алерт; бесконечного отскока нет.
- **FAIL:** откат зацикливается без cap/эскалации.
---
## E. Условный раскат и устойчивость (FR-5/FR-6, BR-8/BR-9)
### AC-14 — Не-self репозиторий = no-op pass
- **PASS:** для repo, не входящего в scope и не self-hosting → гейт возвращает
`(True, "security-gate N/A for <repo>")` мгновенно, конвейер такого репо не меняется.
- **FAIL:** гейт реально запускается/блокирует чужой репо при пустом scope.
### AC-15 — Kill-switch отключает гейт
- **PASS:** `ORCH_SECURITY_GATE_ENABLED=false` → гейт — no-op pass (`(True, …)`),
поведение конвейера 1:1 как до ORCH-022.
- **FAIL:** при выключенном флаге гейт всё ещё блокирует.
### AC-16 — never-raise
- **PASS:** искусственный сбой (нет бинаря сканера / таймаут / исключение внутри) →
`(False, reason)` без проброса исключения; `advance_stage` не падает, конвейер других
задач/проектов не встаёт.
- **FAIL:** внутренняя ошибка пробрасывается/вешает движок.
### AC-17 — Таймаут ограничен
- **PASS:** сканирование, превысившее `ORCH_SECURITY_SCAN_TIMEOUT_S`, корректно
прерывается → детерминированный вердикт (по политике degrade), без зависания.
- **FAIL:** сканер висит без таймаута.
---
## F. Инварианты и интеграция (BR-7/BR-12, TRZ §7)
### AC-18 — STAGE_TRANSITIONS/QG_CHECKS консистентны
- **PASS:** при варианте «под-гейт ребра» `STAGE_TRANSITIONS` не изменён; новый чек
зарегистрирован в `QG_CHECKS`; `_run_qg` корректно его диспетчеризует. Все
существующие тесты гейтов/стадий зелёные.
- **FAIL:** сломан реестр/переходы/существующие тесты.
### AC-19 — Гейт не деплоит/не рестартит прод
- **PASS:** код гейта не вызывает деплой-хук/рестарт прод-контейнера; только
чтение/сканирование.
- **FAIL:** гейт инициирует рестарт/деплой.
### AC-20 — Документация обновлена в том же PR (BR-12)
- **PASS:** обновлены `CLAUDE.md` (артефакт 17-…), `docs/architecture/README.md`
(таблица гейтов + реестр QG + раздел ORCH-022), `CHANGELOG.md`, `.env.example`
(`ORCH_SECURITY_*`); заведён ADR `06-adr/ADR-001-*`.
- **FAIL:** функционал есть, документация/ADR не обновлены → reviewer обязан
REQUEST_CHANGES (CLAUDE.md §6).
### AC-21 — End-to-end на тестовой задаче
- **PASS:** прогон на self-hosting-репо: грязная ветка (секрет/CVE) → откат на
`development`; после фикса чистая ветка → гейт зелёный → конвейер идёт дальше; прод не
затронут в процессе.
- **FAIL:** любой шаг E2E не воспроизводится.

View File

@@ -0,0 +1,126 @@
work_item: ORCH-022
title: "Security-гейт: secret-scanning + dependency audit перед мержем"
notes: >
План тестов для security-гейта. Чистая логика выносится в leaf-модуль
src/security_gate.py (never-raise) — основной предмет unit-тестов (по образцу
tests для merge_gate / image_freshness / post_deploy / staging_verdict).
Интеграция врезки в advance_stage и условный раскат — integration-тесты.
Имена модулей тестов финализирует разработчик/архитектор по факту реализации.
tests:
# --- Secret-scanning (FR-1 / AC-1..AC-3) ---
- id: TC-01
type: unit
description: "Подсаженный тестовый секрет в diff -> вердикт FAIL, secrets_found>=1, причина называет находку."
module: tests/test_security_gate.py
expected: PASS
- id: TC-02
type: unit
description: "Чистая ветка без секретов -> вердикт PASS, secrets_found=0."
module: tests/test_security_gate.py
expected: PASS
- id: TC-03
type: unit
description: "Совпадение из аллоулиста (плейсхолдер .env.example / фикстура) НЕ даёт FAIL."
module: tests/test_security_gate.py
expected: PASS
# --- Dependency audit + пороги (FR-2 / AC-4..AC-7) ---
- id: TC-04
type: unit
description: "CVE уровня HIGH/CRITICAL при пороге HIGH -> вклад в FAIL, deps_blocking>=1."
module: tests/test_security_gate.py
expected: PASS
- id: TC-05
type: unit
description: "Только MEDIUM/LOW уязвимости -> PASS, deps_warning>=1, находки в теле артефакта."
module: tests/test_security_gate.py
expected: PASS
- id: TC-06
type: unit
description: "Конфиг порога: severity=CRITICAL делает HIGH-CVE warning; severity=HIGH делает её блоком."
module: tests/test_security_gate.py
expected: PASS
- id: TC-07
type: unit
description: "Недоступный CVE-фид -> детерминированный degrade по политике ADR (дефолт fail-open + warning), без исключения и без ложного FAIL."
module: tests/test_security_gate.py
expected: PASS
# --- Вердикт / парсер frontmatter (FR-3 / AC-8..AC-10) ---
- id: TC-08
type: unit
description: "Вердикт читается ТОЛЬКО из YAML-frontmatter; проза PASS/FAIL в теле не влияет; negative-токен авторитетен."
module: tests/test_security_gate.py
expected: PASS
- id: TC-09
type: unit
description: "Нет frontmatter / битый YAML / нет поля security_status -> (False, reason) (fail-closed на чтении)."
module: tests/test_security_gate.py
expected: PASS
- id: TC-10
type: unit
description: "Артефакт 17-security-report.md создаётся с валидным frontmatter (security_status, secrets_found, deps_blocking, deps_warning) и телом-списком."
module: tests/test_security_gate.py
expected: PASS
# --- never-raise / таймаут / условность (FR-5/FR-6 / AC-14..AC-17) ---
- id: TC-11
type: unit
description: "Отсутствие бинаря сканера / внутреннее исключение -> (False, reason), исключение не пробрасывается (never-raise)."
module: tests/test_security_gate.py
expected: PASS
- id: TC-12
type: unit
description: "Превышение ORCH_SECURITY_SCAN_TIMEOUT_S -> корректное прерывание и детерминированный вердикт, без зависания."
module: tests/test_security_gate.py
expected: PASS
- id: TC-13
type: unit
description: "check_security_gate: не-self репо при пустом scope -> (True, 'security-gate N/A for <repo>') мгновенно."
module: tests/test_qg_security.py
expected: PASS
- id: TC-14
type: unit
description: "check_security_gate: ORCH_SECURITY_GATE_ENABLED=false -> no-op pass (True)."
module: tests/test_qg_security.py
expected: PASS
- id: TC-15
type: unit
description: "Новый чек зарегистрирован в QG_CHECKS и корректно диспетчеризуется _run_qg."
module: tests/test_qg_security.py
expected: PASS
# --- Откат / retry в stage_engine (FR-4 / AC-11..AC-13) ---
- id: TC-16
type: integration
description: "security_status FAIL -> advance_stage откатывает на development, enqueue developer, Plane-коммент + notify_qg_failure."
module: tests/test_stage_engine_security_gate.py
expected: PASS
- id: TC-17
type: integration
description: "task_desc перезапущенного developer содержит дословную причину находок (ORCH-046-паттерн), не только ссылку."
module: tests/test_stage_engine_security_gate.py
expected: PASS
- id: TC-18
type: integration
description: "После MAX_DEVELOPER_RETRIES (3) -> set_issue_blocked + Telegram-алерт; бесконечного отскока нет."
module: tests/test_stage_engine_security_gate.py
expected: PASS
- id: TC-19
type: integration
description: "security_status PASS -> advance_stage продвигает конвейер штатно (без отката, без шумных нотификаций)."
module: tests/test_stage_engine_security_gate.py
expected: PASS
# --- Инварианты / интеграция (BR-7/BR-12 / AC-18..AC-19) ---
- id: TC-20
type: integration
description: "При варианте 'под-гейт ребра' STAGE_TRANSITIONS не изменён; существующие тесты стадий/гейтов остаются зелёными."
module: tests/test_stages.py
expected: PASS
- id: TC-21
type: integration
description: "Гейт не вызывает деплой-хук/рестарт прод-контейнера (self-hosting safety)."
module: tests/test_stage_engine_security_gate.py
expected: PASS

View File

@@ -0,0 +1,235 @@
# ADR-001: Security-гейт — secret-scanning + dependency audit перед мержем
- **Статус:** Accepted (proposed → принято архитектором ORCH-022)
- **Дата:** 2026-06-07
- **Задача:** ORCH-022
- **Связанный global ADR:** `docs/architecture/adr/adr-0012-security-gate.md`
- **Источники:** `01-brd.md` (BR-1..BR-14), `02-trz.md` (FR-1..FR-7, §4 варианты, §7 инварианты),
`03-acceptance-criteria.md` (AC-1..AC-21).
---
## Контекст
Оркестратор автономен: `developer`-агент пишет код без человека-фильтра. Перед слиянием
ветки задачи в `main` нет автоматической проверки на утёкший секрет (ключ/токен/пароль/
приватный ключ) и на уязвимую зависимость (известная CVE). Для self-hosting это особенно
опасно: один общий прод-инстанс обслуживает все проекты с общей БД — секрет или CVE,
просочившийся через одну задачу, попадает в прод всех проектов (CLAUDE.md §self-hosting, §8).
Конвейер уже содержит линию детерминированных страховок на ребре `deploy-staging → deploy`
(непосредственно перед фактическим мержем PR в `main`, который делает `deployer` в начале
стадии `deploy`):
- **merge-gate** (ORCH-043, `check_branch_mergeable`) — догон `main` + re-test + сериализация;
- **image-freshness** (ORCH-058, `check_staging_image_fresh`) — провенанс staging-образа.
Оба построены по одному паттерну: **leaf-модуль чистой логики (never-raise) + тонкая обёртка
в `QG_CHECKS` + врезка-обработчик `_handle_*` в `advance_stage`**, с условным раскатом
(`*_enabled` + `*_repos`, реально только для self-hosting при пустом scope) и откатом на
`development` с developer-retry (cap `MAX_DEVELOPER_RETRIES = 3`).
Открытые вопросы BRD §8 / TRZ §4, требующие решения архитектора:
1. Размещение гейта в пайплайне (review / merge-edge / CI-job).
2. Где запускается сканер (CI-job через `check_ci_green` / отдельный QG-чек).
3. Degrade при недоступном CVE-фиде (fail-open / fail-closed).
4. Выбор инструментов (gitleaks/trufflehog; pip-audit/trivy).
---
## Решение
### Р-1. Размещение — Вариант M (под-гейт ребра `deploy-staging → deploy`), ПЕРВЫМ среди edge-под-гейтов
Security-гейт реализуется как **детерминированный под-гейт того же ребра**
`deploy-staging → deploy`, что merge-gate и image-freshness, и исполняется **ПЕРВЫМ**
**ДО** merge-gate. `STAGE_TRANSITIONS` **не меняется** (триггер — то же событие «staging-
deployer завершился»; инвариант TRZ §7.1).
Порядок врезок в `advance_stage` (блок `current_stage == "deploy-staging"`):
```
check_staging_status (PASS, существующий QG стадии)
→ security-gate (НОВЫЙ, _handle_security_gate) ← первым
→ merge-gate (_handle_merge_gate)
→ image-freshness (_handle_image_freshness)
→ Phase A (self-deploy approve)
```
**Почему merge-edge, а не review (Вариант R):**
- BRD-требование «перед слиянием в `main`» удовлетворяют оба, но на review-стадии diff
может разойтись с тем, что реально вольётся в `main` (параллельная задача двигает `main`
вперёд между review и merge). Merge-edge — последняя точка перед фактическим мержем.
- Переиспользуется готовая машинерия отката/retry/нотификаций edge-под-гейтов
(минимальный blast-radius, инвариант TRZ §7).
**Почему ПЕРВЫМ (до merge-gate), а не после image-freshness:**
- **Дёшево фейлить.** merge-gate (rebase + re-test, минуты) и image-freshness (docker
rebuild, до 1200с) — дорогие. Нет смысла гонять их на ветке с секретом/CVE.
- **Корректность для секретов.** Секрет живёт в собственных коммитах ветки;
rebase онто `main` его не добавляет и не убирает → скан диапазона `origin/main..HEAD`
до rebase ловит ровно те коммиты, что попадут в `main`.
- **Анти-петля для зависимостей.** Аудит ветки **до** rebase оценивает то, что вносит
ИМЕННО эта задача (её `requirements.txt`/diff), а не уязвимость, которую притащил в
ветку обновившийся `main`. Аудит после rebase «обвинял» бы задачу в чужой (main'овой)
CVE → ложный откат `→ development` → петля (прецедент ORCH-061). Скан до rebase этого
избегает.
- **Проще, чем image-freshness.** Гейт исполняется ДО захвата merge-lease → при FAIL
**lease освобождать не нужно** (в отличие от `_handle_image_freshness`). Чистый откат.
**Почему не CI-job (Вариант C):** пороги severity, warning-vs-block, аллоулист и
машиночитаемый артефакт-вердикт плохо выражаются одним статусом коммита Gitea; путь
коуплится с CI-раннером. Отклонено для v1; оставлено как точка расширения (BR-14).
### Р-2. Инструменты
- **Secret-scanning — `gitleaks`.** Полностью **offline** (без сетевого фида → гарантия
«секрет всегда блокирует» не зависит от сети, BR-2), один статический бинарь,
детерминированный, конфиг + аллоулист в репо (`.gitleaks.toml`, BR-13), поддержка
`--log-opts="origin/main..HEAD"` (скан диапазона), JSON-отчёт, exit-code контракт
(0 = чисто, 1 = найдены секреты, ≥2 = ошибка инструмента). Бинарь устанавливается в
`Dockerfile` (Go-бинарь, не pip-пакет) — см. `07-infra-requirements.md`.
- **Dependency audit — `pip-audit`.** Python-native (v1-стек — сам оркестратор, Python),
читает `requirements.txt`, источник advisory — OSV/PyPI, JSON-выход, ставится через
`requirements.txt`. trivy/trufflehog отклонены как тяжелее/контейнер-ориентированные для
v1-цели «Python-only» (A3).
Конкретные инструменты — деталь реализации; контракт гейта (вход: repo/branch/wi,
выход: `(bool, reason)` + артефакт) от них не зависит, заменяемы за leaf-модулем.
### Р-3. Degrade при недоступном CVE-фиде — **fail-open + громкий warning** (дефолт)
`pip-audit` требует сети (OSV/PyPI advisory DB). Недоступность фида **по умолчанию**:
- **fail-open**: dep-audit не даёт FAIL по причине недоступности фида (иначе — ложные
откаты `→ development` → петля при сетевых проблемах прод-инстанса, прецедент ORCH-061);
- **громко**: в артефакте `deps_audit_degraded: true`, лог `logger.warning`, Telegram-алерт.
- **Секреты не деградируют:** gitleaks offline → гарантия BR-2 безусловна даже при
отсутствии сети. Деградирует ТОЛЬКО dep-audit.
- **Конфигурируемо:** флаг `security_dep_audit_fail_closed` (дефолт `false`) позволяет
Owner'у переключить на fail-closed (недоступность фида → FAIL) без редеплоя кода.
Это разделяет две гарантии: «нет секрета в прод» — **безусловная**; «нет известной CVE» —
**best-effort при доступности фида**. Закреплено в acceptance (AC-7).
### Р-4. Пороги классификации (A4, BR-10)
- **Секреты:** любой подтверждённый (не из аллоулиста) секрет → **вклад в FAIL** (всегда
блок; флаг `security_secrets_block`, дефолт `true`).
- **Зависимости:** severity ≥ `security_dep_block_severity` (дефолт `HIGH`) → **вклад в
FAIL** (`deps_blocking`); ниже порога (`MEDIUM`/`LOW`) → **warning** (`deps_warning`,
не блокирует, фиксируется в теле).
- **Severity = UNKNOWN** (OSV/advisory без CVSS — частый случай pip-audit): трактуется как
**ниже порога → warning**, никогда не авто-блок (анти-петля). Логируется.
### Р-5. Артефакт и вердикт (FR-3, BR-6, канон проекта)
- Новый артефакт **`17-security-report.md`** (следующий свободный номер; финализировано).
- YAML-frontmatter:
```
---
security_status: PASS # PASS | FAIL
secrets_found: 0
deps_blocking: 0
deps_warning: 2
deps_audit_degraded: false
---
```
Тело — человекочитаемый список находок (секреты: файл/правило/маскированное совпадение;
CVE: пакет/версия/идентификатор/severity).
- **Единый источник истины:** гейт вычисляет находки → пишет артефакт → **читает вердикт
обратно через `parse_security_status(content)`** (frontmatter-парсер по образцу
`_parse_deploy_status`/`_parse_staging_status`) → возвращает этот вердикт. Так возвращаемый
`(bool, reason)` гарантированно == frontmatter артефакта (канон «машинный вердикт — только
из YAML-frontmatter, никогда из прозы», AC-8). Negative-токен (`FAIL`) авторитетен.
- Битый/отсутствующий frontmatter / нет поля `security_status` → `(False, reason)` —
fail-closed на чтении вердикта (AC-9).
### Р-6. Поведение красного гейта (FR-4, BR-5)
`security_status: FAIL` → врезка `_handle_security_gate` (по образцу
`_handle_image_freshness`, но БЕЗ работы с lease — гейт до его захвата):
- `update_task_stage(development)` + `enqueue_job("developer", …)`;
- retry-счётчик — **существующий** `_developer_retry_count` (общий с merge/freshness;
без новой колонки, TRZ §6); cap `MAX_DEVELOPER_RETRIES = 3` → при исчерпании
`set_issue_blocked` + Telegram;
- `task_desc` несёт **дословную причину** (какие секреты/файлы, какие пакеты/CVE/severity)
по образцу ORCH-046 — не только ссылку на артефакт (AC-12);
- `notify_qg_failure` + Plane-коммент (наблюдаемость BR-11).
PASS → `return False` из обработчика → `advance_stage` идёт к merge-gate (тишина, без шума).
### Р-7. Условный раскат и устойчивость (FR-5/FR-6)
- `check_security_gate(repo, work_item_id, branch)` в `QG_CHECKS`; обёртка делегирует в
`src/security_gate.py` (ленивый импорт во избежание цикла — по образцу
`_check_staging_image_fresh`).
- Условность: `security_gate_enabled=False` → `(True, "security-gate disabled")`;
`security_gate_repos` (CSV) пусто → реально только `is_self_hosting_repo` → прочие репо
`(True, "security-gate N/A for <repo>")` (AC-14/AC-15).
- **never-raise** (двойной guard как `check_branch_mergeable`): любая ошибка (нет бинаря,
таймаут, исключение) → `(False, reason)`, исключение не уходит в `advance_stage` (AC-16).
- Таймаут сканирования `security_scan_timeout_s` (дефолт 300) на каждый внешний вызов
(`subprocess … timeout=`) — превышение → детерминированный degrade-вердикт (AC-17).
### Р-8. Self-hosting safety (инвариант TRZ §7.5, AC-19)
Гейт **только читает/сканирует** (git, gitleaks, pip-audit, запись артефакта). Не вызывает
деплой-хук, не рестартит и не трогает прод-контейнер (8500/8501).
---
## Точки касания (для developer; reviewer проверяет полноту — AC-20)
| Модуль | Изменение |
|--------|-----------|
| `src/security_gate.py` (**новый leaf**) | `security_gate_applies`, `scan_secrets`, `audit_dependencies`, `classify_severity`, `compute_verdict`, `write_security_report`, `parse_security_status`, `check_security_gate`. never-raise, fail-closed на чтении вердикта. По образцу `image_freshness.py`. |
| `src/qg/checks.py` | `check_security_gate` (тонкая обёртка, ленивый импорт) + регистрация в `QG_CHECKS`. |
| `src/stage_engine.py` | `_handle_security_gate(...)` + врезка ПЕРВОЙ в блоке `current_stage == "deploy-staging"` (до `_handle_merge_gate`). FAIL → откат на `development`. never-raise. **`STAGE_TRANSITIONS` НЕ меняется.** |
| `src/config.py` | `security_gate_enabled` (True), `security_gate_repos` (""), `security_dep_block_severity` ("HIGH"), `security_scan_timeout_s` (300), `security_dep_audit_fail_closed` (False), `security_secrets_block` (True) — с docstring по образцу ORCH-043/058. |
| `Dockerfile` | Установка `gitleaks` (release-бинарь). |
| `requirements.txt` | `pip-audit`. |
| `.gitleaks.toml` (**новый, корень репо**) | Конфиг правил + аллоулист (`.env.example`-плейсхолдеры, тест-фикстуры) — BR-13. |
| `.openclaw/agents/developer.md` | (Опц.) краткая инструкция про устранение security-находок при заворотах. |
| `tests/` | `test_security_gate.py`, `test_qg_security.py`, `test_stage_engine_security_gate.py` (см. `04-test-plan.yaml`). |
| **Документация** | `CLAUDE.md` (артефакт 17-…), `docs/architecture/README.md` (таблица гейтов + реестр QG + раздел), `CHANGELOG.md`, `.env.example` (`ORCH_SECURITY_*`), global `adr-0012`. |
---
## Альтернативы (отклонены)
- **Вариант R (review-стадия):** раньше/дешевле, но diff может разойтись с тем, что
вольётся в `main`; merge-edge уже закрывает «последнюю страховку».
- **Вариант C (CI-job через `check_ci_green`):** пороги/severity/аллоулист/артефакт плохо
выражаются статусом коммита; коуплинг с CI-раннером. → точка расширения BR-14.
- **fail-closed dep-audit по умолчанию:** ложные откаты при сетевых сбоях → петля. →
только опционально через флаг.
- **Аудит после rebase (как анкер image-freshness):** обвиняет задачу в CVE из `main` →
петля. → скан ветки ДО merge-gate.
- **Новая стадия `security`:** «пустая» стадия без агента не имеет триггера (как
отклонено в ORCH-043). → под-гейт ребра.
- **Новая колонка retry в БД:** не нужна — переиспользуем `_developer_retry_count`.
---
## Последствия
**Плюсы.** Структурно невозможно тихо влить секрет (безусловно) или известную CVE
(best-effort) в `main`/прод автономной системы. Самоприменение CLAUDE.md §8. Минимальный
blast-radius: `STAGE_TRANSITIONS`/схема БД не меняются, переиспользован готовый паттерн.
**Минусы / плата.** Ещё один «скрытый» под-гейт ребра (нет в `STAGE_TRANSITIONS`).
Добавлены внешние инструменты (gitleaks-бинарь в образ, pip-audit в зависимости). Время
сканирования добавляется к каждому прогону (ограничено таймаутом). Dep-audit best-effort
при сетевых сбоях (осознанный компромисс против петли). v1 — Python-only (A3); мульти-стек
и SAST — follow-up WI (BR-14).
**Раскат.** Сквозное изменение конвейера (новый QG + новый edge-под-гейт) → лейбл
`arch:major-change`. Прод-деплой ORCH-022 — строго через staging-гейт (8501), без рестарта
прод-контейнера в рамках задачи (self-hosting safety).
## Связи
adr-0006 (merge-gate — паттерн edge-под-гейта/отката), adr-0008 (image-freshness —
условность/never-raise/fail-closed), adr-0003 (`is_self_hosting_repo` — образец условности),
adr-0009/ORCH-061 (анти-петля ложных FAIL), ORCH-046 (дословный reason в `task_desc`),
ORCH-9/15 (мульти-стек — будущая зависимость).

View File

@@ -0,0 +1,56 @@
# 07 — Инфраструктурные требования: Security-гейт (ORCH-022)
См. `06-adr/ADR-001-security-gate.md` (Р-2, Р-3, Р-8). Топология не меняется (один сервер
mva154, Docker Compose). Новые требования — только инструменты сканирования и сетевой доступ
к CVE-фиду.
## I-1. Бинарь `gitleaks` в образе
- **Что:** статический Go-бинарь `gitleaks` (secret-scanning), устанавливается в `Dockerfile`
(НЕ pip-пакет). Зафиксировать версию (pinned release) для детерминизма.
- **Почему в образе, а не на хосте:** гейт исполняется внутри контейнера оркестратора
(`advance_stage`); сканируется per-task worktree, смонтированный в контейнер.
- **Оффлайн:** gitleaks не требует сети (правила локальны) → гарантия «секрет всегда
блокирует» (BR-2) не зависит от доступности интернета.
- **Контракт exit-кодов:** 0 = чисто, 1 = найдены секреты, ≥2 = ошибка инструмента
(≥2 → never-raise degrade-вердикт гейта).
## I-2. `pip-audit` в зависимостях
- **Что:** Python-пакет `pip-audit` (dependency audit), добавляется в `requirements.txt`
(pinned-версия).
- **Источник advisory:** OSV / PyPI advisory DB — **требует сетевого доступа** (исходящий
HTTPS к OSV/PyPI).
- **Цель v1:** аудит `requirements.txt` корня репо (Python-стек, A3). Мульти-стек — follow-up.
## I-3. Сетевой доступ к CVE-фиду (degrade-политика)
- **Требование:** исходящий HTTPS из прод-контейнера к OSV/PyPI advisory.
- **При недоступности (Р-3):** **fail-open + громкий warning** по умолчанию — dep-audit не
краснит гейт из-за сетевого сбоя (анти-петля ORCH-061); фиксируется
`deps_audit_degraded: true` + Telegram + лог. Флаг `security_dep_audit_fail_closed`
(дефолт `false`) — для перевода в строгий режим без редеплоя кода.
- **Секреты не зависят от сети** (I-1) — критическая гарантия безусловна.
## I-4. Конфиг-файлы в репозитории (версионируемые, BR-13)
- `.gitleaks.toml` (корень репо): правила + аллоулист заведомо-безопасных совпадений
(плейсхолдеры `.env.example`, тест-фикстуры). Версионируется, ревьюится как код.
## I-5. Env-флаги (`.env.example` + хост `.env`/`.env.staging`)
| Переменная | Дефолт | Назначение |
|------------|--------|-----------|
| `ORCH_SECURITY_GATE_ENABLED` | `true` | глобальный kill-switch |
| `ORCH_SECURITY_GATE_REPOS` | `` (пусто) | CSV scope; пусто → только self-hosting |
| `ORCH_SECURITY_DEP_BLOCK_SEVERITY` | `HIGH` | порог блокировки зависимостей |
| `ORCH_SECURITY_SCAN_TIMEOUT_S` | `300` | таймаут каждого внешнего вызова сканера |
| `ORCH_SECURITY_DEP_AUDIT_FAIL_CLOSED` | `false` | строгий режим при недоступном фиде |
| `ORCH_SECURITY_SECRETS_BLOCK` | `true` | секреты блокируют (всегда по дефолту) |
Секреты-значения в гит НЕ коммитятся (CLAUDE.md §8) — только дефолты в `.env.example`.
## I-6. Ресурсы и тайминги
- Время сканирования добавляется к каждому прогону задачи на ребре `deploy-staging → deploy`,
ограничено `ORCH_SECURITY_SCAN_TIMEOUT_S` (по образцу `merge_retest_timeout_s`).
- Гейт исполняется ДО merge-gate/image-freshness (дёшево фейлить до дорогих rebase/rebuild).
## I-7. Self-hosting safety (инвариант)
Гейт **только читает/сканирует** (git, gitleaks, pip-audit, запись артефакта). Не вызывает
деплой-хук, не рестартит/не трогает прод-контейнер (8500/8501). Прод-деплой ORCH-022 — строго
через staging-гейт (8501).

View File

@@ -0,0 +1,26 @@
# 08 — Требования к схеме БД: Security-гейт (ORCH-022)
## Решение: схема БД НЕ меняется
Миграций нет. Обоснование (соответствует TRZ §6 и паттерну edge-под-гейтов ORCH-043/058):
1. **Вердикт гейта — артефакт-файл** `17-security-report.md` (YAML-frontmatter), как
`14-deploy-log.md` / `15-staging-log.md`. Не хранится в БД.
2. **Состояние/идемпотентность** — детерминированная пересборка вердикта при каждом тике
(гейт чистый, без долгоживущего состояния между прогонами); sentinel-файлы НЕ требуются
(в отличие от deploy-state/post-deploy-state — там асинхронный self-restart).
3. **Retry-счётчик** — переиспользуется существующий `_developer_retry_count(task_id)`
(подсчёт по `jobs`/`agent_runs`), общий с merge-gate/image-freshness. **Новой колонки
`security_retry` НЕ вводим** (TRZ §6: предпочесть подсчёт по `jobs`/`agent_runs`). Это
корректно: security-FAIL, как merge/freshness-FAIL, откатывает на `development` и
запускает developer — он и есть единица retry; общий cap=3 защищает от петли.
## Используемые существующие таблицы (без изменений)
- `tasks` — стадия задачи (`update_task_stage` при откате на `development`).
- `jobs` — enqueue `developer` при FAIL; основа `_developer_retry_count`.
- `agent_runs` — usage/duration; основа подсчёта retry.
## Что НЕ делаем
- Не добавляем таблицу findings/CVE-журнала (история находок — в артефактах per-task; петля
уроков ORCH-8 читает артефакт).
- Не добавляем колонок в `tasks`/`jobs`.

View File

@@ -0,0 +1,16 @@
# 10 — Технические риски: Security-гейт (ORCH-022)
| ID | Риск | Вероятность / Влияние | Митигация (заложена в ADR-001) |
|----|------|----------------------|-------------------------------|
| R-1 | **Ложные срабатывания → петля отката** `→ development` (прецедент ORCH-061 staging-loop). | Средн. / Выс. | Аллоулист `.gitleaks.toml` (BR-13); cap `MAX_DEVELOPER_RETRIES=3` → эскалация (`set_issue_blocked`+Telegram); конфигурируемый порог severity; kill-switch; UNKNOWN-severity → warning, не блок. |
| R-2 | **Недоступность CVE-фида** даёт ложный красный/исключение. | Средн. / Выс. | fail-open + громкий warning по умолчанию (Р-3); `deps_audit_degraded:true`; флаг `security_dep_audit_fail_closed` для строгого режима. Секреты offline → не затронуты. |
| R-3 | **Скан вешает worker-слот** (зависший gitleaks/pip-audit) → стоит конвейер всех проектов (общий инстанс, `max_concurrency`). | Низк. / Выс. | `security_scan_timeout_s` (300) на каждый внешний вызов; never-raise degrade-вердикт; гейт ПЕРВЫМ на ребре (фейлит до дорогих rebase/rebuild). |
| R-4 | **Исключение гейта роняет `advance_stage`** → встаёт движок. | Низк. / Выс. | Двойной never-raise guard (внешний+внутренний) как `check_branch_mergeable`; AC-16/TC-11. |
| R-5 | **Скан после rebase обвиняет задачу в CVE из `main`** → петля. | — (устранён дизайном) | Гейт исполняется ДО merge-gate (скан ветки до rebase); Р-1. |
| R-6 | **Отсутствие бинаря `gitleaks` в образе** (забыт в Dockerfile) → гейт всегда degrade. | Низк. / Средн. | Установка в Dockerfile (I-1), pinned-версия; TC-11 (нет бинаря → `(False,reason)`, never-raise); проверяется на staging (8501) до прода. |
| R-7 | **pip-audit без severity (UNKNOWN)** → либо ложный блок, либо пропуск. | Средн. / Средн. | UNKNOWN → warning (не блок), логируется; осознанный анти-петля компромисс; ужесточение — follow-up. |
| R-8 | **Self-hosting: гейт трогает прод** (рестарт/деплой). | — (запрещено дизайном) | Гейт только читает/сканирует; AC-19/TC-21; прод-деплой ORCH-022 — через staging-гейт. |
| R-9 | **Drift вердикта vs артефакта** (возврат ≠ frontmatter). | Низк. / Средн. | Единый источник: гейт пишет артефакт → читает обратно через `parse_security_status` → возвращает (Р-5); AC-8. |
| R-10 | **Регресс существующих гейтов/стадий** (сломан `QG_CHECKS`/`STAGE_TRANSITIONS`). | Низк. / Выс. | `STAGE_TRANSITIONS` не меняется; новый чек — аддитивно в реестр; полный прогон `tests/` (TC-20); staging-гейт перед прод. |
| R-11 | **v1 Python-only** — секреты/CVE в не-Python стеке (JS/Android) не ловятся. | — (вне scope v1, A3) | Условность scope; точка расширения мульти-стек/SAST (BR-14); зависимость ORCH-9/15 зафиксирована. |
| R-12 | **Стоимость времени** на каждом прогоне задачи. | Низк. / Низк. | Таймаут; гейт первым (ранний выход); только self-hosting по умолчанию. |

View File

@@ -0,0 +1,74 @@
---
type: review
work_item_id: ORCH-022
verdict: APPROVED
version: 1
---
# Review ORCH-022
## Summary
Security-гейт (secret-scanning `gitleaks` + dependency audit `pip-audit`) реализован как
детерминированный под-гейт ребра `deploy-staging → deploy`, исполняемый ПЕРВЫМ среди
edge-под-гейтов — в точности по ADR-001 (Вариант M) и эталонному паттерну соседей
(merge-gate ORCH-043 / image-freshness ORCH-058): leaf-модуль `src/security_gate.py`
(never-raise) + тонкая обёртка `check_security_gate` в `QG_CHECKS` (lazy-import, нет цикла)
+ врезка `_handle_security_gate` ПЕРВОЙ в блоке `current_stage == "deploy-staging"`.
`STAGE_TRANSITIONS` и схема БД не тронуты. Все 772 теста зелёные (25 из них —
security-специфичные: `test_security_gate.py`, `test_qg_security.py`,
`test_stage_engine_security_gate.py`). Документация обновлена полностью и в этом же PR.
### Соответствие ТЗ (02-trz)
- FR-1 secret-scan offline `origin/main..HEAD`, любой секрет вне аллоулиста → FAIL ✓
- FR-2 dep-audit по severity (`HIGH` дефолт), MEDIUM/LOW/UNKNOWN → warning ✓
- FR-3 машинный вердикт ТОЛЬКО из frontmatter `17-security-report.md`, negative-токен
авторитетен, write→read-back (единый источник истины) ✓
- FR-4 FAIL → откат на `development` + developer-retry (cap 3) + `task_desc` с дословными
находками (ORCH-046) ✓
- FR-5 условность `security_gate_enabled` / `security_gate_repos` (пусто → self-hosting) ✓
- FR-6 never-raise + таймаут `security_scan_timeout_s`
- FR-7 наблюдаемость (Telegram при degraded/FAIL, лог при PASS) ✓
- §6 без миграций БД, §7 инварианты соблюдены (STAGE_TRANSITIONS/QG_CHECKS консистентны,
gate не деплоит/не рестартит прод) ✓
### Соответствие ADR (06-adr/ADR-001 + global adr-0012)
Р-1 (размещение ПЕРВЫМ, до merge-gate, до захвата merge-lease → lease не освобождается),
Р-2 (gitleaks pinned Go-бинарь в Dockerfile, pip-audit в requirements), Р-3 (fail-open
degrade + флаг `security_dep_audit_fail_closed`), Р-4 (пороги, UNKNOWN→warning), Р-5
(артефакт + read-back), Р-6 (откат/cap/эскалация), Р-7 (lazy-import, double-guard
never-raise), Р-8 (self-hosting safety) — все реализованы как описано.
### Критерии приёмки (03)
AC-1..AC-21 покрыты тестами TC-01..TC-21 (incl. rollback TC-16, verbatim task_desc TC-17,
cap+blocked TC-18, PASS-advance TC-19, no-deploy-on-FAIL TC-21). AC-20 (документация) —
подтверждён ниже.
## Findings
### P0 — Blocker
- нет
### P1 — Must fix
- нет
### P2 — Should fix
- нет
### P3 — Nice-to-have
- Глобальный `docs/architecture/adr/adr-0012-security-gate.md` помечен `Статус: proposed`,
тогда как per-WI `06-adr/ADR-001``Accepted`. Косметическая рассинхронизация статуса,
на функциональность/гейты не влияет.
## Документация
Обновлена в том же PR (AC-20, CLAUDE.md §6 соблюдён):
- `CLAUDE.md` — раздел «Артефакты задачи» (добавлен `17-security-report.md`) + строка о
машинных вердиктах (`security_status:`).
- `docs/architecture/README.md` — реестр `QG_CHECKS` (`check_security_gate (ORCH-022)`),
новый раздел «Security-гейт …», статусная сноска внизу.
- `docs/architecture/adr/adr-0012-security-gate.md` — новый global ADR (+ per-WI ADR-001).
- `CHANGELOG.md` — подробная запись в `[Unreleased] / Added`.
- `.env.example` — все шесть `ORCH_SECURITY_*` с комментариями.
- `Dockerfile` (pinned gitleaks), `requirements.txt` (pip-audit), `.gitleaks.toml` (корень,
правила + аллоулист) — инфраструктура версионирована.
Статус: документация = golden source — синхронна с кодом. Замечаний нет.

View File

@@ -0,0 +1,76 @@
---
type: test-report
work_item_id: ORCH-022
result: PASS
---
# Test Report — ORCH-022
Security-гейт: secret-scanning (gitleaks) + dependency audit (pip-audit) как под-гейт
ребра `deploy-staging → deploy`.
## Окружение
- Python: 3.12.13
- pytest: 8.3.3
- Дата: 2026-06-07
- Ветка: `feature/ORCH-022-security-secret-scanning`
- Review verdict: APPROVED (`12-review.md`)
## Smoke test API (prod 8500, self-hosting — не трогаем контейнер)
| Endpoint | Результат |
|----------|-----------|
| `GET /health` | `{"status":"ok","service":"orchestrator"}` — OK |
| `GET /status` | OK (active task ORCH-022 в stage=testing виден) |
| `GET /queue` | OK (counts/resilience/reconcile/reaper/post_deploy присутствуют) |
## Результаты (привязка к 04-test-plan.yaml)
| TC ID | Описание | Тест | Результат |
|-------|----------|------|-----------|
| TC-01 | Секрет в diff → FAIL, secrets_found>=1, причина называет находку | test_security_gate.py::test_tc01_secret_in_diff_fails | PASS |
| TC-02 | Чистая ветка → PASS, secrets_found=0 | test_tc02_clean_branch_passes | PASS |
| TC-03 | Аллоулист подавляет заведомо-безопасное | test_tc03_allowlisted_match_does_not_fail | PASS |
| TC-04 | HIGH/CRITICAL CVE при пороге HIGH → FAIL, deps_blocking>=1 | test_tc04_high_cve_at_high_threshold_blocks | PASS |
| TC-05 | Только MEDIUM/LOW → PASS, deps_warning>=1 | test_tc05_only_medium_low_warns_passes | PASS |
| TC-06 | Конфиг порога severity влияет на классификацию | test_tc06_threshold_config_changes_classification | PASS |
| TC-07 | Недоступный фид → детерминированный degrade (fail-open default / fail-closed strict) | test_tc07_degraded_feed_failopen_default_failclosed_strict | PASS |
| TC-08 | Вердикт ТОЛЬКО из frontmatter; negative-токен авторитетен | test_tc08_verdict_only_from_frontmatter | PASS |
| TC-09 | Нет/битый frontmatter → (False, reason) fail-closed | test_tc09_missing_or_broken_frontmatter_failclosed | PASS |
| TC-10 | Артефакт 17-security-report.md с валидным frontmatter + телом | test_tc10_artifact_has_valid_frontmatter_and_body | PASS |
| TC-11 | Нет бинаря / исключение → (False, reason), never-raise | test_tc11_missing_binary_failclosed_never_raises | PASS |
| TC-12 | Таймаут → детерминированный fail-closed, без зависания | test_tc12_timeout_is_deterministic_failclosed | PASS |
| TC-13 | Не-self репо при пустом scope → (True, N/A) мгновенно | test_qg_security.py::test_tc13_non_self_repo_empty_scope_is_na | PASS |
| TC-14 | ORCH_SECURITY_GATE_ENABLED=false → no-op pass | test_tc14_disabled_is_noop_pass | PASS |
| TC-15 | Зарегистрирован в QG_CHECKS и диспетчеризуется _run_qg | test_tc15_registered_in_qg_checks / test_tc15_dispatched_by_run_qg | PASS |
| TC-16 | FAIL → откат на development, enqueue developer, notify_qg_failure | test_stage_engine_security_gate.py::test_tc16_fail_rolls_back_and_enqueues_developer | PASS |
| TC-17 | task_desc несёт дословную причину (ORCH-046) | test_tc17_task_desc_has_verbatim_findings | PASS |
| TC-18 | После MAX_DEVELOPER_RETRIES (3) → set_issue_blocked + Telegram | test_tc18_retry_cap_blocks_and_alerts | PASS |
| TC-19 | PASS → штатное продвижение конвейера | test_tc19_pass_advances_normally | PASS |
| TC-20 | STAGE_TRANSITIONS не изменён; тесты стадий зелёные | tests/test_stages.py (полный прогон) | PASS |
| TC-21 | Гейт не вызывает деплой-хук/рестарт прод (self-hosting safety) | test_tc21_fail_never_triggers_deploy | PASS |
Все 21 TC покрыты и зелёные. Соответствие критериям приёмки (03-acceptance-criteria):
AC-1..AC-21 закрыты соответствующими TC (AC-N ↔ TC-N для N=1..21; AC-20 «документация»
подтверждён в review 12-review.md).
## Вывод pytest
### Security-специфичные тесты (25 шт.)
```
tests/test_security_gate.py ............... (15)
tests/test_qg_security.py ...... (6)
tests/test_stage_engine_security_gate.py ..... (5)
======================== 25 passed, 1 warning in 0.49s =========================
```
### Полный регресс
```
======================= 772 passed, 1 warning in 14.70s ========================
```
(1 warning — PydanticDeprecatedSince20 в src/config.py, не связан с ORCH-022,
существовал до задачи.)
## Итог
**PASS** — полный регресс 772/772 зелёный, 25 security-тестов покрывают все 21 TC
плана и AC-1..AC-21, smoke-тесты API прод-инстанса OK. Прод-контейнер в процессе
тестирования не затронут (тесты офлайн/изолированы). Задача готова к стадии deploy-staging.

View File

@@ -0,0 +1,12 @@
---
deploy_status: SUCCESS
work_item: ORCH-022
hook_exit_code: 0
deployed_by: deploy-finalizer
---
# Deploy log — ORCH-036 executable self-deploy
Прод-деплой завершён хост-хуком с exit-code `0` -> `deploy_status: SUCCESS`.
Вердикт зафиксирован детерминированным finalizer'ом (Фаза C), не LLM.

View File

@@ -0,0 +1,14 @@
---
post_deploy_status: HEALTHY
action_taken: NONE
work_item: ORCH-022
window_s: 900
checks_total: 30
checks_failed: 0
---
# Post-deploy log — ORCH-021 post-deploy monitor
Наблюдение прода завершено: `post_deploy_status: HEALTHY`, `action_taken: NONE`.
Окно наблюдения: 900s; опросов всего: 30, из них с провалом: 0.

View File

@@ -0,0 +1,7 @@
# Business Request: Approve деплоя через статус Confirm Deploy (вместо перегруженного Approved)
Work Item ID: ORCH-059
## Description
TBD

View File

@@ -0,0 +1,115 @@
# 01 — BRD: Approve прод-деплоя через выделенный статус «Confirm Deploy»
Work Item: **ORCH-059**
Repo: `orchestrator`
Stage: analysis
Тип: enhancement / risk-reduction (self-hosting)
## 1. Контекст и проблема
В ORCH-036 («исполняемый самодеплой стадии `deploy`») прод-деплой self-hosting
инстанса (контейнер `orchestrator`, порт 8500) запускается **Фазой B**: человек
переводит issue в Plane-статус **`Approved`**, webhook
`work_item.updated``handle_issue_updated``handle_verdict(approved=True)`
`_try_advance_stage``advance_stage(finished_agent=None)`, и в
`stage_engine.advance_stage` срабатывает блок
`current_stage == "deploy" and finished_agent is None`
`_handle_self_deploy_phase_b` → detached host-деплой прода.
**Перегрузка статуса.** Тот же самый Plane-статус `Approved` (UUID
`a519a341-…`) используется как **человеческий гейт одобрения BRD** на ранней
стадии `analysis` (`check_analysis_approved`: analysis → architecture) и в общем
verdict-роутинге `handle_verdict`. Один и тот же визуальный «Approved» на доске
означает две принципиально разные вещи:
- на `analysis` — «BRD/ТЗ/AC приняты, продолжай конвейер» (дёшево, обратимо);
- на `deploy` — «**ВЫКАТИ В ПРОД** инструмент, который прямо сейчас обслуживает
все проекты из одного инстанса с общей БД» (дорого, групповой риск, см.
раздел Self-hosting в `CLAUDE.md`).
### Последствия (Pain)
- **Двусмысленность семантики.** Один статус — два смысла; оператор не видит из
названия, что клик на `deploy` запускает реальный прод-рестарт.
- **Риск случайного клика.** Привычный жест «Approved» (которым оператор
штатно одобряет BRD десятки раз) на стадии `deploy` молча триггерит
прод-деплой. Цена ошибки — незапланированный рестарт прод-инстанса,
встающий конвейер всех проектов.
- **Несоответствие ожиданиям ORCH-036.** В scope ORCH-36 заявлялась Telegram
inline-кнопка подтверждения; в коде её **нет** — developer реализовал approve
исключительно через Plane-статус. Отдельного «осознанного» жеста подтверждения
деплоя в системе сейчас не существует.
## 2. Решение Owner
Ввести **отдельный Plane-статус `Confirm Deploy`** в проекте ORCH, который
триггерит **ТОЛЬКО** Фазу B self-deploy на стадии `deploy`. Статус `Approved`
перестаёт запускать прод-деплой и сохраняет единственный смысл — человеческое
одобрение на гейтах конвейера (прежде всего BRD на `analysis`).
Минимальная правка: `handle_verdict` в `src/webhooks/plane.py` + регистрация
нового состояния в проекте ORCH (Plane + резолвер состояний).
## 3. Бизнес-цели
- **BG-1.** Убрать двусмысленность: жест «запустить прод-деплой» отделён от жеста
«одобрить артефакт».
- **BG-2.** Снизить риск случайного прод-деплоя: запуск прода требует явного,
редко используемого статуса `Confirm Deploy`, а не привычного `Approved`.
- **BG-3.** Не сломать работающий self-hosting конвейер при доработке самого
инструмента (нулевая регрессия `analysis`-гейта и не-self репозиториев).
## 4. Объём (Scope)
### В объёме
- Новый логический статус `confirm_deploy` («Confirm Deploy») в резолвере
состояний Plane (`src/plane_sync.py`).
- Маршрутизация нового статуса в `src/webhooks/plane.py`
(`handle_issue_updated` / `handle_verdict`) на путь Фазы B прод-деплоя.
- Прекращение триггера Фазы B по статусу `Approved` на стадии `deploy`.
- Обновление текста CTA Фазы A (Plane-комментарий + Telegram в
`stage_engine._handle_self_deploy_phase_a`): инструктировать оператора
переводить задачу в `Confirm Deploy`, а не в `Approved`.
- Конфигурация Plane: создание статуса «Confirm Deploy» в проекте ORCH
(предусловие эксплуатации — фиксируется в TRZ/AC как требование среды).
- Обновление документации (`CLAUDE.md`, `docs/architecture/README.md` секция
ORCH-036, `CHANGELOG.md`) и ADR per-work-item.
### Вне объёма
- Telegram inline-кнопки подтверждения деплоя (отдельная задача; здесь не
реализуем — управление по-прежнему статусом Plane).
- Полностью автоматический approve деплоя (ORCH-54).
- Изменение Фаз A/C, exit-кодов хука, merge-gate, `check_deploy_status`,
схемы БД, реестров `STAGE_TRANSITIONS` / `QG_CHECKS`.
- Поведение прод-деплоя для не-self репозиториев (остаётся прежним).
- Post-deploy наблюдение (ORCH-021) — не затрагивается.
## 5. Заинтересованные стороны
- **Owner/оператор** — переводит задачи по статусам; главный выгодоприобретатель
снижения риска.
- **Self-hosting конвейер** — все проекты на общем инстансе; косвенно зависят от
безопасности прод-деплоя орка.
## 6. Допущения
- A-1. Plane позволяет добавить кастомный статус «Confirm Deploy» в проект ORCH;
его UUID резолвится через `get_project_states` (API `/states/`).
- A-2. Статус `Confirm Deploy` нужен только проекту ORCH (self-hosting). Прочие
проекты прод-деплой через Plane-approve не используют
(`self_deploy_applies` → только `orchestrator`).
- A-3. Оператор переводит задачу в `Confirm Deploy` только когда она реально
находится на стадии `deploy` (approval-pending после Фазы A).
## 7. Риски (детально — 10-tech-risks.md, ведёт архитектор)
- R-1. Новый логический ключ `confirm_deploy` отсутствует в fallback
`_DEFAULT_STATES` и в проектах без этого статуса → обращение к ключу должно
быть безопасным (fail-closed: нет статуса → нет деплоя, не падение).
- R-2. Регрессия: `Approved` на `deploy` после правки не должен НИ
запускать деплой, НИ вызывать ложный откат/advance.
- R-3. Самоправка прода: правка не должна потребовать ручного рестарта прод-
контейнера вне штатной стадии deploy-staging → deploy.
## 8. Definition of Done (бизнес-уровень)
- Перевод задачи стадии `deploy` в `Confirm Deploy` запускает прод-деплой
(Фаза B) ровно так же, как раньше делал `Approved`.
- Перевод задачи стадии `deploy` в `Approved` прод-деплой НЕ запускает.
- `Approved` на `analysis` (и прочих человеческих гейтах) работает без изменений.
- CTA Фазы A просит `Confirm Deploy`.
- Документация и ADR обновлены в том же PR.

View File

@@ -0,0 +1,103 @@
# 02 — ТЗ: выделенный статус «Confirm Deploy» как триггер прод-деплоя
Work Item: **ORCH-059** · Repo: `orchestrator` · Stage: analysis
> ТЗ описывает **что** должно измениться и **поведенческий контракт**. Конкретный
> дизайн (сигнатуры, способ проброса признака «confirm-deploy» из webhook в
> `stage_engine`, sentinel-обработка) — за архитектором (ADR per-work-item).
> Точки касания ниже заданы бизнес-запросом Owner и текущей реализацией ORCH-036.
## 1. Задействованные модули `src/`
| Модуль | Роль в задаче |
|--------|---------------|
| `src/plane_sync.py` | Резолвер состояний Plane. Добавить логический ключ `confirm_deploy` ↔ имя статуса «Confirm Deploy»; обеспечить безопасный доступ при отсутствии статуса (fallback/неполный конфиг). |
| `src/webhooks/plane.py` | `handle_issue_updated` — маршрутизация нового статуса; `handle_verdict` — отделить «подтверждение деплоя» от обычного approve; снять триггер Фазы B со статуса `Approved` на `deploy`. |
| `src/stage_engine.py` | Блок Фазы B (`current_stage == "deploy" and finished_agent is None`) должен срабатывать ТОЛЬКО по сигналу confirm-deploy, не по обычному Approved. Обновить CTA-текст Фазы A (`_handle_self_deploy_phase_a`). |
| `src/config.py` | (опционально, на усмотрение архитектора) флаг/имя статуса, если потребуется конфигурируемость. По умолчанию — не требуется. |
## 2. Поведенческий контракт (требования)
### TRZ-1. Регистрация статуса «Confirm Deploy»
Резолвер состояний (`get_project_states`) обязан возвращать UUID статуса
«Confirm Deploy» под логическим ключом `confirm_deploy` для проекта ORCH.
Маппинг имени `"Confirm Deploy" → "confirm_deploy"` добавляется в
`_PLANE_NAME_TO_KEY`. Для проектов/сред, где статус отсутствует (enduro,
fallback `_DEFAULT_STATES`, недоступный API), ключ может отсутствовать —
обращение к нему должно быть **fail-closed**: «нет статуса → ветка confirm-deploy
не активируется», без `KeyError`/исключения.
### TRZ-2. Триггер прод-деплоя по «Confirm Deploy»
Когда задача находится на стадии `deploy` и issue переводится в статус
`Confirm Deploy`, система обязана инициировать **Фазу B** прод-деплоя
(эквивалент текущего `_handle_self_deploy_phase_b`: idempotency-guard `initiated`,
`self_deploy.initiate_deploy`, постановка `deploy-finalizer`, комментарии/Telegram).
Поведение, идемпотентность и Фаза C — **без изменений** относительно ORCH-036;
меняется только **что именно является триггером**.
### TRZ-3. `Approved` больше не запускает прод-деплой
Перевод задачи стадии `deploy` в статус `Approved` **не должен** инициировать
Фазу B. Он не должен также вызывать ложный откат (БАГ-8) или ложный advance
по `check_deploy_status` (вердикта ещё нет). Допустимое поведение — **no-op с
логированием** (issue остаётся на `deploy`/approval-pending). Конкретный способ
(игнор на уровне webhook-роутинга или на уровне `stage_engine`) — за архитектором.
### TRZ-4. Сохранность гейта `Approved` на остальных стадиях
Статус `Approved` обязан продолжать работать как человеческий гейт:
- `analysis``architecture` (`check_analysis_approved`, approved-via-status);
- любой иной человеческий approve-advance, существующий сегодня.
Регрессия `handle_verdict(approved=True)` для НЕ-`deploy` стадий недопустима.
### TRZ-5. CTA Фазы A
Текст запроса approve в `_handle_self_deploy_phase_a` (Plane-комментарий + Telegram)
обязан инструктировать оператора переводить задачу в статус **`Confirm Deploy`**
(а не `Approved`) для запуска прод-деплоя.
### TRZ-6. Условность (как ORCH-35/36)
Ветка confirm-deploy реальна только для self-hosting
(`self_deploy.self_deploy_applies(repo)``orchestrator`). Для прочих репо —
прежнее поведение (синхронный деплой агентом), статус `Confirm Deploy` не
требуется и не влияет.
## 3. Изменения API
Изменений HTTP-эндпоинтов **нет**. Канал — существующий `POST /webhook/plane`
(событие `work_item.updated`). Внешнее изменение: в проекте ORCH появляется
дополнительный статус доски «Confirm Deploy» (Plane-конфигурация, не код-API).
## 4. Изменения схемы БД
**Нет.** `STAGE_TRANSITIONS`, реестр `QG_CHECKS`, таблицы `tasks`/`jobs`/
`agent_runs`/`events` — без изменений. Статусы — на стороне Plane; restart-safe
состояние деплоя — существующие sentinel-файлы ORCH-036 (без миграций).
## 5. Требования к новым QG checks
**Нет.** Новый Quality Gate не вводится. `check_deploy_status` /
`_parse_deploy_status` и контракт exit-кодов хука (0/1/2) — без изменений.
## 6. Конфигурация среды (предусловие эксплуатации)
- В проекте ORCH в Plane создаётся статус доски **«Confirm Deploy»** (точное имя,
чувствительно к регистру — должно совпасть с ключом `_PLANE_NAME_TO_KEY`).
- Размещение статуса на доске — рядом со стадией deploy/approval-pending
(рекомендация эксплуатации, не код).
- Кэш состояний (`get_project_states` / `reload_project_states`): после создания
статуса может потребоваться сброс кэша или рестарт по штатной стадии deploy.
## 7. Артефакты, создаваемые/обновляемые по pipeline
- `docs/work-items/ORCH-059/06-adr/ADR-001-confirm-deploy-status.md` — решение
(как отличается триггер; где разрезается перегрузка `Approved`; fail-closed
при отсутствии статуса) — **ведёт архитектор**.
- `CLAUDE.md` — упоминание выделенного статуса approve прод-деплоя (раздел
self-hosting / артефакты).
- `docs/architecture/README.md` — секция ORCH-036: уточнить, что Фаза B
триггерится статусом `Confirm Deploy`, а не `Approved`.
- `CHANGELOG.md` — запись ORCH-059.
- `12-review.md`, `13-test-report.md`, `14-deploy-log.md`, `15-staging-log.md`
штатно по стадиям конвейера.
## 8. Совместимость и инварианты
- Не меняются: `STAGE_TRANSITIONS`, `QG_CHECKS`, `check_deploy_status`,
БАГ-8 (FAILED → откат на development), merge-gate, exit-коды хука, Фазы A/C,
схема БД, post-deploy (ORCH-021).
- Self-hosting safety: правка НЕ требует внепланового рестарта прод-контейнера;
выкат — через штатный deploy-staging (8501) → deploy.
- Never-crash: отсутствие статуса `Confirm Deploy` в резолвере не приводит к
исключению в webhook-пути.

View File

@@ -0,0 +1,76 @@
# 03 — Критерии приёмки: ORCH-059
Repo: `orchestrator` · Stage: analysis
Каждый критерий — однозначный PASS/FAIL. Проверка: unit/integration (см.
`04-test-plan.yaml`) + ручная верификация для инфра-предусловий.
## AC-1 — Статус «Confirm Deploy» резолвится
**Given** проект ORCH со статусом доски «Confirm Deploy»
**When** вызывается резолвер состояний для проекта ORCH
**Then** возвращается логический ключ `confirm_deploy` с непустым UUID,
а маппинг `"Confirm Deploy" → "confirm_deploy"` присутствует в `_PLANE_NAME_TO_KEY`.
**FAIL:** ключ отсутствует или указывает на UUID статуса `Approved`.
## AC-2 — «Confirm Deploy» на стадии `deploy` запускает Фазу B
**Given** задача self-hosting (`orchestrator`) на стадии `deploy`,
`deploy_require_manual_approve=true`, маркер `initiated` отсутствует
**When** приходит `work_item.updated` со статусом `Confirm Deploy`
**Then** инициируется Фаза B: вызывается `self_deploy.initiate_deploy`,
ставится job `deploy-finalizer`, пишется маркер `initiated`.
**FAIL:** прод-деплой не инициирован, либо finalizer не поставлен.
## AC-3 — «Approved» на стадии `deploy` НЕ запускает прод-деплой
**Given** та же задача на стадии `deploy`
**When** приходит `work_item.updated` со статусом `Approved`
**Then** `self_deploy.initiate_deploy` **НЕ** вызывается; Фаза B не стартует;
задача не откатывается (БАГ-8 не срабатывает) и не «доходит» по
`check_deploy_status` (вердикта нет); событие залогировано как no-op.
**FAIL:** вызван `initiate_deploy`, либо произошёл откат/ложный advance.
## AC-4 — «Approved» на `analysis` работает без регрессии
**Given** задача на стадии `analysis` (BRD готов, approval-pending)
**When** issue переводится в `Approved`
**Then** срабатывает approved-via-status и задача продвигается
`analysis → architecture` (как до правки).
**FAIL:** approve на analysis перестал продвигать конвейер.
## AC-5 — Идемпотентность Фазы B по «Confirm Deploy»
**Given** задача на `deploy`, маркер `initiated` уже существует
**When** повторно приходит статус `Confirm Deploy` (двойной клик / дубль webhook)
**Then** повторного `initiate_deploy` не происходит (no-op,
`self-deploy-already-initiated`).
**FAIL:** прод-деплой запускается повторно.
## AC-6 — CTA Фазы A просит «Confirm Deploy»
**Given** Фаза A (`deploy-staging → deploy`, approval-pending)
**When** формируются Plane-комментарий и Telegram-уведомление запроса approve
**Then** текст инструктирует перевести задачу в статус **`Confirm Deploy`**
(а не «Approved») для запуска прод-деплоя.
**FAIL:** CTA по-прежнему упоминает только «Approved».
## AC-7 — Fail-closed при отсутствии статуса
**Given** среда без статуса «Confirm Deploy» (enduro / fallback `_DEFAULT_STATES`
/ недоступный Plane API)
**When** обрабатывается `work_item.updated`
**Then** webhook-путь не выбрасывает исключение; ветка confirm-deploy не
активируется (прод-деплой не запускается «вслепую»).
**FAIL:** `KeyError`/исключение в обработчике, либо ложный запуск Фазы B.
## AC-8 — Условность для не-self репозиториев
**Given** не-self репозиторий (`self_deploy_applies(repo) == False`)
**When** приходит любой verdict-статус на стадии `deploy`
**Then** поведение прод-деплоя не меняется относительно текущего (синхронный
деплой агентом); статус `Confirm Deploy` не требуется.
**FAIL:** изменилось поведение деплоя не-self проекта.
## AC-9 — Инварианты не нарушены
**Then** `STAGE_TRANSITIONS`, реестр `QG_CHECKS`, `check_deploy_status`/
`_parse_deploy_status`, контракт exit-кодов хука (0/1/2), Фазы A/C, merge-gate,
схема БД — без изменений; `pytest tests/ -q` зелёный.
**FAIL:** изменён любой из перечисленных контрактов или красные тесты.
## AC-10 — Документация обновлена (golden source)
**Then** в том же PR обновлены `CLAUDE.md`, секция ORCH-036 в
`docs/architecture/README.md`, `CHANGELOG.md`; заведён
`06-adr/ADR-001-confirm-deploy-status.md`.
**FAIL:** функционал изменён, документация — нет (Reviewer → REQUEST_CHANGES).

View File

@@ -0,0 +1,109 @@
work_item: ORCH-059
title: Approve прод-деплоя через выделенный статус «Confirm Deploy»
repo: orchestrator
stage: analysis
# Контракт-тесты: триггер прод-деплоя смещается с перегруженного `Approved`
# на выделенный статус `Confirm Deploy`. Деплой и сетевые вызовы мокаются.
tests:
- id: TC-01
type: unit
description: "_PLANE_NAME_TO_KEY содержит маппинг 'Confirm Deploy' -> 'confirm_deploy'"
module: tests/test_plane_states.py
expected: PASS
- id: TC-02
type: unit
description: >-
get_project_states для проекта ORCH (мок API со статусом 'Confirm Deploy')
возвращает непустой UUID под ключом 'confirm_deploy', отличный от 'approved'
module: tests/test_plane_states.py
expected: PASS
- id: TC-03
type: unit
description: >-
Fail-closed: при отсутствии статуса 'Confirm Deploy' (fallback _DEFAULT_STATES /
недоступный API) доступ к ключу confirm_deploy не выбрасывает исключение
и не активирует ветку confirm-deploy
module: tests/test_plane_states.py
expected: PASS
- id: TC-04
type: unit
description: >-
handle_issue_updated: статус 'Confirm Deploy' на задаче стадии deploy
маршрутизируется на путь Фазы B (а не на обычный approve/advance)
module: tests/test_plane_confirm_deploy.py
expected: PASS
- id: TC-05
type: unit
description: >-
handle_verdict/Approved на стадии deploy НЕ вызывает self_deploy.initiate_deploy
(initiate_deploy замокан и не должен быть вызван)
module: tests/test_plane_confirm_deploy.py
expected: PASS
- id: TC-06
type: unit
description: >-
Approved на стадии analysis по-прежнему продвигает analysis -> architecture
(approved-via-status, регрессия гейта check_analysis_approved)
module: tests/test_plane_confirm_deploy.py
expected: PASS
- id: TC-07
type: unit
description: >-
stage_engine: блок Фазы B (current_stage==deploy, finished_agent is None)
инициирует deploy ТОЛЬКО по сигналу confirm-deploy; Approved-сигнал -> no-op
module: tests/test_stage_engine_phase_b.py
expected: PASS
- id: TC-08
type: unit
description: >-
Идемпотентность: при существующем маркере 'initiated' повторный
Confirm Deploy не вызывает initiate_deploy (self-deploy-already-initiated)
module: tests/test_stage_engine_phase_b.py
expected: PASS
- id: TC-09
type: unit
description: >-
CTA Фазы A (_handle_self_deploy_phase_a): текст Plane-комментария и Telegram
содержат 'Confirm Deploy' и не предлагают 'Approved' как триггер деплоя
module: tests/test_stage_engine_phase_a_cta.py
expected: PASS
- id: TC-10
type: integration
description: >-
E2E (мок Plane API + self_deploy): задача на deploy -> webhook Confirm Deploy
-> initiate_deploy вызван, deploy-finalizer поставлен, маркер initiated записан
module: tests/test_confirm_deploy_integration.py
expected: PASS
- id: TC-11
type: integration
description: >-
E2E: задача на deploy -> webhook Approved -> прод-деплой НЕ инициирован,
задача остаётся на deploy (нет отката, нет advance в done)
module: tests/test_confirm_deploy_integration.py
expected: PASS
- id: TC-12
type: integration
description: >-
Условность: для не-self репозитория verdict-статусы на deploy не меняют
поведение деплоя (self_deploy_applies == False)
module: tests/test_confirm_deploy_integration.py
expected: PASS
regression:
- id: RG-01
type: integration
description: "pytest tests/ -q зелёный; STAGE_TRANSITIONS и QG_CHECKS без изменений"
module: tests/
expected: PASS

View File

@@ -0,0 +1,156 @@
# ADR-001 (ORCH-059): Выделенный статус «Confirm Deploy» как триггер прод-деплоя
## Статус
Accepted (design) — реализация в ветке `feature/ORCH-059-approve-confirm-deploy-approve`.
## Контекст
ORCH-036 (исполняемый самодеплой стадии `deploy`) запускает прод-деплой
self-hosting инстанса **Фазой B**: человек переводит issue в Plane-статус
`Approved` → webhook `work_item.updated``handle_issue_updated`
`handle_verdict(approved=True)``_try_advance_stage`
`advance_stage(finished_agent=None)`; в `stage_engine.advance_stage` блок
`current_stage == "deploy" and finished_agent is None`
`_handle_self_deploy_phase_b` → detached host-деплой прода (8500).
Тот же UUID `Approved` (`a519a341-…`, `_DEFAULT_STATES["approved"]`) — это
**человеческий гейт одобрения** на стадии `analysis`
(`check_analysis_approved`, путь `approved-via-status`) и общий verdict-роутинг
в `handle_verdict`. Один визуальный «Approved» на доске значит две принципиально
разные вещи: «принять BRD» (дёшево, обратимо) и «**ВЫКАТИТЬ В ПРОД** инструмент,
обслуживающий все проекты из одного инстанса с общей БД» (дорого, групповой
риск). Привычный жест approve на стадии `deploy` молча триггерит прод-рестарт —
цена случайного клика высока (см. self-hosting в `CLAUDE.md`).
Ограничения, формирующие дизайн (см. `02-trz.md`, `03-acceptance-criteria.md`):
1. **Нулевая регрессия** гейта `Approved` на `analysis` и прочих стадиях (TRZ-4).
2. **Fail-closed**: среды без статуса (enduro, fallback `_DEFAULT_STATES`,
недоступный API) не должны падать и не должны «вслепую» деплоить (TRZ-1, R-1).
3. **`Approved` на `deploy` не должен** запускать Фазу B И не должен вызывать
ложный откат (БАГ-8) или ложный advance по `check_deploy_status` — вердикта
ещё нет (TRZ-3, R-2).
4. **Без правки контрактов**: `STAGE_TRANSITIONS`, `QG_CHECKS`,
`check_deploy_status`, Фазы A/C, merge-gate, exit-коды хука, схема БД (TRZ-8).
5. **Self-hosting safety**: правка — чистая маршрутизация, не требует внепланового
рестарта прода; выкат через штатный `deploy-staging` (8501) → `deploy` (R-3).
## Решение
Ввести отдельный логический статус `confirm_deploy` («Confirm Deploy»), который
триггерит **ТОЛЬКО** Фазу B на стадии `deploy`. `Approved` теряет смысл «запусти
прод-деплой» и остаётся исключительно человеческим гейтом конвейера.
Четыре точечные правки в трёх модулях:
### 1. Резолвер состояний — `src/plane_sync.py`
- В `_PLANE_NAME_TO_KEY` добавить маппинг `"Confirm Deploy" → "confirm_deploy"`.
- В `_DEFAULT_STATES` ключ `confirm_deploy` **НЕ добавлять** (реального UUID для
enduro/fallback нет; отсутствие ключа = fail-closed). Для проекта ORCH ключ
резолвится `get_project_states` из живого Plane API; для проектов без статуса и
на fallback-пути ключ просто отсутствует в результирующем словаре.
- Следствие: `get_project_states(orch)["confirm_deploy"]` → реальный UUID;
`get_project_states(enduro).get("confirm_deploy")``None`.
### 2. Маршрутизация webhook — `src/webhooks/plane.py`
В `handle_issue_updated`, **до** ветки `approved`, добавить fail-closed-ветку:
```python
confirm_state = proj_states.get("confirm_deploy") # .get -> AC-7/R-1
if confirm_state and new_state == confirm_state:
await handle_confirm_deploy(data, project_id)
elif new_state == proj_states["in_progress"]:
...
elif new_state == proj_states["approved"]:
await handle_verdict(data, project_id, approved=True)
```
Новый `handle_confirm_deploy(data, project_id)`:
- резолвит задачу по `plane_id`;
- если `stage != "deploy"`**no-op с логом** (Confirm Deploy осмыслен только на
approval-pending стадии `deploy`; защищает прочие гейты от случайного approve);
- иначе → `_try_advance_stage(..., confirm_deploy=True)`.
`handle_verdict(approved=True)` не меняется — продолжает звать `_try_advance_stage`
с `confirm_deploy=False` (дефолт).
### 3. Сигнал в движок — `src/stage_engine.advance_stage(...)`
Добавить keyword-only параметр `confirm_deploy: bool = False` (back-compat: все
существующие вызовы из launcher/reconciler/finalizer/webhook передают
`finished_agent`, новый kwarg дефолтный). Блок Фазы B переписать так, чтобы он
**всегда возвращался рано** для `deploy + finished_agent is None` self-hosting,
но деплоил только по сигналу:
```python
if (current_stage == "deploy" and finished_agent is None
and settings.deploy_require_manual_approve
and self_deploy.self_deploy_applies(repo)):
if confirm_deploy:
_handle_self_deploy_phase_b(task_id, repo, work_item_id, branch, result)
else:
# TRZ-3/R-2: обычный Approved на deploy — no-op; НЕ запускаем
# check_deploy_status (вердикта ещё нет -> ложный откат БАГ-8).
result.note = "approved-on-deploy-noop"
return result
```
Ключевое: возврат **до** блока Quality Gate в обоих случаях → `check_deploy_status`
по `Approved` на `deploy` не исполняется. Фаза C (finalizer,
`finished_agent="deployer"`) не затронута — условие требует `finished_agent is
None`.
### 4. CTA Фазы A — `src/stage_engine._handle_self_deploy_phase_a`
Текст Plane-комментария и Telegram изменить: вместо «смените статус на Approved»
инструктировать перевести задачу в статус **«Confirm Deploy»** для запуска
прод-деплоя (TRZ-5/AC-6).
### Условность (как ORCH-35/36)
Вся ветка реальна только для `self_deploy.self_deploy_applies(repo)`
`orchestrator`. Прочие репо — прежний синхронный ssh-деплой агентом; статус
`Confirm Deploy` им не нужен и на них не влияет (AC-8).
## Альтернативы
- **A. Telegram inline-кнопка подтверждения** вместо нового статуса — отклонено:
кнопочная инфраструктура в коде отсутствует, заявлено вне scope (ORCH-036 п.
«inline-кнопка» не реализован); управление остаётся статусом Plane.
- **B. Добавить `confirm_deploy` в `_DEFAULT_STATES`** — отклонено: реального UUID
«Confirm Deploy» для enduro/fallback нет; пришлось бы подставить фиктивный или
дублирующий UUID, что ломает fail-closed (enduro «получил бы» триггер деплоя) и
смешивает семантику.
- **C. Отдельный публичный entrypoint `stage_engine.initiate_confirm_deploy()`**,
минующий `advance_stage` — отклонено: дублирует гарды
(`deploy_require_manual_approve`, `self_deploy_applies`, idempotency `initiated`),
и всё равно пришлось бы внутри `advance_stage` гасить `Approved`-на-`deploy` в
no-op. Параметр-сигнал проще и держит единую точку правды.
- **D. Сигнал через sentinel-маркер, записываемый webhookом** — отклонено: вызов
синхронный в пределах одного `advance_stage`, persistence не нужна; параметр
явнее и не плодит файловое состояние.
## Последствия
**Плюсы**
- Жест «запустить прод-деплой» отделён от «одобрить артефакт»; случайный approve
на доске больше не роняет прод (BG-1, BG-2).
- `Approved` на `deploy` детерминированно безопасен: no-op без отката/advance
(закрывает R-2).
- Fail-closed: нет статуса → нет деплоя, нет исключения (R-1, AC-7).
- Минимальный диффузный риск: контракты `STAGE_TRANSITIONS`/`QG_CHECKS`/
`check_deploy_status`/Фазы A/C/merge-gate/схема БД не тронуты (AC-9).
- Реконсилятор F-1 на `deploy` (finished_agent=None) теперь попадает в no-op-ветку
вместо прежнего неявного запуска Фазы B → прод-деплой невозможно инициировать
автоматически, только явным человеческим `Confirm Deploy` (усиление safety).
**Минусы / цена**
- Эксплуатационное предусловие: в Plane-проекте ORCH нужно создать статус доски
«Confirm Deploy» (точное имя, регистр) и сбросить кэш состояний — см.
`07-infra-requirements.md`. До создания статуса прод-деплой через approve не
запустится (это и есть желаемое fail-closed-поведение).
- Сигнатура `advance_stage` расширена одним kwarg (обратносовместимо).
**Хэндофф документации (golden source, в том же PR — стадия development).**
ADR (этот файл) — артефакт архитектора. Переписать `Approve = Approved`
`Confirm Deploy` в `docs/architecture/README.md` (секция ORCH-036), `CLAUDE.md`
(self-hosting/артефакты) и добавить запись в `CHANGELOG.md` обязан developer
одновременно с кодом (AC-10), чтобы доки не описывали ещё не существующее
поведение. В README на стадии architecture добавлена forward-looking пометка
ORCH-059 (design), как принято для незамёрженных доработок.
## Связанные ADR
- `adr-0007-executable-self-deploy.md` (ORCH-036) — задаёт Фазы A/B/C; ORCH-059
меняет **только триггер** Фазы B (`Approved``Confirm Deploy`) и делает
`Approved`-на-`deploy` no-op; Фазы внутренне не меняются.
- `adr-0003-staging-gate.md` (ORCH-35) — паттерн условности self-hosting.
- `adr-0007-reconciler.md` (ORCH-053) — реконсилятор F-1: поведение на `deploy`
становится no-op (см. Последствия).

View File

@@ -0,0 +1,44 @@
# 07 — Требования к инфраструктуре: ORCH-059
Work Item: **ORCH-059** · Repo: `orchestrator`
Связано: `06-adr/ADR-001-confirm-deploy-status.md`, `02-trz.md` §6.
> Топология контейнеров/портов/деплоя НЕ меняется (см. `docs/operations/INFRA.md`).
> Единственное инфра-требование ORCH-059 — конфигурация Plane-доски проекта ORCH.
## IR-1. Статус доски «Confirm Deploy» в проекте ORCH (предусловие эксплуатации)
- В Plane-проекте **ORCH** создать кастомный статус доски с **точным** именем
`Confirm Deploy` (case-sensitive, ровно один пробел) — должно посимвольно
совпасть с ключом `_PLANE_NAME_TO_KEY["Confirm Deploy"]`. Несовпадение →
fail-closed (деплой не запустится), не краш (R-9).
- UUID статуса генерирует Plane; код резолвит его через `get_project_states`
(`GET /workspaces/<ws>/projects/<orch>/states/`). Хардкодить UUID не нужно.
- **Размещение** на доске — рядом с approval-pending/`deploy` (рекомендация
эксплуатации, на поведение кода не влияет).
- **Только проект ORCH** (self-hosting). Для enduro и прочих проектов статус НЕ
создаётся и НЕ требуется — `self_deploy_applies` истинно лишь для `orchestrator`.
## IR-2. Сброс кэша состояний после создания статуса
`get_project_states` кэширует резолв per-project на время жизни процесса
(`_STATES_CACHE`). После создания статуса в Plane закэшированный словарь не
содержит `confirm_deploy` (R-5). Применить ОДНО из:
- вызвать `reload_project_states(<orch_project_id>)` (или полный сброс), либо
- штатно перезапустить прод по конвейеру `deploy-staging → deploy` (рестарт
процесса очищает кэш).
> Внеплановый ручной рестарт прод-контейнера для применения этой задачи **не
> требуется** и противопоказан (self-hosting групповой риск). Выкат — только через
> штатный staging→deploy.
## IR-3. Контрольная проверка готовности среды
После IR-1+IR-2:
1. `get_project_states(<orch>)` содержит `confirm_deploy` с непустым UUID,
отличным от `approved` (AC-1, TC-02).
2. Перевод тестовой задачи стадии `deploy` (sandbox) в `Confirm Deploy` запускает
Фазу B; перевод в `Approved` — нет (AC-2/AC-3).
## Что НЕ меняется
- Порты (8500 prod / 8501 staging), контейнеры, compose-профили, env-карта,
деплой-хук, схема БД, sentinel-каталоги ORCH-036 — без изменений.
- HTTP-эндпоинты (`POST /webhook/plane` тот же канал, событие
`work_item.updated`).

View File

@@ -0,0 +1,25 @@
# 10 — Технические риски: ORCH-059
Work Item: **ORCH-059** · Repo: `orchestrator` · ведёт: архитектор
Связано: `06-adr/ADR-001-confirm-deploy-status.md`.
| ID | Риск | Вероятн. | Влияние | Митигация | Проверка |
|----|------|----------|---------|-----------|----------|
| R-1 | Ключ `confirm_deploy` отсутствует в `_DEFAULT_STATES` / у проектов без статуса → `KeyError` в webhook-пути | Сред | Выс (краш обработчика) | Доступ ТОЛЬКО через `.get("confirm_deploy")`; `_DEFAULT_STATES` не содержит ключ намеренно; отсутствие → ветка не активируется (fail-closed) | TC-03, AC-7 |
| R-2 | `Approved` на `deploy` после правки вызывает `check_deploy_status` (вердикта нет) → ложный откат БАГ-8 / ложный advance | Выс | Выс (петля dev↔deploy, ложный rollback прода) | Блок Фазы B возвращается рано для `deploy + finished_agent is None` self-hosting в ОБОИХ случаях; `Approved``note=approved-on-deploy-noop`, QG не запускается | TC-05, TC-07, TC-11, AC-3 |
| R-3 | Самоправка прода требует внепланового рестарта прод-контейнера | Низ | Выс (встаёт конвейер всех проектов) | Изменение — чистая маршрутизация в коде; выкат через штатный `deploy-staging` (8501) → `deploy`; sentinel-состояние ORCH-036 не трогаем | AC-9, RG-01 |
| R-4 | `Confirm Deploy` прислан на не-`deploy` стадии (оператор ошибся) → срабатывает как обычный approve и продвигает чужой гейт | Низ | Сред | `handle_confirm_deploy` гардит `stage == "deploy"`; иначе no-op с логом | TC-04 (+ ручная верификация) |
| R-5 | Кэш `get_project_states` закэширован до создания статуса «Confirm Deploy» → ключ не виден после конфигурации Plane | Сред | Сред (деплой не запускается) | После создания статуса в Plane — `reload_project_states(orch)` или штатный рестарт по стадии `deploy`; зафиксировано в `07-infra-requirements.md` | ручная верификация |
| R-6 | Новый kwarg `confirm_deploy` ломает существующие вызовы `advance_stage` (launcher/reconciler/finalizer) | Низ | Выс | keyword-only с дефолтом `False`; все вызовы передают `finished_agent`; не-`deploy`/finished_agent≠None пути не затронуты | RG-01, AC-9 |
| R-7 | Регрессия идемпотентности Фазы B (двойной `Confirm Deploy`) | Низ | Сред | Внутренности `_handle_self_deploy_phase_b` (маркер `initiated`) не меняются; меняется только триггер | TC-08, AC-5 |
| R-8 | Реконсилятор F-1 на `deploy` (finished_agent=None) меняет поведение | Низ | Низ (улучшение) | Намеренно: раньше неявно мог войти в Фазу B, теперь → no-op. Прод-деплой инициируется только явным `Confirm Deploy`. Документировано в ADR/README | RG-01 |
| R-9 | Несовпадение имени статуса в Plane и `_PLANE_NAME_TO_KEY` (регистр/пробел) → ключ не резолвится | Сред | Сред (деплой не запускается, fail-closed) | Точное имя «Confirm Deploy» (case-sensitive) — требование среды в `07-infra-requirements.md`; маппинг ровно этой строкой | TC-01, TC-02 |
## Сводный вывод
Все риски — низкого/среднего остаточного уровня после митигаций. Доминирующий
класс — **fail-closed**: любая неполнота конфигурации (нет статуса, протухший кэш,
недоступный API) приводит к «деплой не запускается», а не к «деплой запускается
вслепую» или к крашу. Контракты конвейера (`STAGE_TRANSITIONS`, `QG_CHECKS`,
`check_deploy_status`, Фазы A/C, merge-gate, схема БД) не затрагиваются, поэтому
поверхность регрессии ограничена тремя модулями (`plane_sync.py`,
`webhooks/plane.py`, `stage_engine.py`).

View File

@@ -0,0 +1,59 @@
---
type: review
work_item_id: ORCH-059
verdict: APPROVED
version: 1
---
# Review ORCH-059
## Summary
Выделенный Plane-статус «Confirm Deploy» как единственный триггер Фазы B прод-деплоя
self-hosting; `Approved` на стадии `deploy` становится детерминированным no-op. Реализация
точно соответствует ТЗ (TRZ-1..6), ADR-001 и критериям приёмки (AC-1..10). Четыре точечные
правки в трёх модулях (`plane_sync.py`, `webhooks/plane.py`, `stage_engine.py`), без изменения
контрактов (`STAGE_TRANSITIONS`, `QG_CHECKS`, `check_deploy_status`, Фазы A/C, merge-gate, схема
БД). Документация обновлена в том же PR. `pytest tests/ -q` — 763 passed.
## Соответствие ТЗ и ADR
- **TRZ-1 / AC-1** — `"Confirm Deploy" → "confirm_deploy"` добавлен в `_PLANE_NAME_TO_KEY`;
намеренно отсутствует в `_DEFAULT_STATES` → fail-closed. Покрыто `test_tc01/tc02`.
- **TRZ-2 / AC-2** — `handle_confirm_deploy` (гард `stage=="deploy"`) →
`_try_advance_stage(..., confirm_deploy=True)` → Фаза B. Покрыто `test_tc04/tc07/tc10`.
- **TRZ-3 / AC-3** — `Approved` на `deploy`: ранний возврат ДО Quality Gate с
`note="approved-on-deploy-noop"`, без `initiate_deploy`, без ложного отката БАГ-8.
Покрыто `test_tc05/tc07_approved_without_confirm_is_noop/tc11`.
- **TRZ-4 / AC-4** — `handle_verdict(approved=True)` не тронут; approve на `analysis`
продвигает конвейер. Покрыто `test_tc06_approved_on_analysis_still_advances`.
- **AC-5** — идемпотентность повторного «Confirm Deploy» (`self-deploy-already-initiated`).
Покрыто `test_tc08`, `test_tc06_approved_calls_prod_hook_exactly_once`.
- **TRZ-5 / AC-6** — CTA Фазы A (Plane-коммент + Telegram) просит «Confirm Deploy» и явно
отмечает, что «Approved» прод-деплой не запускает. Покрыто `test_tc09`.
- **TRZ-1 / AC-7** — доступ через `.get("confirm_deploy")`, отсутствие статуса → ветка не
активируется, без `KeyError`. Покрыто `test_tc03` (API недоступен / статуса нет на доске).
- **TRZ-6 / AC-8** — условность через `self_deploy.self_deploy_applies`; не-self репо без
изменений. Покрыто `test_tc12`.
- **AC-9** — контракты и схема БД не изменены; 763 теста зелёные.
## Findings
### P0 — Blocker
- нет
### P1 — Must fix
- нет
### P2 — Should fix
- нет
## Документация
Обновлено в том же PR (AC-10 выполнен):
- `CLAUDE.md` — раздел self-hosting: прод-деплой только через «Confirm Deploy», `Approved` = no-op.
- `docs/architecture/README.md` — секция ORCH-036 уточнена + добавлена подсекция ORCH-059
(статус-триггер «Confirm Deploy»), запись в перечне статусов доработок.
- `CHANGELOG.md` — запись ORCH-059 в `[Unreleased] / Added`.
- ADR `docs/work-items/ORCH-059/06-adr/ADR-001-confirm-deploy-status.md` — заведён, отражает
реализацию (4 правки, fail-closed, рассмотренные альтернативы).
- `07-infra-requirements.md` — эксплуатационное предусловие (создать статус доски + сброс кэша).
Документация консистентна с кодом; golden-source инвариант соблюдён.

View File

@@ -0,0 +1,71 @@
---
type: test-report
work_item_id: ORCH-059
result: PASS
---
# Test Report — ORCH-059
Выделенный Plane-статус «Confirm Deploy» как единственный триггер Фазы B прод-деплоя
self-hosting; `Approved` на стадии `deploy` — детерминированный no-op.
## Окружение
- Python: 3.12.13
- pytest: 8.3.3
- Prod orchestrator (8500): `/health``{"status":"ok"}`
- Дата: 2026-06-07
## Результаты (контракт-тесты `04-test-plan.yaml`)
| TC ID | Описание | Тест | Результат |
|-------|----------|------|-----------|
| TC-01 | `_PLANE_NAME_TO_KEY`: `'Confirm Deploy' → 'confirm_deploy'` | test_tc01_confirm_deploy_name_to_key_mapping; test_tc01_confirm_deploy_not_in_default_states | PASS |
| TC-02 | `get_project_states` ORCH резолвит непустой UUID под `confirm_deploy`, ≠ `approved` | test_tc02_get_project_states_resolves_confirm_deploy | PASS |
| TC-03 | Fail-closed при отсутствии статуса (API недоступен / нет на доске) — без исключения | test_tc03_fail_closed_when_api_unreachable; test_tc03_fail_closed_when_status_not_on_board | PASS |
| TC-04 | `handle_issue_updated`: `Confirm Deploy` на `deploy` → путь Фазы B | test_tc04_confirm_deploy_routes_phase_b; test_tc04b_confirm_deploy_off_deploy_stage_is_noop | PASS |
| TC-05 | `Approved` на `deploy` НЕ вызывает `initiate_deploy` | test_tc05_approved_on_deploy_does_not_initiate | PASS |
| TC-06 | `Approved` на `analysis` по-прежнему продвигает → architecture | test_tc06_approved_on_analysis_still_advances | PASS |
| TC-07 | stage_engine: Фаза B только по confirm-deploy; `Approved` → no-op | test_tc07_confirm_deploy_initiates; test_tc07_approved_without_confirm_is_noop | PASS |
| TC-08 | Идемпотентность: повтор `Confirm Deploy` при маркере `initiated` → no-op | test_tc08_idempotent_repeat_confirm_deploy | PASS |
| TC-09 | CTA Фазы A содержит «Confirm Deploy», не предлагает «Approved» как триггер | test_tc09_phase_a_cta_requests_confirm_deploy | PASS |
| TC-10 | E2E: `Confirm Deploy``initiate_deploy` вызван, finalizer поставлен, маркер записан | test_tc10_confirm_deploy_e2e_initiates | PASS |
| TC-11 | E2E: `Approved` → деплой НЕ инициирован, задача остаётся на `deploy` | test_tc11_approved_e2e_noop | PASS |
| TC-12 | Условность: не-self репо verdict-статусы не меняют поведение деплоя | test_tc12_non_self_repo_unaffected | PASS |
| RG-01 | Полный регресс зелёный; STAGE_TRANSITIONS / QG_CHECKS без изменений | tests/ (763 passed) | PASS |
Все 16 целевых тестов ORCH-059 (TC-01..TC-12) — PASS.
## Сопоставление с критериями приёмки (`03-acceptance-criteria.md`)
| AC | Покрытие | Результат |
|----|----------|-----------|
| AC-1 Статус резолвится | TC-01, TC-02 | PASS |
| AC-2 Confirm Deploy на `deploy` → Фаза B | TC-04, TC-07, TC-10 | PASS |
| AC-3 Approved на `deploy` НЕ деплоит | TC-05, TC-07, TC-11 | PASS |
| AC-4 Approved на `analysis` без регрессии | TC-06 | PASS |
| AC-5 Идемпотентность Фазы B | TC-08 | PASS |
| AC-6 CTA Фазы A просит Confirm Deploy | TC-09 | PASS |
| AC-7 Fail-closed без статуса | TC-03 | PASS |
| AC-8 Условность для не-self | TC-12 | PASS |
| AC-9 Инварианты, pytest зелёный | RG-01 (763 passed) | PASS |
| AC-10 Документация обновлена | проверено reviewer (12-review.md, APPROVED) | PASS |
## Smoke test API (prod 8500)
- `GET /health``{"status":"ok","service":"orchestrator"}`
- `GET /status` → 200, активные задачи отдаются (вкл. ORCH-059 на `testing`)
- `GET /queue` → 200, counts + resilience + reconcile + reaper + post_deploy
## Вывод pytest
```
======================= 763 passed, 1 warning in 15.45s ========================
```
Целевой набор ORCH-059:
```
======================== 16 passed, 1 warning in 0.75s =========================
```
(1 warning — PydanticDeprecatedSince20 в `src/config.py`, не относится к ORCH-059.)
## Итог
**PASS** — все контракт-тесты (TC-01..TC-12) и регресс (763 passed) зелёные,
критерии приёмки AC-1..AC-10 покрыты, smoke API OK. Задача готова к стадии
deploy-staging.

View File

@@ -0,0 +1,12 @@
---
deploy_status: SUCCESS
work_item: ORCH-059
hook_exit_code: 0
deployed_by: deploy-finalizer
---
# Deploy log — ORCH-036 executable self-deploy
Прод-деплой завершён хост-хуком с exit-code `0` -> `deploy_status: SUCCESS`.
Вердикт зафиксирован детерминированным finalizer'ом (Фаза C), не LLM.

View File

@@ -0,0 +1,14 @@
---
post_deploy_status: HEALTHY
action_taken: NONE
work_item: ORCH-059
window_s: 900
checks_total: 30
checks_failed: 0
---
# Post-deploy log — ORCH-021 post-deploy monitor
Наблюдение прода завершено: `post_deploy_status: HEALTHY`, `action_taken: NONE`.
Окно наблюдения: 900s; опросов всего: 30, из них с провалом: 0.

View File

@@ -0,0 +1,7 @@
# Business Request: BUG: reconciler livelock — спам unblock done-задачи (ET-002)
Work Item ID: ORCH-068
## Description
TBD

View File

@@ -0,0 +1,52 @@
# BRD — ORCH-068: BUG reconciler livelock (спам unblock done-задачи)
## 1. Контекст и предыстория
Reconciler (`src/reconciler.py`, ORCH-053) — фоновый поток, который доигрывает пропущенные webhook-переходы. Ветвь **F-2 (plane-side)** опрашивает Plane per-project и реплеит In Progress / Approved / Rejected через штатные `handle_status_start` / `handle_verdict`. При фактической разблокировке вызывается `_note_unblock` → лог + Telegram.
ORCH-066 (мердж `feature/ORCH-066-plane`, прод 2026-06-07 ~22:16 UTC) ввела новую статусную модель Plane (маппинг стадия↔статус, новые имена `Done`, `Monitoring after Deploy` и т.п.). Это спровоцировало регрессию в F-2.
## 2. Проблема (бизнес-симптом)
С 22:17 UTC (рестарт прод-контейнера после деплоя ORCH-066) reconciler каждые ~120с шлёт в Telegram:
```
reconciler: ET-002 done разблокирована (потерян webhook)
```
для задачи **ET-002** (enduro-trails), которая в Done с 2026-05-21. На момент анализа — 191+ сообщений подряд, поток не прекращается (ночной спам, найден Славой 2026-06-08 01:22 UTC).
**Ключевой факт:** ET-002 полностью синхронизирована — БД `stage=done`, Plane `state=Done`. Reconciler обязан молчать (инвариант «silence when in sync», AC-9/AC-10 из ORCH-053), но шлёт уведомления вхолостую.
## 3. Диагностика (проведена, root cause найден)
1. **Деньги/токены НЕ тратятся:** `jobs` / `agent_runs` за 4ч пусты; `advance_stage` для done = no-op; `handle_verdict` для done-задачи ничего не меняет. Это «дешёвый», но шумный и подрывающий доверие баг (livelock + ложный alert-fatigue).
2. **Механизм:**
- `_reconcile_plane_project` (`src/reconciler.py` ~241) тянет `list_issues_by_state(pid, [in_progress, approved, rejected])`.
- На enduro-trails статусы «схлопнуты»: после ORCH-066 терминальный `Done` алиасится под UUID `approved` (см. ниже п.4) → ET-002 (Plane=Done) **попадает** в actionable-выборку.
- В `_reconcile_plane_issue` (~295) срабатывает ветка `new_state == approved and task is not None``handle_verdict(approved)` (no-op, задача уже done) **+ безусловный `_note_unblock`**.
- `_note_unblock` (~317) вызывается **сразу после `_dispatch`, не проверяя фактическое изменение состояния** — хотя его docstring обещает «fires only on an actual state change, never per idle tick». Инвариант нарушен.
3. **Два независимых дефекта складываются:**
- **D1 (выборка):** терминальные статусы (`Done`/`Cancelled`) не исключены из actionable-набора F-2; на «схлопывающих» проектах Done не отличается от approved по голому UUID.
- **D2 (нотификация):** `_note_unblock` срабатывает безусловно после no-op dispatch, а не только при подтверждённом state change.
4. **Почему `get_project_states` схлопывает:** функция строит маппинг по *именам* статусов из Plane API, затем недостающие ключи добивает из `_DEFAULT_STATES` (enduro-значения). После ORCH-066 набор статусов enduro изменился — голый UUID перестал однозначно различать `Done` (completed-группа) и `approved` (review). Группа состояния (`state.group`) при этом различает их корректно, но в коде не используется.
## 4. Связанный баг (BUG КЭША СТАТУСОВ, найден 2026-06-07 при деплое ORCH-066)
`_STATES_CACHE` (`src/plane_sync.py` ~134) кэширует статусы Plane на **весь lifetime процесса**. После создания нового Plane-статуса (напр. `Confirm Deploy`) боевой процесс держит устаревший набор → webhook на новый статус даёт «no pipeline action» (Phase B не триггерится). Лечилось только рестартом орка. Примитив сброса уже есть — `reload_project_states()` — но он нигде не вызывается автоматически.
Оба бага — следствие хрупкости статусной модели после ORCH-066. **Решение:** вести их в одном work item (см. scope ниже), окончательное разделение — на усмотрение архитектора.
## 5. Цели (Goals)
- G1. Reconciler НЕ шлёт «разблокирована» для синхронизированной done/cancelled задачи (восстановить инвариант silence-when-in-sync).
- G2. `_note_unblock` срабатывает **только** при реальном state change (соблюдён AC-9/AC-10).
- G3. Дедуп: нет повторного спама по той же задаче без изменения её состояния.
- G4. Корректное различение терминальных (`Done`/`Cancelled`) и review-статусов (`approved`/`rejected`) даже на проектах, «схлопывающих» их по UUID — на всех проектах (enduro И orchestrator).
- G5 (secondary). Устаревший `_STATES_CACHE` обновляется без рестарта процесса (TTL / flush-on-unknown / endpoint).
## 6. Не-цели (Out of scope)
- N1. Менять source-of-truth: ориентир F-2 на Plane **остаётся** корректным по дизайну (таблица `tasks` без status-колонки; статусы двигает человек в Plane). Идею F-2 НЕ переписываем — баг в маппинге/нотификации, не в концепции.
- N2. Менять реестры `STAGE_TRANSITIONS` / `QG_CHECKS`, схему БД, контракты гейтов.
- N3. Менять поведение F-1 (gate-side) и F-3.
- N4. Полный авто-approve деплоя (ORCH-54).
## 7. Затронутые стороны
- **Все проекты на одном инстансе** (enduro-trails + orchestrator, общая БД/очередь) — баг проявился на ET-002, но фикс выборки терминалов обязан быть проектно-независимым.
- **Self-hosting:** правка идёт в работающий прод-инструмент → обязательна страховка staging (8501), запрет на рестарт прод-контейнера в рамках задачи.
## 8. Критерий успеха (бизнес)
Тик reconciler для синхронизированной done/cancelled задачи = **0 уведомлений, 0 jobs, 0 токенов**. Telegram-спам прекращён. Легитимная разблокировка (реально потерянный approved/in_progress webhook) по-прежнему работает (нет регресса F-2).

View File

@@ -0,0 +1,68 @@
# ТЗ — ORCH-068: устранить livelock reconciler F-2 + спам unblock done-задачи
> Документ описывает ТРЕБОВАНИЯ к изменению поведения (что и где), а не выбор реализации. Конкретный механизм (state.group vs явный allowlist терминалов; TTL vs flush-on-unknown) — решение архитектора в ADR.
## 1. Затронутые модули `src/`
| Модуль | Роль в баге | Требуемое изменение |
|--------|-------------|---------------------|
| `src/reconciler.py` | F-2 `_reconcile_plane_project` / `_reconcile_plane_issue` / `_note_unblock` | Исключить терминальные статусы из actionable-выборки; `_note_unblock` только при подтверждённом state change; дедуп. |
| `src/plane_sync.py` | `get_project_states`, `list_issues_by_state`, `_STATES_CACHE` | Дать способ различать терминальные/review статусы (группа состояния); устранить вечно-устаревший кэш (TTL/flush). |
| `src/config.py` | флаги | (если нужны) новые kill-switch/настройки TTL — с дефолтами, не меняющими прод-инварианты. |
| `src/main.py` (`/queue`) | наблюдаемость | (опц.) отразить дедуп/skip-терминалов в снимке `reconcile`. |
**НЕ трогать:** `src/stages.py` (`STAGE_TRANSITIONS`), `src/qg/checks.py` (`QG_CHECKS`), схему БД, контракты `handle_status_start` / `handle_verdict`, F-1 (`reconcile_gate_once`), F-3.
## 2. Требования к F-2 (src/reconciler.py)
### TR-1. Исключить терминальные статусы из actionable-выборки
`_reconcile_plane_project` НЕ должен подавать задачи в терминальном Plane-статусе (`Done` и прочие completed-группы, `Cancelled`) ни в `list_issues_by_state`, ни в последующее сравнение веток.
- Требование проектно-независимое: работает на enduro И orchestrator, независимо от того, «схлопывает» ли проект статусы по UUID.
- Различение `Done`/`Cancelled` (completed) от `approved`/`rejected` (review) НЕ должно опираться только на голый UUID, если проект их алиасит. Допустимый ориентир — группа состояния Plane (`state.group`: `completed`/`started`/`unstarted`/`backlog`/`cancelled`) либо явный набор логических ключей терминалов. Выбор — за архитектором.
### TR-2. `_note_unblock` — только при реальном state change
`_note_unblock` (лог + Telegram + инкремент `unblocked_total`) ВЫЗЫВАЕТСЯ ТОЛЬКО когда диспетчеризованный обработчик фактически изменил состояние задачи (advance / replayed transition, реально сдвинувший стадию). No-op dispatch (задача уже в целевом состоянии) → нотификация НЕ отправляется.
- Сейчас `_dispatch` (`asyncio.run(coro_fn(...))`) отбрасывает результат, а `_note_unblock` зовётся безусловно. Требуется механизм подтверждения изменения (напр. сравнение стадии задачи до/после dispatch через `get_task_by_plane_id`, либо проброс сигнала из обработчика). Конкретику выбирает архитектор; контракт обработчиков `handle_*` менять НЕ обязательно (предпочтительно сравнение состояния до/после на стороне reconciler).
- Восстановить соответствие docstring `_note_unblock`: «Fires only on an actual state change … never per idle tick».
### TR-3. Дедуп / идемпотентность нотификаций
Reconciler НЕ должен слать повторное уведомление о той же задаче, если её состояние не менялось с прошлого тика. TR-1+TR-2 закрывают основной кейс (done более не входит в выборку и не нотифицируется); TR-3 — дополнительная страховка (best-effort), чтобы любой будущий no-op путь не дал повторного спама.
## 3. Требования к статус-кэшу (src/plane_sync.py) — secondary
### TR-4. Устаревший `_STATES_CACHE` обновляется без рестарта
После появления нового Plane-статуса процесс не должен бесконечно держать устаревший набор. Допустимые подходы (выбор архитектора, можно комбинировать):
- TTL на запись кэша (напр. `ORCH_PLANE_STATES_TTL_S`, дефолт разумный, 0/неуст. = прежнее поведение для совместимости);
- flush-on-unknown: при детекте неизвестного статуса в вебхуке/реконсилере — вызвать существующий `reload_project_states(pid)` и перезапросить;
- админ-эндпоинт/сигнал для ручного flush без рестарта.
`reload_project_states()` уже существует — переиспользовать как примитив сброса, новую логику сброса не дублировать.
## 4. Изменения API
- Новых обязательных endpoint'ов нет.
- Опционально (TR-4, на усмотрение архитектора): admin-эндпоинт сброса кэша статусов (напр. `POST /admin/plane-states/reload`) — если выбран этот вариант flush. Должен быть защищён/идемпотентен; документировать в README таблице API.
- Снимок `GET /queue` (блок `reconcile`) — без ломающих изменений; допустимо добавить поля наблюдаемости (skip-терминалов / dedup-счётчик).
## 5. Изменения схемы БД
**Нет.** Дедуп TR-3 реализуется in-memory (best-effort, как существующие счётчики `unblocked_total`/`last_unblocked`, AC-11 ORCH-053 допускает их сброс при рестарте) либо через сравнение живого состояния Plane/БД. Миграции не требуются.
## 6. Требования к новым QG checks
Нет. Реестр `QG_CHECKS` не меняется.
## 7. Инварианты (обязаны сохраниться)
- INV-1. Source of truth F-2 — Plane (НЕ меняем).
- INV-2. never-raise на единицу работы (per-issue / per-project / per-tick) сохранён.
- INV-3. Kill-switch `ORCH_RECONCILE_ENABLED` (+ `ORCH_RECONCILE_PLANE_ENABLED` гасит только F-2) — работают.
- INV-4. F-1 / F-3 поведение не изменено.
- INV-5. 0 jobs / 0 токенов для синхронизированных задач (как сейчас) сохранено.
- INV-6. Легитимная разблокировка реально-потерянного approved/in_progress webhook продолжает работать (нет регресса F-2).
- INV-7. Self-hosting: тик reconciler НИКОГДА не рестартит/не роняет прод-контейнер.
## 8. Артефакты pipeline, которые надо обновить в ТОМ ЖЕ PR
- `CLAUDE.md` — если меняется наблюдаемое поведение reconciler/кэша (раздел про reconciler/правила).
- `docs/architecture/README.md` — секция «Reconciler … (ORCH-053)»: уточнить исключение терминалов + дедуп; при TR-4 — секция «Plane Sync».
- `docs/architecture/adr/adr-0007-reconciler.md` (или новый per-WI ADR `docs/work-items/ORCH-068/06-adr/ADR-001-…`) — зафиксировать решение по терминалам/группе состояния и по кэшу.
- `CHANGELOG.md` — запись о фиксе (`fix:`).
- `.env.example` / `.env.staging` — если введён новый флаг (TTL/kill-switch).
## 9. Замечания по приёмке/тестированию
- Регресс-тест ОБЯЗАН покрывать: задача в `Done` (синхронизирована) → тик F-2 = 0 нотификаций, на enduro И orchestrator проектах (terminal не зависит от алиасинга).
- Тест НЕ должен делать реальных сетевых вызовов — мокать `list_issues_by_state` / `get_project_states` / `send_telegram` / `_dispatch` (handle_*).

View File

@@ -0,0 +1,84 @@
# Критерии приёмки — ORCH-068
Формат: каждый AC имеет чёткое условие PASS/FAIL. Задача принимается только при ВСЕХ PASS.
## Основное (P0) — livelock / спам
### AC-1. Синхронизированная done-задача → тишина
**Дано:** задача в Plane `state=Done`, БД `stage=done`, активных job нет.
**Когда:** выполняется один тик F-2 (`reconcile_plane_once` / `_reconcile_plane_project`).
- PASS: `_note_unblock` НЕ вызван; `send_telegram` НЕ вызван; `unblocked_total` не изменился; создано 0 jobs.
- FAIL: любое уведомление/лог «разблокирована»/инкремент счётчика для этой задачи.
### AC-2. Терминалы исключены из actionable-выборки
**Дано:** проект, где `Done` (и/или `Cancelled`) по UUID совпадает/«схлопнут» с `approved`/`rejected`.
**Когда:** `_reconcile_plane_project` формирует набор и обходит issues.
- PASS: issue в терминальном статусе (completed-группа / `Cancelled`) НЕ попадает ни в одну из веток `in_progress/approved/rejected`; для неё F-2 — no-op silence.
- FAIL: терминальная issue заходит в ветку approved/rejected/in_progress.
### AC-3. `_note_unblock` только при реальном state change
**Дано:** dispatch обработчика (`handle_verdict`/`handle_status_start`) фактически НЕ изменил стадию задачи (no-op, задача уже в целевом состоянии).
- PASS: `_note_unblock` НЕ вызван.
- FAIL: `_note_unblock` вызван после no-op dispatch.
### AC-4. Дедуп по неизменному состоянию
**Дано:** две последовательные итерации тика по одной и той же синхронизированной задаче, состояние между тиками не менялось.
- PASS: суммарно 0 повторных уведомлений по этой задаче.
- FAIL: повторное уведомление на втором тике без изменения состояния.
### AC-5. Нет регресса легитимной разблокировки F-2
**Дано:** задача, у которой Plane=`Approved`, а локальная стадия НЕ продвинулась (реально потерянный verdict-webhook), grace выдержан, активных job нет.
**Когда:** тик F-2.
- PASS: `handle_verdict(approved)` доигран; задача продвинута; `_note_unblock` вызван РОВНО один раз (реальный state change).
- FAIL: задача не продвинута ИЛИ нотификация не отправлена ИЛИ отправлена многократно.
### AC-6. Аналогично для in_progress-старта и rejected-отката
- PASS: потерянный `In Progress` (task is None) → старт пайплайна + 1 unblock; потерянный `Rejected` → откат + 1 unblock — оба только при реальном изменении.
- FAIL: ложный/повторный unblock или отсутствие легитимного.
## Инварианты (P0)
### AC-7. Деньги/ресурсы не тратятся на синхронизированные задачи
- PASS: 0 jobs, 0 agent_runs, 0 токенов для done/cancelled задач (как до бага).
- FAIL: любой созданный job/agent_run.
### AC-8. never-raise сохранён
**Дано:** `list_issues_by_state` / `get_project_states` / `_dispatch` / `send_telegram` бросают исключение.
- PASS: тик не падает; ошибка изолирована (per-issue / per-project), логируется; остальные задачи обрабатываются.
- FAIL: непойманное исключение роняет тик/поток.
### AC-9. Kill-switch'и работают
- PASS: `ORCH_RECONCILE_ENABLED=false` → F-1 и F-2 не выполняются; `ORCH_RECONCILE_PLANE_ENABLED=false` → F-2 не выполняется, F-1 работает.
- FAIL: любой свитч не гасит соответствующую ветку.
### AC-10. F-1 / F-3 / реестры / схема БД не изменены
- PASS: `STAGE_TRANSITIONS`, `QG_CHECKS`, схема БД, контракты `handle_*`, поведение F-1/F-3 — без изменений (diff не затрагивает).
- FAIL: любое изменение перечисленного.
### AC-11. Self-hosting безопасность
- PASS: ни один путь reconciler не рестартит/не роняет прод-контейнер `orchestrator`.
- FAIL: обратное.
## Secondary (P1) — кэш статусов
### AC-12. Устаревший `_STATES_CACHE` обновляется без рестарта
**Дано:** после старта процесса в Plane появился новый статус (его UUID отсутствует в кэше).
**Когда:** срабатывает выбранный механизм (TTL истёк / flush-on-unknown / ручной flush).
- PASS: следующий `get_project_states` возвращает свежий набор, включающий новый статус; webhook на новый статус даёт корректное pipeline-действие БЕЗ рестарта.
- FAIL: процесс продолжает отдавать устаревший набор → «no pipeline action».
### AC-13. Совместимость кэша по умолчанию
- PASS: при дефолтных настройках (TTL не задан / flush не сработал) поведение `get_project_states` не регрессирует; enduro по-прежнему получает свои UUID, fallback на `_DEFAULT_STATES` при недоступности API сохранён.
- FAIL: регресс резолва статусов или потеря fallback.
## Документация (P0 по правилам проекта)
### AC-14. Документация обновлена в том же PR
- PASS: обновлены применимые из {`docs/architecture/README.md` (Reconciler/Plane Sync), ADR, `CHANGELOG.md`, `CLAUDE.md`, `.env.example`}; reviewer подтверждает.
- FAIL: поведение изменено, доки/ADR/CHANGELOG не обновлены.
## Тесты (P0)
### AC-15. `pytest tests/ -q` зелёный
- PASS: весь набор тестов проходит; добавлены регресс-тесты из `04-test-plan.yaml`, включая done→silence на enduro и orchestrator.
- FAIL: любой красный тест или отсутствие регресс-теста на основной баг.

View File

@@ -0,0 +1,122 @@
work_item: ORCH-068
description: >
Регрессионные и модульные тесты на устранение livelock reconciler F-2
(спам _note_unblock для синхронизированной done-задачи) и связанного бага
кэша статусов. Все тесты офлайн: Plane API / Telegram / dispatch мокаются.
Целевые модули: src/reconciler.py, src/plane_sync.py.
tests:
# ---------- P0: основной баг (livelock / спам) ----------
- id: TC-01
type: unit
description: >
Синхронизированная done-задача (Plane=Done, БД=done, нет активных job):
один тик F-2 -> _note_unblock НЕ вызван, send_telegram НЕ вызван,
unblocked_total не изменился, 0 jobs. (AC-1, AC-7)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-02
type: unit
description: >
Терминал «схлопнут» с approved по UUID: issue в Done с тем же UUID, что и
approved-набор, НЕ заходит ни в одну ветку in_progress/approved/rejected
(silence). Проверка проектно-независимого исключения терминалов. (AC-2)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-03
type: unit
description: >
Cancelled терминал также исключён из actionable-выборки -> тик = silence,
0 нотификаций. (AC-2)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-04
type: unit
description: >
_note_unblock не вызывается после no-op dispatch: handle_verdict не сдвинул
стадию (задача уже в целевом состоянии) -> 0 нотификаций. (AC-3)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-05
type: unit
description: >
Дедуп: два последовательных тика по одной синхронизированной задаче без
изменения состояния -> суммарно 0 повторных уведомлений. (AC-4)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-06
type: unit
description: >
Нет регресса: Plane=Approved, локальная стадия не продвинута, grace выдержан,
нет активных job -> handle_verdict доигран, задача продвинута, _note_unblock
вызван РОВНО один раз. (AC-5)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-07
type: unit
description: >
Нет регресса для in_progress (task is None -> старт пайплайна, 1 unblock) и
rejected (task существует -> откат, 1 unblock), оба только при реальном
изменении состояния. (AC-6)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-08
type: unit
description: >
never-raise: list_issues_by_state / get_project_states / _dispatch /
send_telegram бросают исключение -> тик не падает, ошибка изолирована и
залогирована, прочие issues обработаны. (AC-8)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-09
type: unit
description: >
Kill-switch: reconcile_enabled=False -> F-2 не выполняется;
reconcile_plane_enabled=False -> F-2 не выполняется, F-1 не затронут. (AC-9)
module: tests/test_reconciler_plane.py
expected: PASS
- id: TC-10
type: integration
description: >
End-to-end F-2 на двух проектах (enduro И orchestrator): задача в Done на
каждом -> тик reconcile_plane_once = 0 нотификаций / 0 jobs на обоих,
независимо от алиасинга статусов проекта. Главный регресс-тест бага. (AC-1, AC-2)
module: tests/test_reconciler_plane.py
expected: PASS
# ---------- P1: связанный баг кэша статусов ----------
- id: TC-11
type: unit
description: >
Устаревший _STATES_CACHE обновляется без рестарта: после появления нового
статуса срабатывает выбранный механизм (TTL/flush) -> следующий
get_project_states содержит новый статус. (AC-12)
module: tests/test_plane_states_cache.py
expected: PASS
- id: TC-12
type: unit
description: >
Совместимость по умолчанию: при дефолтных настройках get_project_states не
регрессирует — enduro отдаёт свои UUID, fallback на _DEFAULT_STATES при
недоступности API сохранён. (AC-13)
module: tests/test_plane_states_cache.py
expected: PASS
# ---------- P0: общий прогон ----------
- id: TC-13
type: integration
description: >
Полный набор pytest tests/ -q зелёный (нет регресса в reconciler/plane/qg/
stage_engine). (AC-15)
module: tests/
expected: PASS

View File

@@ -0,0 +1,162 @@
# ADR-001 (ORCH-068): Исключение терминалов из F-2 по группе состояния + подтверждённый unblock + TTL кэша статусов
- **Статус:** Accepted
- **Дата:** 2026-06-08
- **Задача:** ORCH-068 (BUG: reconciler livelock — спам «разблокирована» по синхронизированной done-задаче)
- **Сквозной ADR:** уточняет [adr-0007-reconciler.md](../../../architecture/adr/adr-0007-reconciler.md) (F-2) — реестры/схема НЕ меняются
- **Связанные:** ORCH-053 (reconciler F-2), ORCH-066 (новая статусная модель Plane — триггер регрессии), ORCH-060 (F-1 пред-гарды), ORCH-10 (`get_project_states`)
## Контекст
Reconciler F-2 (`src/reconciler.py`, `_reconcile_plane_project` / `_reconcile_plane_issue`)
опрашивает Plane per-project и доигрывает потерянные webhook-переходы через штатные
`handle_status_start` / `handle_verdict`. После мерджа ORCH-066 (новая статусная модель
Plane) на проде с 22:17 UTC reconciler каждые ~120с слал в Telegram
`reconciler: ET-002 done разблокирована (потерян webhook)` для задачи ET-002, которая
полностью синхронизирована (БД `stage=done`, Plane `state=Done` с 2026-05-21). 191+
сообщений за ночь — livelock без advance/jobs/токенов, но подрывающий доверие alert-fatigue.
Диагностика (BRD §3) выявила **два независимых, складывающихся дефекта**:
- **D1 (выборка):** F-2 различает actionable-статусы по **голому UUID**
(`in_progress`/`approved`/`rejected`). `get_project_states` строит маппинг по *именам*
статусов, недостающие ключи добивает из `_DEFAULT_STATES` (enduro-значения). После
ORCH-066 набор имён enduro изменился → терминальный `Done` перестал однозначно
отличаться от `approved` по UUID, и ET-002 (Plane=Done) **попала** в actionable-набор
ветки `approved`. Терминальные статусы (`Done`/`Cancelled`) нигде не исключаются из F-2.
- **D2 (нотификация):** `_note_unblock` вызывается **безусловно сразу после `_dispatch`**,
не проверяя, изменил ли обработчик реально состояние задачи. `handle_verdict(approved)`
для уже-`done` задачи — no-op, но нотификация всё равно уходит. Это прямо нарушает
собственный docstring `_note_unblock` («fires only on an actual state change, never per
idle tick») и инвариант silence-when-in-sync (AC-9/AC-10 ORCH-053).
Связанный secondary-баг (BRD §4): `_STATES_CACHE` (`src/plane_sync.py`) кэширует статусы
на **весь lifetime процесса**. После появления нового Plane-статуса боевой процесс держит
устаревший набор → webhook на новый статус даёт «no pipeline action», лечилось только
рестартом орка. Примитив сброса `reload_project_states()` уже есть, но автоматически не
вызывается.
Ограничения (из ТЗ, обязаны сохраниться): источник истины F-2 — Plane (не переписываем);
НЕ трогать `STAGE_TRANSITIONS` / `QG_CHECKS` / схему БД / контракты `handle_*` / F-1 / F-3;
never-raise per unit of work; kill-switch'и; 0 jobs/0 токенов для синхронизированных задач;
self-hosting — reconciler НИКОГДА не рестартит прод-контейнер.
## Решение
Чиним **оба** дефекта независимыми слоями (defense in depth, как принято в проекте —
ORCH-058) плюс TTL для кэша. Все правки локальны в `src/reconciler.py` и
`src/plane_sync.py`; реестры, схема БД и контракты обработчиков не меняются.
### Слой D1 — исключение терминалов по ГРУППЕ состояния (TR-1, AC-2)
Различаем терминальные (`completed`/`cancelled`) и review/work-статусы по **группе
состояния Plane** (`state.group ∈ {backlog, unstarted, started, completed, cancelled}`),
а НЕ по голому UUID. Группа — авторитетный, проектно-независимый дискриминатор: она
корректно различает `Done` (completed) и `approved` (started/review) даже когда проект
«схлопывает» их по UUID после переименований.
Механика (single API fetch, без новых сетевых вызовов):
- `/states/`-ответ Plane содержит для каждого статуса поле `group`. Расширяем кэш-запись
`_STATES_CACHE` так, чтобы из ОДНОГО запроса хранить и текущий `{logical_key → uuid}`,
и `{uuid → group}`. `get_project_states` сохраняет **прежнюю сигнатуру и форму возврата**
(`{logical_key: uuid}`) — обратная совместимость (AC-13). Добавляется sibling-аксессор
`get_project_state_groups(project_id) -> dict[uuid, group]` (или эквивалент), читающий ту
же кэш-запись.
- В `_reconcile_plane_issue` ДО выбора ветки: если группа `new_state`
{`completed`, `cancelled`} → **тишина** (return, no-op). Fallback, когда группа
недоступна (API не отдал `group` / fallback на `_DEFAULT_STATES`): исключать по логическим
ключам терминалов `{states.get("done"), states.get("cancelled")}`.
Терминал-исключение применяется **per-issue** (а не сужением `wanted`-набора
`list_issues_by_state`), потому что при UUID-алиасинге терминал может физически совпадать с
actionable-UUID в `wanted` — фильтрация по UUID его не отсечёт, а проверка группы отсечёт.
### Слой D2 — `_note_unblock` только при подтверждённом state change (TR-2, AC-3)
`_note_unblock` (лог + Telegram + `unblocked_total`) вызывается ТОЛЬКО когда диспетчеризованный
обработчик **фактически изменил состояние задачи**. Реализация — сравнение состояния
**до/после на стороне reconciler** (предпочтение ТЗ; контракты `handle_*` НЕ меняются):
- `approved`/`rejected` (task существует): захватить `stage_before` (из уже прочитанного
`task`), после `_dispatch` перечитать `get_task_by_plane_id(issue_id)``stage_after`;
`_note_unblock` только если `stage_after != stage_before`.
- `in_progress` + `task is None` (старт пайплайна): подтверждение = задача **появилась**
после dispatch (`get_task_by_plane_id` теперь не None).
No-op dispatch (задача уже в целевом состоянии) → 0 нотификаций. Восстанавливает соответствие
docstring и инвариант silence-when-in-sync.
### Слой TR-3 — дедуп нотификаций (страховка, AC-4)
In-memory best-effort guard: `{issue_id → last_unblocked_state_uuid}`. `_note_unblock` для
issue+state, уже отмеченного, подавляется. Сбрасывается при рестарте (допустимо — AC-11
ORCH-053, как `unblocked_total`/`last_unblocked`). D1+D2 закрывают основной кейс; TR-3 —
дополнительная сетка против любого будущего no-op-пути.
### Слой TR-4 — TTL кэша статусов (secondary, AC-12/AC-13)
Кэш-запись `_STATES_CACHE` хранит timestamp; `get_project_states` перезапрашивает API при
истечении `ORCH_PLANE_STATES_TTL_S`. Примитив инвалидации — существующий
`reload_project_states()` (не дублируем логику сброса). Новый флаг
`plane_states_ttl_s` (env `ORCH_PLANE_STATES_TTL_S`):
- дефолт **300** (5 мин) — устаревший набор самозалечивается без рестарта (G5);
- `0` — отключает TTL → строго прежний lifetime-кэш (escape hatch / strict back-compat).
Fallback на `_DEFAULT_STATES` при недоступности API сохранён без изменений; TTL-перезапрос
возвращает тот же корректный набор → не регресс (AC-13).
## Альтернативы
- **Явный allowlist логических ключей терминалов (`done`/`cancelled`) без группы** —
отклонён как primary: хрупок к будущим переименованиям/добавлению completed-статусов
(`Monitoring after Deploy` и т.п.) и к UUID-алиасингу. Оставлен как **fallback**, когда
`group` недоступен.
- **Сужение `wanted`-набора в `list_issues_by_state`** — недостаточно: при UUID-алиасинге
терминал совпадает с actionable-UUID и не отсекается фильтром по UUID. Нужна проверка
группы per-issue.
- **Проброс «changed»-сигнала из `handle_*`** — отклонён: меняет контракт обработчиков
(запрещено ТЗ N2). Выбрано сравнение до/после на стороне reconciler.
- **Флаг подавления нотификаций в `advance_stage`** — отклонён (как и в adr-0007):
трогает общий критический путь.
- **flush-on-unknown как primary для кэша** — допустимо ТЗ и дешевле, но недетерминирован
для юнит-теста (TC-11) и не лечит «тихий устаревший набор» без триггера-вебхука. Выбран
TTL (детерминированный, самозалечивающий); flush-on-unknown может быть добавлен позже как
комплемент, переиспользуя `reload_project_states`.
- **Admin-эндпоинт `POST /admin/plane-states/reload`** — отклонён в объёме (требует
ручного действия, не лечит автоматически); TTL покрывает G5 без нового API.
## Последствия
- **Плюсы:** livelock устранён двумя независимыми слоями; терминал-исключение
проектно-независимо (enduro И orchestrator), устойчиво к будущим переименованиям статусов;
`_note_unblock` снова соответствует своему контракту; устаревший кэш самозалечивается без
рестарта прода. Реестры/схема/контракты/F-1/F-3 не тронуты.
- **Минусы / плата:** один доп. accessor группы (тот же API-запрос, без новой сетевой
стоимости); TTL добавляет редкий перезапрос `/states/` (раз в 5 мин/проект); дедуп-словарь
— небольшая in-memory структура, неперсистентная (приемлемо).
- **Совместимость:** `get_project_states` форма возврата неизменна; `plane_states_ttl_s=0`
→ строго прежнее поведение кэша; `_DEFAULT_STATES`-fallback сохранён.
- **Self-hosting:** ни один путь не рестартит прод-контейнер (AC-11); правка
обязательно проходит staging-гейт (8501) перед прод-деплоем орка.
- **Наблюдаемость (опц.):** допустимо добавить в блок `reconcile` снимка `GET /queue`
счётчики `skipped_terminal` / `deduped` без ломающих изменений.
## Инварианты (подтверждение)
INV-1 источник истины F-2 = Plane — сохранён (правим маппинг/нотификацию, не концепцию).
INV-2 never-raise per-issue/-project/-tick — сохранён (новый guard в том же try-периметре).
INV-3 kill-switch'и `ORCH_RECONCILE_ENABLED` / `ORCH_RECONCILE_PLANE_ENABLED` — без изменений.
INV-4 F-1/F-3 — не тронуты. INV-5 0 jobs/0 токенов для done/cancelled — восстановлен.
INV-6 легитимная разблокировка реально-потерянного approved/in_progress — работает (D2
подтверждает реальный change, не подавляет его). INV-7 self-hosting — тик не рестартит прод.
## Объём изменений (для разработчика)
- `src/reconciler.py`: терминал-гард по группе + fallback в `_reconcile_plane_issue`;
before/after-сравнение стадии вокруг `_dispatch`; in-memory дедуп-словарь в `Reconciler`.
- `src/plane_sync.py`: кэш-запись с timestamp + `{uuid→group}`; `get_project_state_groups`;
TTL-логика в `get_project_states` (переиспользуя `reload_project_states`).
- `src/config.py`: флаг `plane_states_ttl_s` (env `ORCH_PLANE_STATES_TTL_S`, дефолт 300).
- `.env.example` / `.env.staging`: задокументировать `ORCH_PLANE_STATES_TTL_S`.
- Доки в ТОМ ЖЕ PR: `docs/architecture/README.md` (Reconciler/Plane Sync), `CHANGELOG.md`
(`fix:`), `CLAUDE.md` (при изменении наблюдаемого поведения), этот ADR.
- Тесты: `04-test-plan.yaml` (TC-01…TC-13), офлайн (мок Plane/Telegram/`_dispatch`).

View File

@@ -0,0 +1,17 @@
# Технические риски — ORCH-068
| ID | Риск | Вероятность | Влияние | Митигация |
|----|------|-------------|---------|-----------|
| R-1 | Plane `/states/` не отдаёт поле `group` (старая версия API / урезанный ответ) → терминал-исключение по группе не срабатывает | Низкая | Высокое (рецидив livelock) | Fallback на логические ключи терминалов `{done, cancelled}` при отсутствии `group`; never-raise → консервативная тишина при сбое резолва |
| R-2 | Over-exclusion: легитимная задача в started/review-группе ошибочно классифицирована как терминал → пропущена легитимная разблокировка (регресс INV-6) | Низкая | Среднее | Исключаем ТОЛЬКО группы `completed`/`cancelled`; `approved`/`rejected` относятся к started/unstarted → не задеты; регресс-тесты TC-06/TC-07 |
| R-3 | Гонка before/after: между `stage_before` и `stage_after` живой webhook двигает стадию → ложный `_note_unblock` | Очень низкая | Низкое | active-job guard + `max_concurrency=1` уже сериализуют; дедуп TR-3 подавляет повтор; ложный unblock безвреден (0 jobs/токенов) |
| R-4 | TTL `300s` провоцирует частые `/states/`-перезапросы при многих проектах | Низкая | Низкое | 1 запрос/проект/5 мин — пренебрежимо; `ORCH_PLANE_STATES_TTL_S=0` отключает TTL |
| R-5 | TTL-перезапрос в момент недоступности Plane → временный fallback на `_DEFAULT_STATES` (enduro) для не-enduro проекта | Низкая | Среднее | Поведение идентично текущему cold-cache fallback; самозалечивается следующим успешным запросом; не хуже статус-кво |
| R-6 | Дедуп-словарь растёт неограниченно (по issue_id) | Очень низкая | Низкое | Ключи — только реально разблокированные issue (редки); сбрасывается при рестарте; при необходимости — ограничить размер/LRU |
| R-7 | Изменение в `get_project_states` (кэш-запись) ломает прочих потребителей формы возврата | Низкая | Высокое | Внешняя сигнатура и форма `{logical_key: uuid}` сохранены; группа — отдельный accessor; покрыто TC-12 (совместимость по умолчанию) |
| R-8 | Self-hosting: правка в работающем прод-инструменте | — | Высокое | Обязательный staging-гейт (8501); запрет рестарта прод-контейнера в рамках задачи; INV-7 |
## Замечания
- Все правки локальны (`reconciler.py`, `plane_sync.py`, `config.py`); схема БД, реестры
`STAGE_TRANSITIONS`/`QG_CHECKS`, контракты `handle_*`, F-1/F-3 — не затронуты (AC-10).
- Тесты офлайн (мок Plane API / Telegram / `_dispatch`) — сетевых вызовов в CI нет.

View File

@@ -0,0 +1,47 @@
---
type: review
work_item_id: ORCH-068
verdict: APPROVED
version: 1
---
# Review ORCH-068
## Summary
Фикс livelock reconciler F-2 (спам `_note_unblock` по синхронизированной done-задаче после ORCH-066) реализован чисто и полностью по ТЗ/ADR. Два независимых слоя (D1 терминал-исключение по группе состояния + D2 подтверждённый state change) плюс TR-3 дедуп и TR-4 TTL кэша. Правки строго локальны в `src/reconciler.py` (F-2), `src/plane_sync.py`, `src/config.py`. Запрещённые ТЗ артефакты (`STAGE_TRANSITIONS`, `QG_CHECKS`, схема БД, контракты `handle_*`, F-1, F-3) не тронуты — diff не выходит за 3 файла `src/`. `pytest tests/ -q`**764 passed**.
## Соответствие ТЗ
- **TR-1** (исключить терминалы) ✅ — `_is_terminal_state` по `state.group ∈ {completed, cancelled}` с fallback на логические ключи `done`/`cancelled`; проверка per-issue (а не сужением `wanted`), что корректно для UUID-алиасинга.
- **TR-2** (`_note_unblock` только при реальном change) ✅ — `_stage_changed` (сравнение стадии до/после `_dispatch`), для in_progress-старта подтверждение = задача появилась; контракты `handle_*` не менялись.
- **TR-3** (дедуп) ✅ — in-memory guard `{issue_id → state_uuid}`, best-effort, сброс при рестарте (как `unblocked_total`).
- **TR-4** (TTL кэша) ✅ — `plane_states_ttl_s` (дефолт 300, `0`=lifetime), переиспользует `reload_project_states`; форма возврата `get_project_states` неизменна; при сбое перезапроса отдаётся stale-but-correct набор.
## Соответствие ADR
ADR-001 (terminal-exclusion-and-cache-ttl) реализован 1:1: группа как primary-дискриминатор, allowlist-fallback, before/after-сравнение на стороне reconciler, TTL с инвалидацией через существующий примитив. Сквозной adr-0007 дополнен корректной ссылкой. Все инварианты INV-1…INV-7 сохранены (источник истины Plane, never-raise per-issue, kill-switch'и, F-1/F-3 нетронуты, self-hosting не рестартит прод).
## Критерии приёмки
AC-1…AC-15 — все PASS. Покрытие тестами адресное: TC-01 (synced Done silence), TC-02 (aliased terminal по группе — ядро D1), TC-03 (Cancelled), TC-04 (no-op silence), TC-05 (дедуп), TC-06/TC-07 (легитимный unblock ×1), TC-08 (never-raise изоляция), TC-09 (kill-switch), TC-10 (enduro И orchestrator — headline-регресс), TC-11/TC-12 (TTL self-heal + back-compat + stale-on-failure).
## Findings
### P0 — Blocker
- нет
### P1 — Must fix
- нет
### P2 — Should fix
- нет
### P3 — Nice-to-have
- [ ] Дедуп-guard ключуется по `issue_id → state_uuid` без сброса при смене состояния. Теоретический edge-case: задача legitимно проходит `approved`(uuid_X)→…→снова `approved`(тот же uuid_X) — повторное (но легитимное) уведомление будет подавлено. Функционального ущерба нет (advance выполняется в `_dispatch` независимо от нотификации), это потеря только уведомления, и D2 — основной гард. Best-effort по контракту ТЗ. Можно при желании чистить запись при детекте смены состояния away-and-back. Не блокирует.
## Документация
Обновлена полно и в том же PR (AC-14 PASS):
- `docs/architecture/README.md` — компонент **Plane Sync** (TTL + `{uuid→group}`) и секция **Reconciler/F-2/F-4** (терминал-исключение, дедуп, счётчики `skipped_terminal_total`/`deduped_total`); футер «обновлять при изменении» расширен записью ORCH-068.
- `docs/architecture/adr/adr-0007-reconciler.md` — добавлена кросс-ссылка на per-WI ADR.
- `docs/work-items/ORCH-068/06-adr/ADR-001-…` — детальный ADR (Accepted).
- `CHANGELOG.md` — запись `### Fixed` (D1/D2/TR-3/TR-4, инварианты, тесты).
- `.env.example``ORCH_PLANE_STATES_TTL_S=300` с комментарием.
Изменение `src/` сопровождено соответствующим обновлением документации — требование golden-source выполнено.

View File

@@ -0,0 +1,64 @@
---
type: test-report
work_item_id: ORCH-068
result: PASS
---
# Test Report — ORCH-068
Фикс livelock reconciler F-2 (спам `_note_unblock` по синхронизированной
done-задаче) + связанный баг устаревшего `_STATES_CACHE`.
## Окружение
- Python: 3.12.13
- pytest: 8.3.3 (plugins: anyio-4.13.0, asyncio-0.23.8)
- Среда исполнения: worktree `feature/ORCH-068-bug-reconciler-livelock-unbloc`
- Prod health (8500): `{"status":"ok","service":"orchestrator"}` — OK (read-only smoke)
- Дата: 2026-06-08T05:13:59Z
## Smoke test API (read-only, прод 8500 не трогался деструктивно)
| Endpoint | Результат |
|----------|-----------|
| `GET /health` | `{"status":"ok",...}` — OK |
| `GET /status` | 200, активные задачи отданы — OK |
| `GET /queue` | 200, counts + блок `reconcile` (enabled/plane_enabled/unblocked_total) — OK |
## Результаты
| TC ID | Описание | Тест | Результат |
|-------|----------|------|-----------|
| TC-01 | Синхронизированная done → тишина (AC-1, AC-7) | `test_tc01_synced_done_is_silent` | PASS |
| TC-02 | Терминал «схлопнут» с approved по UUID исключён (AC-2) | `test_tc02_terminal_aliased_to_approved_excluded` | PASS |
| TC-03 | Cancelled терминал исключён (AC-2) | `test_tc03_cancelled_excluded` | PASS |
| TC-04 | `_note_unblock` не вызван после no-op dispatch (AC-3) | `test_tc04_noop_dispatch_no_unblock` | PASS |
| TC-05 | Дедуп: 0 повторных уведомлений (AC-4) | `test_tc05_dedup_no_repeat_notification` | PASS |
| TC-06 | Легитимный approved → unblock ровно один раз (AC-5) | `test_tc06_legit_approved_unblock_once` | PASS |
| TC-07 | in_progress-старт и rejected-откат, каждый 1 unblock (AC-6) | `test_tc07_in_progress_start_and_rejected_each_one_unblock` | PASS |
| TC-08 | never-raise: изоляция ошибок (AC-8) | `test_tc08_never_raise_isolation` | PASS |
| TC-09 | Kill-switch'и F-2 (AC-9) | `test_tc09_kill_switches` | PASS |
| TC-10 | done→silence на enduro И orchestrator (headline-регресс, AC-1/AC-2) | `test_tc10_done_silent_on_all_projects` | PASS |
| TC-11 | Устаревший `_STATES_CACHE` self-heal по TTL (AC-12) | `test_tc11_stale_cache_refreshes_after_ttl` + accessor/zero-ttl тесты | PASS |
| TC-12 | Совместимость кэша по умолчанию + fallback (AC-13) | `test_tc12_enduro_uuids_unchanged`, `test_tc12_api_error_falls_back_to_defaults`, `test_tc12_stale_served_when_refresh_fails` | PASS |
| TC-13 | Полный прогон `pytest tests/` зелёный (AC-15) | весь набор | PASS |
## Соответствие критериям приёмки
AC-1…AC-15 — все PASS. Целевые регресс-тесты (TC-01..TC-12) проходят;
полный набор без регрессий в reconciler / plane / qg / stage_engine / webhooks.
## Вывод pytest
### Целевые файлы (tests/test_reconciler_plane.py + tests/test_plane_states_cache.py)
```
collected 26 items
... (все 26 PASSED, включая tc01..tc17 reconciler + tc11/tc12 cache)
======================== 26 passed, 1 warning in 0.82s =========================
```
### Полный набор
```
======================= 764 passed, 1 warning in 13.66s ========================
```
(1 warning — PydanticDeprecatedSince20 в src/config.py, не относится к ORCH-068, предсуществующий.)
## Итог
PASS

View File

@@ -0,0 +1,12 @@
---
deploy_status: SUCCESS
work_item: ORCH-068
hook_exit_code: 0
deployed_by: deploy-finalizer
---
# Deploy log — ORCH-036 executable self-deploy
Прод-деплой завершён хост-хуком с exit-code `0` -> `deploy_status: SUCCESS`.
Вердикт зафиксирован детерминированным finalizer'ом (Фаза C), не LLM.

View File

@@ -0,0 +1,33 @@
---
staging_status: SUCCESS
timestamp: 2026-06-08T05:17:46Z
base_url: http://localhost:8501
---
# Staging Gate Log
Staging test suite completed. Exit code 0 → SUCCESS.
Executed canonically inside the `orchestrator-staging` container (ORCH-048, ADR-001),
so the B6 registry-isolation check read the staging instance's own process-env
(`.env.staging` → SANDBOX-only registry):
```
docker exec orchestrator-staging \
python3 /repos/orchestrator/scripts/staging_check.py \
--base-url http://localhost:8501 --mode stub
```
## Result: 8/10 checks PASS
- REAL failed: none
- All REAL checks green (Block A SMOKE, Block B ACCESS incl. B6 registry isolation,
C7 create issue, C8 trigger pipeline).
INFRA-WAIVED: C9a Branch appears in orchestrator-sandbox, C9b Analyst job enqueued in staging queue (known sandbox-infra; real checks green)
VERDICT: SUCCESS (exit 0) — SUCCESS (infra-waived): ['C9a Branch appears in orchestrator-sandbox', 'C9b Analyst job enqueued in staging queue'] are known sandbox-infra checks; all real checks green
Per ORCH-061, the two infra-only checks C9a/C9b (which depend on SANDBOX bot accounts
being project members, not on the pipeline) are tolerated when every REAL check is
green; the script prints `INFRA-WAIVED:`/`VERDICT:` lines and exits 0. Verdict trusts
the exit code.

View File

@@ -0,0 +1,14 @@
---
post_deploy_status: HEALTHY
action_taken: NONE
work_item: ORCH-068
window_s: 900
checks_total: 30
checks_failed: 0
---
# Post-deploy log — ORCH-021 post-deploy monitor
Наблюдение прода завершено: `post_deploy_status: HEALTHY`, `action_taken: NONE`.
Окно наблюдения: 900s; опросов всего: 30, из них с провалом: 0.

View File

@@ -4,3 +4,8 @@ pydantic-settings==2.5.0
httpx==0.27.0
pytest==8.3.3
pytest-asyncio==0.23.8
# ORCH-022: dependency audit (OSV/PyPI advisory) for the security-gate. Needs the
# network at scan time -> an unreachable feed degrades fail-open + warning by
# default (ADR-001 Р-3 / 07-infra I-2). gitleaks (secret-scan) is a pinned Go
# binary baked into the Dockerfile, NOT a pip package.
pip-audit==2.7.3

View File

@@ -219,6 +219,36 @@ class Settings(BaseSettings):
image_freshness_enabled: bool = True
image_freshness_repos: str = ""
# ORCH-022: security-gate (secret-scanning + dependency audit) on the
# deploy-staging -> deploy edge, run FIRST among the edge sub-gates (cheap to
# fail before the expensive rebase/rebuild). Deterministic (no LLM): gitleaks
# (offline secret-scan) + pip-audit (OSV/PyPI dependency audit), verdict in the
# versioned 17-security-report.md frontmatter; FAIL -> rollback to development +
# developer-retry (cap MAX_DEVELOPER_RETRIES). See ADR-001-security-gate.md.
# security_gate_enabled -> SINGLE kill-switch; False -> pipeline 1:1 as
# before ORCH-022 for everyone. Env
# ORCH_SECURITY_GATE_ENABLED.
# security_gate_repos -> CSV of repos where the gate is REAL; empty ->
# only the self-hosting repo (orchestrator).
# Mirrors merge_gate_repos / image_freshness_repos.
# security_dep_block_severity -> CVE severity threshold that BLOCKS (CRITICAL >
# HIGH > MEDIUM > LOW); below it / UNKNOWN -> a
# warning only (anti-loop ADR-001 Р-4).
# security_scan_timeout_s -> per external scanner call timeout (mirrors
# merge_retest_timeout_s).
# security_dep_audit_fail_closed -> strict mode: an unreachable CVE feed -> FAIL
# instead of the default fail-open + warning
# (Р-3). Default False (anti-loop ORCH-061).
# security_secrets_block -> a found secret blocks (always True by default;
# the offline secrets guarantee is unconditional,
# BR-2).
security_gate_enabled: bool = True
security_gate_repos: str = ""
security_dep_block_severity: str = "HIGH"
security_scan_timeout_s: int = 300
security_dep_audit_fail_closed: bool = False
security_secrets_block: bool = True
# ORCH-061: tolerate KNOWN sandbox-infra FAILs (C9a/C9b) in the staging suite.
# The self-hosting deploy-staging stage looped because scripts/staging_check.py
# exited non-zero on ANY failed check, so two infra-only failures (sandbox bot
@@ -265,6 +295,18 @@ class Settings(BaseSettings):
reconcile_notify_unblock: bool = True
reconcile_skip_blocked_enabled: bool = True
# ORCH-068: TTL for the per-project Plane states cache (_STATES_CACHE in
# plane_sync). Historically the cache lived for the whole process lifetime,
# so a status added to Plane after start was never seen without a restart
# ("stale set -> no pipeline action"). With a TTL the entry self-heals by
# re-fetching /states/ after it expires (invalidation reuses the existing
# reload_project_states() primitive — no duplicated reset logic).
# plane_states_ttl_s (env ORCH_PLANE_STATES_TTL_S):
# >0 -> seconds before a cache entry is re-fetched (default 300 = 5 min);
# 0 -> disable TTL -> strictly the previous lifetime cache (back-compat
# escape hatch). get_project_states return shape is unchanged.
plane_states_ttl_s: int = 300
# ORCH-021: post-deploy production monitoring + degradation reaction. After
# the terminal deploy->done transition for an applicable repo, a reserved-agent
# `post-deploy-monitor` job (no LLM, modelled on deploy-finalizer) probes prod

View File

@@ -1,6 +1,7 @@
"""Plane API sync — update issue state and add comments."""
import logging
import time
import httpx
from .config import settings
@@ -141,6 +142,12 @@ _PLANE_NAME_TO_KEY: dict[str, str] = {
"Needs Input": "needs_input",
"In Review": "in_review",
"Blocked": "blocked",
# ORCH-059: dedicated prod-deploy trigger status, distinct from the
# human-gate "Approved". Resolved from the live Plane API for the ORCH
# project; intentionally ABSENT from _DEFAULT_STATES so environments without
# this board status (enduro / API fallback) fail-closed — no UUID, no
# confirm-deploy branch, no KeyError (accessed via .get).
"Confirm Deploy": "confirm_deploy",
# ORCH-066: meaningful per-stage / human-input statuses (layer B).
"To Analyse": "to_analyse",
"Analysis": "analysis",
@@ -166,18 +173,42 @@ _STATE_ALIAS_FALLBACK: dict[str, str] = {
"monitoring": "done",
}
# Per-project state cache: {project_id: {logical_key: state_uuid}}
_STATES_CACHE: dict[str, dict[str, str]] = {}
# Per-project state cache (ORCH-10 + ORCH-068).
#
# Each entry is a RECORD, not a bare mapping:
# {"states": {logical_key: state_uuid}, # the ORCH-10 mapping (unchanged shape)
# "groups": {state_uuid: group}, # ORCH-068 D1: {uuid -> Plane state.group}
# "ts": monotonic timestamp} # ORCH-068 TR-4: for TTL self-heal
# get_project_states() still RETURNS the bare {logical_key: state_uuid} mapping
# (backward compatible — AC-13); the richer record is internal.
_STATES_CACHE: dict[str, dict] = {}
def _cache_record_fresh(record: dict) -> bool:
"""ORCH-068 (TR-4): is a cache record still within its TTL?
``plane_states_ttl_s <= 0`` disables the TTL -> a record never expires
(strictly the previous lifetime-cache behaviour, back-compat escape hatch).
"""
ttl = settings.plane_states_ttl_s
if ttl <= 0:
return True
ts = record.get("ts", 0.0)
return (time.monotonic() - ts) <= ttl
def get_project_states(project_id: str) -> dict[str, str]:
"""ORCH-10: resolve {logical_key -> state_uuid} for a specific Plane project.
Source of truth: Plane API GET /projects/<project_id>/states/.
Results are cached per project_id for the lifetime of the process.
Results are cached per project_id. ORCH-068 (TR-4): a cached entry is
re-fetched once it is older than ``plane_states_ttl_s`` (default 300s) so a
status added to Plane after start self-heals without a process restart;
``plane_states_ttl_s = 0`` keeps the previous lifetime cache.
Falls back to _DEFAULT_STATES (enduro-trails values) if:
* project_id is empty/None,
* the API call fails (network error, non-2xx),
* the API call fails (network error, non-2xx) AND nothing is cached,
* the response contains no recognisable states.
The enduro-trails project therefore returns the same UUIDs as before
@@ -187,8 +218,9 @@ def get_project_states(project_id: str) -> dict[str, str]:
if not project_id:
return _DEFAULT_STATES
if project_id in _STATES_CACHE:
return _STATES_CACHE[project_id]
cached = _STATES_CACHE.get(project_id)
if cached is not None and _cache_record_fresh(cached):
return cached["states"]
url = f"{PLANE_BASE}/workspaces/{WORKSPACE}/projects/{project_id}/states/"
try:
@@ -201,12 +233,21 @@ def get_project_states(project_id: str) -> dict[str, str]:
raise ValueError(f"unexpected states response shape: {type(items)}")
resolved: dict[str, str] = {}
groups: dict[str, str] = {}
for item in items:
name = item.get("name", "")
uid = item.get("id", "")
key = _PLANE_NAME_TO_KEY.get(name)
if key and uid:
resolved[key] = uid
# ORCH-068 D1: capture {uuid -> group} for terminal-state detection
# (a single API fetch — no extra network cost). The group is the
# authoritative, project-independent discriminator of terminal
# (completed/cancelled) vs review/work statuses, robust to UUID
# aliasing after status renames (ORCH-066).
grp = item.get("group", "")
if uid and grp:
groups[uid] = grp
if not resolved:
raise ValueError("no recognisable states in API response")
@@ -226,13 +267,26 @@ def get_project_states(project_id: str) -> dict[str, str]:
for k, v in _DEFAULT_STATES.items():
resolved.setdefault(k, v)
_STATES_CACHE[project_id] = resolved
_STATES_CACHE[project_id] = {
"states": resolved,
"groups": groups,
"ts": time.monotonic(),
}
logger.debug(
f"get_project_states: cached {len(resolved)} states for project {project_id[:8]}..."
f"get_project_states: cached {len(resolved)} states / "
f"{len(groups)} groups for project {project_id[:8]}..."
)
return resolved
except Exception as e:
# On a transient API failure keep serving the stale (but project-correct)
# set if we have one — far safer than reverting to enduro defaults.
if cached is not None:
logger.warning(
f"get_project_states: API refresh failed for project "
f"{project_id[:8]}..., serving stale cached set. Error: {e}"
)
return cached["states"]
logger.warning(
f"get_project_states: API failed for project {project_id[:8]}..., "
f"falling back to _DEFAULT_STATES. Error: {e}"
@@ -240,6 +294,23 @@ def get_project_states(project_id: str) -> dict[str, str]:
return _DEFAULT_STATES
def get_project_state_groups(project_id: str) -> dict[str, str]:
"""ORCH-068 (D1): return {state_uuid -> group} for a Plane project.
Reads the SAME cache record populated by ``get_project_states`` (no extra
network call). Call ``get_project_states(project_id)`` first to ensure the
record is fresh/populated. Returns ``{}`` when nothing is cached (e.g. the
API was unreachable and the caller fell back to ``_DEFAULT_STATES``); the
reconciler then falls back to logical terminal keys.
"""
record = _STATES_CACHE.get(project_id)
if isinstance(record, dict):
groups = record.get("groups")
if isinstance(groups, dict):
return groups
return {}
def reload_project_states(project_id: str = None) -> None:
"""ORCH-10: clear the per-project states cache.

View File

@@ -716,6 +716,23 @@ def _check_staging_image_fresh(repo: str, work_item_id: str, branch: str) -> tup
return check_staging_image_fresh(repo, work_item_id, branch)
def check_security_gate(repo: str, work_item_id: str, branch: str) -> tuple[bool, str]:
"""ORCH-022 security sub-gate (secret-scan + dependency audit) on the
deploy-staging -> deploy edge, run FIRST (before merge-gate / image-freshness).
Thin registry wrapper that delegates to ``security_gate.check_security_gate``
(gitleaks offline + pip-audit, write/read-back ``17-security-report.md``). The
real logic lives in ``src/security_gate.py`` (leaf module, never-raise,
fail-closed on secrets, fail-open degrade for the dep-audit feed); importing it
lazily here avoids an import cycle (security_gate imports is_self_hosting_repo
from this module). For non-self repos with an empty scope it returns
``(True, "security-gate N/A for <repo>")`` so the deploy edge is unchanged for
them (AC-13/TC-13).
"""
from ..security_gate import check_security_gate as _impl
return _impl(repo, work_item_id, branch)
# Registry for dynamic lookup by name
QG_CHECKS = {
"check_analysis_approved": check_analysis_approved,
@@ -730,4 +747,5 @@ QG_CHECKS = {
"check_staging_status": check_staging_status,
"check_branch_mergeable": check_branch_mergeable,
"check_staging_image_fresh": _check_staging_image_fresh,
"check_security_gate": check_security_gate,
}

View File

@@ -60,7 +60,12 @@ from .stage_engine import (
MAX_DEVELOPER_RETRIES,
)
from .stages import get_qg_for_stage
from .plane_sync import fetch_issue_state, get_project_states, list_issues_by_state
from .plane_sync import (
fetch_issue_state,
get_project_states,
get_project_state_groups,
list_issues_by_state,
)
from .webhooks.plane import handle_status_start, handle_verdict
from .notifications import send_telegram
from . import projects
@@ -139,6 +144,13 @@ class Reconciler:
self.last_run_ts: float | None = None
self.unblocked_total: int = 0
self.last_unblocked: str | None = None
# ORCH-068 observability: terminal-state skips and dedup suppressions.
self.skipped_terminal_total: int = 0
self.deduped_total: int = 0
# ORCH-068 (TR-3): in-memory dedup guard {issue_id -> last unblocked
# state uuid}. Best-effort (resets on restart, like unblocked_total);
# suppresses a repeat unblock notification for the same issue+state.
self._unblock_dedup: dict[str, str] = {}
# -- F-1: gate-side ----------------------------------------------------
def reconcile_gate_once(self) -> None:
@@ -271,23 +283,47 @@ class Reconciler:
# the project's own `in_progress` UUID, so enduro behaviour is identical
# (and `list_issues_by_state` deduplicates the uuid via its internal set).
states = get_project_states(pid)
# ORCH-066 (AC-19): start/resume trigger is `To Analyse`.
to_analyse = states["to_analyse"]
# ORCH-068 D1: {uuid -> group} from the SAME cache record (no extra
# fetch); empty when the API was unreachable -> per-issue fallback by key.
groups = get_project_state_groups(pid)
approved = states["approved"]
rejected = states["rejected"]
issues = list_issues_by_state(pid, [to_analyse, approved, rejected])
for issue in issues:
try:
self._reconcile_plane_issue(
issue, pid, to_analyse, approved, rejected
issue, pid, to_analyse, approved, rejected, states, groups
)
except Exception as e: # noqa: BLE001 - isolate one issue's failure
logger.error(
f"reconciler F-2: issue {issue.get('id')} failed: {e}"
)
def _is_terminal_state(
self, state_uuid: str, states: dict, groups: dict
) -> bool:
"""ORCH-068 D1: is ``state_uuid`` a terminal (completed/cancelled) state?
Primary discriminator is the Plane **state group** (project-independent,
robust to UUID aliasing after status renames): ``group`` in
``{completed, cancelled}`` -> terminal. When the group is unavailable
(API gave no ``group`` / we fell back to ``_DEFAULT_STATES``), fall back
to the logical terminal keys ``done`` / ``cancelled``.
"""
if not state_uuid:
return False
grp = groups.get(state_uuid)
if grp:
return grp in {"completed", "cancelled"}
# Fallback (group unknown): logical terminal keys for this project.
return state_uuid in {states.get("done"), states.get("cancelled")}
def _reconcile_plane_issue(
self, issue: dict, project_id: str,
to_analyse: str, approved: str, rejected: str,
states: dict, groups: dict,
) -> None:
issue_id = str(issue.get("id") or "")
if not issue_id:
@@ -295,6 +331,15 @@ class Reconciler:
state = issue.get("state")
new_state = state.get("id") if isinstance(state, dict) else state
# ORCH-068 D1: a terminal issue (Done / Cancelled) is fully in sync by
# definition -> never actionable. Excluded per-issue (not by narrowing
# `wanted`) because UUID aliasing can make a terminal uuid collide with
# an actionable one — only the state GROUP disentangles them. Restores
# the silence-when-in-sync invariant (AC-1/AC-2).
if self._is_terminal_state(new_state, states, groups):
self.skipped_terminal_total += 1
return
# Grace ("lost, not merely delayed"): use the issue's own updated_at age.
# A missing/unparseable timestamp is treated as old enough (the active-job
# guard + atomic create-claim still prevent doubling).
@@ -319,24 +364,48 @@ class Reconciler:
if new_state == to_analyse and task is None:
# To Analyse without a task -> start the pipeline (lost start webhook).
# ORCH-068 D2: confirm a REAL change (the task now exists) before
# announcing — a no-op dispatch stays silent.
self._dispatch(handle_status_start, issue_data, project_id)
self._note_unblock(issue_id, "analysis")
if get_task_by_plane_id(issue_id) is not None:
self._note_unblock(issue_id, "analysis", new_state)
elif new_state == to_analyse and task is not None:
# To Analyse with an existing (idle) task -> resume the analyst from
# Needs Input (lost resume webhook). handle_status_start applies its
# own busy-guard / start-vs-resume fork.
self._dispatch(handle_status_start, issue_data, project_id)
self._note_unblock(task.get("work_item_id") or issue_id, task["stage"])
self._note_unblock(task.get("work_item_id") or issue_id, task["stage"], new_state)
elif new_state == approved and task is not None:
# Approved but the stage never advanced -> replay the verdict.
stage_before = task["stage"]
self._dispatch(handle_verdict, issue_data, project_id, approved=True)
self._note_unblock(task.get("work_item_id") or issue_id, task["stage"])
if self._stage_changed(issue_id, stage_before):
self._note_unblock(
task.get("work_item_id") or issue_id, stage_before, new_state
)
elif new_state == rejected and task is not None:
# Rejected but never rolled back -> replay the verdict.
stage_before = task["stage"]
self._dispatch(handle_verdict, issue_data, project_id, approved=False)
self._note_unblock(task.get("work_item_id") or issue_id, task["stage"])
if self._stage_changed(issue_id, stage_before):
self._note_unblock(
task.get("work_item_id") or issue_id, stage_before, new_state
)
# else: everything is in sync -> silence (AC-10).
@staticmethod
def _stage_changed(issue_id: str, stage_before: str) -> bool:
"""ORCH-068 D2: did the dispatched handler actually move the stage?
Re-reads the task after ``_dispatch`` and compares to the captured
``stage_before``. A no-op replay (the task was already in the target
state) leaves the stage unchanged -> no unblock notification.
"""
after = get_task_by_plane_id(issue_id)
stage_after = after["stage"] if after else stage_before
return stage_after != stage_before
@staticmethod
def _dispatch(coro_fn, *args, **kwargs) -> None:
"""Run an async plane handler from this sync thread.
@@ -349,12 +418,27 @@ class Reconciler:
asyncio.run(coro_fn(*args, **kwargs))
# -- observability (F-4) ----------------------------------------------
def _note_unblock(self, work_item_id: str, stage: str) -> None:
def _note_unblock(
self, work_item_id: str, stage: str, state_uuid: str | None = None
) -> None:
"""Record + announce that a stuck task was unblocked (AC-12).
Fires only on an actual state change (an advance / replayed transition),
never per idle tick, so it does not conflict with AC-9 / AC-10.
ORCH-068 (TR-3): an in-memory dedup guard keyed by ``issue_id ->
state_uuid`` suppresses a repeat notification for the same issue+state
if a future no-op path ever reaches here. ``state_uuid`` is the issue's
Plane state; ``work_item_id`` doubles as the issue id for the
pipeline-start case (which has no work item yet).
"""
dedup_key = work_item_id
if state_uuid is not None and self._unblock_dedup.get(dedup_key) == state_uuid:
self.deduped_total += 1
return
if state_uuid is not None:
self._unblock_dedup[dedup_key] = state_uuid
self.unblocked_total += 1
self.last_unblocked = work_item_id
logger.info(
@@ -415,6 +499,9 @@ class Reconciler:
"last_run_ts": self.last_run_ts,
"unblocked_total": self.unblocked_total,
"last_unblocked": self.last_unblocked,
# ORCH-068 observability.
"skipped_terminal_total": self.skipped_terminal_total,
"deduped_total": self.deduped_total,
}

689
src/security_gate.py Normal file
View File

@@ -0,0 +1,689 @@
"""Security-gate core (ORCH-022): secret-scanning + dependency audit before merge.
Background
----------
The orchestrator is autonomous: the ``developer`` agent writes code with no human
filter. Before a task branch merges into ``main`` there was no automatic check for a
leaked secret (key / token / password / private key) or a vulnerable dependency
(known CVE). For the self-hosting ``orchestrator`` repo this is acute: one shared
prod instance serves every project from a shared DB, so a secret or CVE that slips
through one task lands in the prod of all projects (CLAUDE.md §self-hosting, §8).
This module provides the deterministic (no-LLM) primitives that the quality-gate
``check_security_gate`` (src/qg/checks.py) composes on the ``deploy-staging ->
deploy`` edge, **FIRST** among the edge sub-gates (BEFORE the merge-gate and
image-freshness), immediately before the deployer merges the PR (ADR-001 Р-1):
* ``scan_secrets`` -> run ``gitleaks`` over ``origin/main..HEAD`` (offline).
* ``audit_dependencies`` -> run ``pip-audit`` over ``requirements.txt`` (OSV/PyPI).
* ``classify_severity`` -> pure: map a CVE severity to block / warning.
* ``compute_verdict`` -> pure: combine findings + thresholds -> the artefact
frontmatter fields + a human-readable reason.
* ``write_security_report`` / ``parse_security_status`` -> write the
``17-security-report.md`` artefact and read its machine verdict back (single
source of truth: the gate returns exactly the frontmatter it wrote, AC-8).
* ``check_security_gate`` -> the orchestrating entry the QG wrapper delegates to.
Invariants (ADR-001 §7, never broken):
* **Secrets are unconditional** (BR-2): gitleaks is fully offline, so the "a
secret always blocks" guarantee does not depend on the network. A secret-scan
TOOL error is **fail-closed** (we cannot prove "no secret" -> FAIL).
* **Dependency audit is best-effort** (Р-3): an unreachable CVE feed degrades
**fail-open + a loud warning** by default (anti-loop, precedent ORCH-061);
``security_dep_audit_fail_closed`` flips it to strict.
* **never-raise**: any internal error -> ``(False, "<reason>")``; an exception
never escapes into ``advance_stage`` (AC-16).
* **Self-hosting safety** (AC-19): the gate only reads / scans / writes the
artefact. It never calls the deploy hook and never restarts the prod container.
This module is a **leaf**: it imports only ``config`` / ``git_worktree`` and lazily
``qg.checks.is_self_hosting_repo`` / ``notifications``; it never imports
``stage_engine``.
"""
import json
import logging
import os
import subprocess
from dataclasses import dataclass, field
from .config import settings
from .git_worktree import ensure_worktree, get_worktree_path
logger = logging.getLogger("orchestrator.security_gate")
# Bounded git timeout so a hung fetch never wedges the monitor-thread running the
# gate (the scan timeout itself comes from settings.security_scan_timeout_s).
_GIT_TIMEOUT = 60
# Severity ranking for the dependency block threshold. UNKNOWN / unrecognised is
# intentionally absent -> classified as "warning" (anti-loop, ADR-001 Р-4).
_SEVERITY_ORDER = {"LOW": 1, "MEDIUM": 2, "HIGH": 3, "CRITICAL": 4}
# ---------------------------------------------------------------------------
# Result containers (plain dataclasses, easy to build in tests)
# ---------------------------------------------------------------------------
@dataclass
class SecretScanResult:
"""Outcome of :func:`scan_secrets`.
status:
* ``"clean"`` -> no secret found.
* ``"found"`` -> ``findings`` lists the confirmed (non-allowlisted) secrets.
* ``"error"`` -> the scanner could not run (missing binary / timeout / rc>=2);
treated as **fail-closed** by :func:`compute_verdict` (BR-2).
"""
status: str = "clean"
findings: list = field(default_factory=list)
detail: str = ""
@dataclass
class DepAuditResult:
"""Outcome of :func:`audit_dependencies`.
status:
* ``"ok"`` -> the audit ran; ``findings`` may be empty or non-empty.
* ``"degraded"`` -> the CVE feed was unreachable / the tool failed; **fail-open**
by default (ADR-001 Р-3), surfaced as ``deps_audit_degraded: true``.
"""
status: str = "ok"
findings: list = field(default_factory=list)
detail: str = ""
# ---------------------------------------------------------------------------
# Conditionality (mirrors _merge_gate_applies / image_freshness_applies)
# ---------------------------------------------------------------------------
def security_gate_applies(repo: str) -> bool:
"""Whether the security-gate is REAL for this repo (conditional rollout).
Mirrors the ORCH-35 / ORCH-43 / ORCH-58 pattern:
* ``security_gate_enabled=False`` -> always False (kill-switch; pipeline is
1:1 as before ORCH-022 for everyone).
* ``security_gate_repos`` (CSV) non-empty -> real only for the listed repos.
* empty CSV -> real ONLY for the self-hosting repo (``orchestrator``).
Never raises (AC-16): any error -> False (the safe no-op default).
"""
try:
if not settings.security_gate_enabled:
return False
raw = (settings.security_gate_repos or "").strip()
if raw:
allowed = {r.strip().lower() for r in raw.split(",") if r.strip()}
return (repo or "").strip().lower() in allowed
# Lazy import keeps this module a leaf (no qg import at module load).
from .qg.checks import is_self_hosting_repo
return is_self_hosting_repo(repo)
except Exception as e: # noqa: BLE001 - never-raise contract
logger.warning("security_gate_applies error for %s: %s", repo, e)
return False
# ---------------------------------------------------------------------------
# Secret-scanning (gitleaks, offline) — FR-1 / AC-1..AC-3
# ---------------------------------------------------------------------------
def _gitleaks_config_path(worktree: str) -> str | None:
"""Versioned ``.gitleaks.toml`` at the repo root (BR-13), or None if absent."""
cfg = os.path.join(worktree, ".gitleaks.toml")
return cfg if os.path.isfile(cfg) else None
def _mask(secret: str) -> str:
"""Mask a matched secret so the artefact never re-leaks it verbatim."""
s = (secret or "").strip()
if len(s) <= 8:
return "****"
return f"{s[:4]}{s[-2:]}"
def parse_gitleaks_report(text: str) -> list:
"""Pure parser for the gitleaks JSON report -> a list of finding dicts.
Each finding: ``{"file", "rule", "line", "match"}`` (the match is MASKED).
Tolerates an empty / non-JSON / non-list body (returns ``[]``); never raises.
"""
try:
data = json.loads(text or "[]")
except (ValueError, TypeError):
return []
if not isinstance(data, list):
return []
out = []
for item in data:
if not isinstance(item, dict):
continue
out.append(
{
"file": item.get("File") or item.get("file") or "?",
"rule": item.get("RuleID") or item.get("Description") or "secret",
"line": item.get("StartLine") or item.get("startLine") or 0,
"match": _mask(item.get("Secret") or item.get("Match") or ""),
}
)
return out
def scan_secrets(repo: str, branch: str) -> SecretScanResult:
"""Scan ``origin/main..HEAD`` of the task branch for secrets with ``gitleaks``.
Offline (BR-2): gitleaks rules are local, so the "a secret always blocks"
guarantee never depends on the network. Scanning the ``origin/main..HEAD``
range covers exactly the commits this task adds (and that will land in
``main``), and — because it runs BEFORE the merge-gate rebase — does not blame
the task for a secret introduced by a parallel update of ``main`` (ADR-001 Р-1).
Exit-code contract (07-infra-requirements.md I-1): 0 = clean, 1 = secrets
found, >=2 = tool error. A tool error / missing binary / timeout -> ``"error"``
(fail-closed downstream). Never raises (AC-16).
"""
try:
wt = ensure_worktree(repo, branch)
except Exception as e: # noqa: BLE001 - never-raise contract
return SecretScanResult(status="error", detail=f"worktree error: {e}")
# Refresh origin/main so the origin/main..HEAD range is meaningful. Best-effort:
# a fetch failure does not abort the scan (gitleaks still scans whatever range
# it can resolve); the scan itself is the security-critical step.
try:
subprocess.run(
["git", "-C", wt, "fetch", "origin", "main"],
capture_output=True, timeout=_GIT_TIMEOUT,
)
except (subprocess.SubprocessError, OSError) as e:
logger.warning("scan_secrets: fetch origin/main failed for %s/%s: %s", repo, branch, e)
report_path = os.path.join(wt, ".gitleaks-report.json")
cmd = [
"gitleaks", "detect",
"--source", wt,
"--log-opts", "origin/main..HEAD",
"--report-format", "json",
"--report-path", report_path,
"--exit-code", "1",
"--no-banner",
]
cfg = _gitleaks_config_path(wt)
if cfg:
cmd += ["--config", cfg]
timeout = settings.security_scan_timeout_s
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
except subprocess.TimeoutExpired:
return SecretScanResult(status="error", detail=f"gitleaks timeout after {timeout}s")
except FileNotFoundError:
# Missing binary -> fail-closed (we cannot prove the branch is secret-free).
return SecretScanResult(status="error", detail="gitleaks binary not found")
except (subprocess.SubprocessError, OSError) as e:
return SecretScanResult(status="error", detail=f"gitleaks error: {e}")
finally:
# The report file is transient scratch inside the worktree; remove it after
# reading so it is never committed/scanned on a later pass.
report_text = ""
try:
if os.path.isfile(report_path):
with open(report_path, "r", encoding="utf-8") as f:
report_text = f.read()
os.remove(report_path)
except OSError:
report_text = ""
if r.returncode == 0:
return SecretScanResult(status="clean", detail="no secrets found")
if r.returncode == 1:
findings = parse_gitleaks_report(report_text) or parse_gitleaks_report(r.stdout)
if not findings:
# rc=1 with no parseable findings -> still treat as found (fail-closed).
findings = [{"file": "?", "rule": "secret", "line": 0, "match": "****"}]
return SecretScanResult(
status="found", findings=findings, detail=f"{len(findings)} secret(s) found"
)
# rc >= 2 (or any other) -> tool error -> fail-closed.
tail = ((r.stderr or "") + (r.stdout or "")).strip()[-200:]
return SecretScanResult(status="error", detail=f"gitleaks rc={r.returncode}: {tail}")
# ---------------------------------------------------------------------------
# Dependency audit (pip-audit, OSV/PyPI) — FR-2 / AC-4..AC-7
# ---------------------------------------------------------------------------
def parse_pip_audit_report(text: str) -> list:
"""Pure parser for the ``pip-audit -f json`` report -> a list of finding dicts.
Each finding: ``{"package", "version", "id", "severity", "fix"}``. pip-audit's
default JSON rarely carries a CVSS severity (OSV advisories often omit it), so a
missing severity is reported as ``"UNKNOWN"`` (classified as a warning, never an
auto-block — ADR-001 Р-4 anti-loop). Tolerates both the modern
``{"dependencies": [...]}`` shape and a bare list; never raises.
"""
try:
data = json.loads(text or "{}")
except (ValueError, TypeError):
return []
if isinstance(data, dict):
deps = data.get("dependencies", data.get("vulnerabilities", []))
elif isinstance(data, list):
deps = data
else:
return []
out = []
for dep in deps or []:
if not isinstance(dep, dict):
continue
name = dep.get("name") or dep.get("package") or "?"
version = dep.get("version") or "?"
for v in dep.get("vulns", dep.get("vulnerabilities", [])) or []:
if not isinstance(v, dict):
continue
sev = _extract_severity(v)
fix = v.get("fix_versions") or v.get("fixed_in") or []
aliases = v.get("aliases") or []
vuln_id = v.get("id") or (aliases[0] if aliases else "?")
out.append(
{
"package": name,
"version": version,
"id": vuln_id,
"severity": sev,
"fix": ", ".join(fix) if isinstance(fix, list) else str(fix),
}
)
return out
def _extract_severity(vuln: dict) -> str:
"""Best-effort severity extraction from a pip-audit vuln record -> UPPER token.
pip-audit JSON may carry severity in different shapes depending on the advisory
source; when none is present we return ``"UNKNOWN"`` (warning, never a block).
"""
raw = vuln.get("severity")
if isinstance(raw, str) and raw.strip():
return raw.strip().upper()
if isinstance(raw, list) and raw:
first = raw[0]
if isinstance(first, dict):
val = first.get("severity") or first.get("score") or first.get("type")
if val:
return str(val).strip().upper()
elif first:
return str(first).strip().upper()
return "UNKNOWN"
def audit_dependencies(repo: str, branch: str) -> DepAuditResult:
"""Audit the branch's ``requirements.txt`` for known CVEs with ``pip-audit``.
The advisory source is OSV/PyPI -> it needs the network. Per ADR-001 Р-3 an
unreachable feed / tool failure degrades **fail-open** by default (status
``"degraded"``), so a transient network problem on the prod instance never
produces a false rollback loop (precedent ORCH-061). The ``"degraded"`` state
is surfaced loudly (``deps_audit_degraded: true`` + warning log + Telegram).
Returns a :class:`DepAuditResult`. Never raises (AC-16).
"""
try:
wt = get_worktree_path(repo, branch)
if not os.path.isdir(wt):
wt = ensure_worktree(repo, branch)
except Exception as e: # noqa: BLE001 - never-raise contract
return DepAuditResult(status="degraded", detail=f"worktree error: {e}")
req = os.path.join(wt, "requirements.txt")
if not os.path.isfile(req):
# Python-only v1 (A3): no manifest -> nothing to audit (not a degrade).
return DepAuditResult(status="ok", detail="no requirements.txt to audit")
cmd = ["pip-audit", "-r", req, "-f", "json", "--progress-spinner", "off"]
timeout = settings.security_scan_timeout_s
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
except subprocess.TimeoutExpired:
return DepAuditResult(status="degraded", detail=f"pip-audit timeout after {timeout}s")
except FileNotFoundError:
# Missing binary -> degrade (dep-audit is best-effort, not unconditional).
return DepAuditResult(status="degraded", detail="pip-audit binary not found")
except (subprocess.SubprocessError, OSError) as e:
return DepAuditResult(status="degraded", detail=f"pip-audit error: {e}")
# pip-audit exits 0 (no vulns) or 1 (vulns found) with valid JSON on stdout. A
# network/feed error produces non-JSON output (and often a non-zero rc) -> if
# we cannot parse the JSON we degrade fail-open rather than block falsely.
out = (r.stdout or "").strip()
if not out:
if r.returncode == 0:
return DepAuditResult(status="ok", detail="no vulnerabilities")
tail = (r.stderr or "").strip()[-200:]
return DepAuditResult(status="degraded", detail=f"pip-audit no output (rc={r.returncode}): {tail}")
try:
json.loads(out)
except ValueError:
tail = (r.stderr or "").strip()[-200:]
return DepAuditResult(status="degraded", detail=f"pip-audit feed unavailable: {tail}")
findings = parse_pip_audit_report(out)
return DepAuditResult(status="ok", findings=findings, detail=f"{len(findings)} vuln(s)")
# ---------------------------------------------------------------------------
# Pure classification + verdict (FR-2/FR-3/Р-4) — the core of the unit tests
# ---------------------------------------------------------------------------
def classify_severity(severity: str, block_threshold: str) -> str:
"""Pure: classify a CVE severity against the block threshold -> token.
Returns ``"block"`` when ``severity >= block_threshold`` in CRITICAL > HIGH >
MEDIUM > LOW order, else ``"warning"``. An UNKNOWN / unrecognised severity is
ALWAYS ``"warning"`` (never an auto-block — anti-loop, ADR-001 Р-4). Never
raises.
"""
sev = (severity or "").upper().strip()
thr = (block_threshold or "HIGH").upper().strip()
sev_rank = _SEVERITY_ORDER.get(sev)
thr_rank = _SEVERITY_ORDER.get(thr, _SEVERITY_ORDER["HIGH"])
if sev_rank is None:
return "warning"
return "block" if sev_rank >= thr_rank else "warning"
def compute_verdict(
secret_result: SecretScanResult,
dep_result: DepAuditResult,
*,
secrets_block: bool,
dep_block_severity: str,
dep_fail_closed: bool,
) -> dict:
"""Pure: combine scan results + thresholds into the artefact's machine fields.
Returns a dict with the frontmatter fields (``security_status``,
``secrets_found``, ``deps_blocking``, ``deps_warning``, ``deps_audit_degraded``),
a one-line ``reason`` summary, and the categorised finding lists for the body.
Decision (ADR-001 Р-4):
* secret-scan ERROR -> FAIL (fail-closed; BR-2 secrets guarantee is unconditional).
* any secret found AND ``secrets_block`` -> FAIL.
* any dependency at/over ``dep_block_severity`` -> FAIL (``deps_blocking``).
* MEDIUM/LOW/UNKNOWN deps -> warning only (``deps_warning``), never block.
* feed degraded -> warning by default; FAIL only when ``dep_fail_closed``.
Never raises.
"""
secret_scan_error = secret_result.status == "error"
secret_findings = list(secret_result.findings) if secret_result.status == "found" else []
secrets_found = len(secret_findings)
deps_audit_degraded = dep_result.status == "degraded"
blocking_findings = []
warning_findings = []
for f in dep_result.findings or []:
if classify_severity(f.get("severity", "UNKNOWN"), dep_block_severity) == "block":
blocking_findings.append(f)
else:
warning_findings.append(f)
reasons = []
fail = False
if secret_scan_error:
fail = True
reasons.append(f"secret scan error (fail-closed): {secret_result.detail}")
if secrets_block and secrets_found > 0:
fail = True
names = ", ".join(
f"{x.get('rule')} in {x.get('file')}:{x.get('line')}" for x in secret_findings
)
reasons.append(f"{secrets_found} secret(s): {names}")
if blocking_findings:
fail = True
names = ", ".join(
f"{x.get('package')} {x.get('version')} {x.get('id')} ({x.get('severity')})"
for x in blocking_findings
)
reasons.append(f"{len(blocking_findings)} blocking CVE(s): {names}")
if deps_audit_degraded and dep_fail_closed:
fail = True
reasons.append(f"dep-audit feed unavailable (fail-closed): {dep_result.detail}")
status = "FAIL" if fail else "PASS"
if reasons:
reason = "; ".join(reasons)
else:
extra = " (dep-audit degraded — warning only)" if deps_audit_degraded else ""
reason = f"clean: {secrets_found} secrets, {len(blocking_findings)} blocking CVE(s){extra}"
return {
"security_status": status,
"secrets_found": secrets_found,
"secret_scan_error": secret_scan_error,
"deps_blocking": len(blocking_findings),
"deps_warning": len(warning_findings),
"deps_audit_degraded": deps_audit_degraded,
"reason": reason,
"secret_findings": secret_findings,
"blocking_findings": blocking_findings,
"warning_findings": warning_findings,
}
# ---------------------------------------------------------------------------
# Artefact: write the report, read the machine verdict back (FR-3 / AC-8..AC-10)
# ---------------------------------------------------------------------------
def _report_rel(work_item_id: str) -> str:
return f"docs/work-items/{work_item_id}/17-security-report.md"
def _report_path(repo: str, work_item_id: str, branch: str) -> str:
"""Absolute path of 17-security-report.md inside the task worktree."""
try:
wt = get_worktree_path(repo, branch)
if not os.path.isdir(wt):
wt = ensure_worktree(repo, branch)
except Exception: # noqa: BLE001 - never-raise; fall back to shared clone
wt = os.path.join(settings.repos_dir, repo)
return os.path.join(wt, _report_rel(work_item_id))
def _bool_yaml(v: bool) -> str:
return "true" if v else "false"
def render_security_report(work_item_id: str, fields: dict) -> str:
"""Pure: render the 17-security-report.md content (frontmatter + body) from the
fields produced by :func:`compute_verdict`. Never raises."""
def _secret_lines():
items = fields.get("secret_findings") or []
if not items:
return "- None"
return "\n".join(
f"- `{x.get('file')}:{x.get('line')}` — {x.get('rule')} (match `{x.get('match')}`)"
for x in items
)
def _dep_lines(key):
items = fields.get(key) or []
if not items:
return "- None"
return "\n".join(
f"- `{x.get('package')}=={x.get('version')}` — {x.get('id')} "
f"severity={x.get('severity')} fix={x.get('fix') or 'n/a'}"
for x in items
)
return (
"---\n"
f"security_status: {fields.get('security_status', 'FAIL')}\n"
f"secrets_found: {int(fields.get('secrets_found', 0))}\n"
f"deps_blocking: {int(fields.get('deps_blocking', 0))}\n"
f"deps_warning: {int(fields.get('deps_warning', 0))}\n"
f"deps_audit_degraded: {_bool_yaml(bool(fields.get('deps_audit_degraded', False)))}\n"
"---\n"
f"# Security Report — {work_item_id}\n\n"
"Детерминированный security-гейт (ORCH-022): secret-scanning (gitleaks, offline) + "
"dependency audit (pip-audit). Машинный вердикт читается ТОЛЬКО из frontmatter выше.\n\n"
"## Verdict\n"
f"{fields.get('reason', '')}\n\n"
"## Secrets\n"
f"{_secret_lines()}\n\n"
"## Dependencies (blocking)\n"
f"{_dep_lines('blocking_findings')}\n\n"
"## Dependencies (warning)\n"
f"{_dep_lines('warning_findings')}\n"
)
def write_security_report(repo: str, work_item_id: str, branch: str, fields: dict) -> str:
"""Write 17-security-report.md into the task worktree; return its path.
Best-effort/never-raise: a write error is logged and the path is still returned
(the caller's read-back then fails closed). The artefact body is human-readable;
the machine verdict lives ONLY in the YAML frontmatter (canon)."""
path = _report_path(repo, work_item_id, branch)
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(render_security_report(work_item_id, fields))
except OSError as e:
logger.error("write_security_report error for %s/%s: %s", repo, work_item_id, e)
return path
def parse_security_status(content: str) -> tuple[bool, str]:
"""Map a 17-security-report.md body to a quality-gate verdict by reading ONLY
the machine-readable ``security_status:`` YAML frontmatter — never the prose.
Mirrors ``_parse_deploy_status`` / ``_parse_staging_status`` (canon: machine
verdict only from frontmatter, AC-8). The negative token (FAIL) is authoritative
(checked first). Returns:
* ``security_status: PASS`` -> ``(True, "Security status: PASS")``
* ``security_status: FAIL`` -> ``(False, "Security status: FAIL")``
* missing field / no frontmatter / bad YAML -> ``(False, <reason>)`` (fail-closed
on the verdict read, AC-9).
"""
import yaml
status = None
if content.startswith("---"):
parts = content.split("---", 2)
if len(parts) >= 3:
try:
fm = yaml.safe_load(parts[1]) or {}
except yaml.YAMLError as e:
return False, f"Invalid YAML frontmatter in security report: {e}"
if isinstance(fm, dict):
status = str(fm.get("security_status", "")).upper().strip()
if status == "FAIL":
return False, "Security status: FAIL"
if status == "PASS":
return True, "Security status: PASS"
return False, f"No machine-readable security_status in frontmatter (got: {status!r})"
def extract_security_findings(report_path: str) -> str:
"""ORCH-046: best-effort verbatim excerpt of the report's finding sections for
embedding into the developer's ``task_desc`` on a rollback.
Pulls the ``## Verdict`` + ``## Secrets`` + ``## Dependencies (blocking)``
sections so the developer sees the must-fix substance directly (not just a
link). Contract «never raise»: any error / missing file -> ``""`` (the caller
then falls back to the reason + link). Mirrors ``review_parse`` defensiveness.
"""
try:
if not os.path.isfile(report_path):
return ""
with open(report_path, "r", encoding="utf-8") as f:
content = f.read()
# Drop the frontmatter; keep the human body.
if content.startswith("---"):
parts = content.split("---", 2)
if len(parts) >= 3:
content = parts[2]
wanted = ("## Verdict", "## Secrets", "## Dependencies (blocking)")
lines = content.splitlines()
out = []
keep = False
for ln in lines:
if ln.startswith("## "):
keep = any(ln.startswith(w) for w in wanted)
if keep:
out.append(ln)
excerpt = "\n".join(out).strip()
return excerpt[:1500]
except Exception as e: # noqa: BLE001 - never-raise (ORCH-046 defensive)
logger.warning("extract_security_findings error for %s: %s", report_path, e)
return ""
# ---------------------------------------------------------------------------
# Orchestrating entry — delegated to by qg.checks.check_security_gate
# ---------------------------------------------------------------------------
def check_security_gate(repo: str, work_item_id: str, branch: str) -> tuple[bool, str]:
"""ORCH-022 security-gate on the deploy-staging -> deploy edge, run FIRST.
Deterministic, no LLM. Algorithm (ADR-001 Р-1/Р-5):
1. Conditionality: ``security_gate_enabled=False`` -> ``(True, "...disabled")``;
a repo the gate is not real for -> ``(True, "security-gate N/A for <repo>")``.
2. ``scan_secrets`` (offline) + ``audit_dependencies`` (best-effort).
3. ``compute_verdict`` -> write ``17-security-report.md`` -> read the verdict
BACK via ``parse_security_status`` (single source of truth: the returned
verdict == the artefact frontmatter, AC-8).
4. FAIL -> ``(False, reason)`` (engine rolls back to ``development``); PASS ->
``(True, reason)`` (engine proceeds to the merge-gate).
A degraded dep-audit on a PASS is surfaced loudly (Telegram + log) without
failing the gate (ADR-001 Р-3). Never-raise (AC-16): any internal error ->
``(False, "<reason>")``; an exception never escapes into ``advance_stage``.
"""
try:
if not settings.security_gate_enabled:
return True, "security-gate disabled"
if not security_gate_applies(repo):
return True, f"security-gate N/A for {repo}"
secret_result = scan_secrets(repo, branch)
dep_result = audit_dependencies(repo, branch)
fields = compute_verdict(
secret_result,
dep_result,
secrets_block=settings.security_secrets_block,
dep_block_severity=settings.security_dep_block_severity,
dep_fail_closed=settings.security_dep_audit_fail_closed,
)
path = write_security_report(repo, work_item_id, branch, fields)
# Read the machine verdict back from the artefact we just wrote — so the
# returned (bool, reason) is guaranteed == the YAML frontmatter (AC-8).
try:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
except OSError as e:
return False, f"cannot read security report (fail-closed): {e}"
ok, _verdict = parse_security_status(content)
# Surface a degraded dep-audit loudly even when the gate passes (Р-3 / BR-11).
if fields.get("deps_audit_degraded"):
logger.warning(
"security-gate %s/%s: dep-audit DEGRADED (fail-%s): %s",
repo, work_item_id,
"closed" if settings.security_dep_audit_fail_closed else "open",
dep_result.detail,
)
try:
from .notifications import send_telegram
send_telegram(
f"⚠️ {work_item_id}: dep-audit недоступен фид CVE "
f"({dep_result.detail}). "
+ ("Гейт fail-closed → FAIL." if settings.security_dep_audit_fail_closed
else "Гейт fail-open → warning (секреты проверены оффлайн).")
)
except Exception as e: # noqa: BLE001 - telegram best-effort
logger.warning("security-gate degraded telegram failed: %s", e)
if ok:
logger.info("security-gate passed for %s/%s: %s", repo, work_item_id, fields["reason"])
return True, f"security clean ({fields['reason']})"
return False, fields["reason"]
except Exception as e: # noqa: BLE001 - never-raise contract (AC-16)
logger.error("check_security_gate error for %s/%s: %s", repo, branch, e)
return False, f"security-gate error: {e}"

View File

@@ -34,6 +34,7 @@ from .db import get_db, update_task_stage, enqueue_job
from .stages import get_next_stage, get_qg_for_stage, get_agent_for_stage
from .git_worktree import get_worktree_path
from .review_parse import extract_review_findings, extract_test_failures
from .security_gate import extract_security_findings
from .qg.checks import QG_CHECKS
from . import merge_gate
from . import self_deploy
@@ -175,6 +176,8 @@ def advance_stage(
work_item_id: str,
branch: str,
finished_agent: str | None = None,
*,
confirm_deploy: bool = False,
) -> AdvanceResult:
"""Run the current stage's quality gate and advance / roll back the pipeline.
@@ -191,6 +194,13 @@ def advance_stage(
approved/REQUEST_CHANGES/tester/architect branches. In the
plane webhook path it is None, so those agent-specific
branches simply do not trigger (matches old plane behavior).
confirm_deploy: ORCH-059 — keyword-only signal that the human flipped the
issue to the dedicated "Confirm Deploy" status. ONLY this
signal initiates Phase B of the self-hosting prod deploy on
the `deploy` stage. A plain `Approved` on `deploy`
(confirm_deploy=False) is a deliberate no-op (no prod
deploy, no false БАГ-8 rollback). All non-webhook callers
leave it at the default.
Returns AdvanceResult describing what happened.
"""
@@ -207,21 +217,32 @@ def advance_stage(
result.note = "terminal"
return result
# --- ORCH-036 Phase B: human Approved on `deploy` -> initiate deploy --
# A human flipping the Plane status to Approved on the `deploy` stage
# (finished_agent is None) is the prod-deploy trigger for the self-hosting
# repo. Initiate the DETACHED host deploy + enqueue the finalizer and
# return WITHOUT running check_deploy_status (the verdict does not exist
# yet — running the gate now would read a stale/absent log and falsely
# roll back, R-2). The finalizer (Phase C, finished_agent="deployer")
# records the verdict later; that path is NOT intercepted here.
# --- ORCH-036/059 Phase B: "Confirm Deploy" on `deploy` -> initiate ----
# ORCH-059: the prod-deploy trigger is now the DEDICATED "Confirm Deploy"
# status (confirm_deploy=True), NOT the overloaded "Approved". On the
# `deploy` stage (finished_agent is None) for the self-hosting repo we
# always return early WITHOUT running check_deploy_status (the verdict
# does not exist yet — running the gate now would read a stale/absent log
# and falsely roll back, R-2/БАГ-8), but we only initiate the DETACHED
# host deploy + enqueue the finalizer when confirm_deploy is set. A plain
# Approved (confirm_deploy=False) is a deliberate no-op — it neither
# deploys nor rolls back (TRZ-3/AC-3). The finalizer (Phase C,
# finished_agent="deployer") records the verdict later; that path is NOT
# intercepted here (it requires finished_agent set).
if (
current_stage == "deploy"
and finished_agent is None
and settings.deploy_require_manual_approve
and self_deploy.self_deploy_applies(repo)
):
_handle_self_deploy_phase_b(task_id, repo, work_item_id, branch, result)
if confirm_deploy:
_handle_self_deploy_phase_b(task_id, repo, work_item_id, branch, result)
else:
result.note = "approved-on-deploy-noop"
logger.info(
f"Task {task_id}: Approved on `deploy` without Confirm Deploy "
f"— no-op (prod deploy requires the 'Confirm Deploy' status)"
)
return result
# --- Quality gate ----------------------------------------------------
@@ -281,6 +302,18 @@ def advance_stage(
# event. If it intervenes (defer on busy-lock, or rollback on conflict /
# red re-test) it owns the outcome and we return without advancing.
if current_stage == "deploy-staging":
# --- ORCH-022 security sub-gate (deploy-staging -> deploy edge) -----
# Run FIRST among the edge sub-gates (BEFORE the merge-gate and the
# image-freshness rebuild): it is cheap (read-only scan) and we want to
# fail BEFORE the expensive rebase/rebuild (07-infra I-6). Deterministic:
# gitleaks (offline secret-scan) + pip-audit (CVE audit). FAIL -> rollback
# to development + developer-retry (cap MAX_DEVELOPER_RETRIES). It owns
# the outcome on intervention (mirrors the merge-gate / image-freshness).
if _handle_security_gate(
task_id, current_stage, repo, work_item_id, branch, agent, result
):
return result
if _handle_merge_gate(
task_id, current_stage, repo, work_item_id, branch, agent, result
):
@@ -931,6 +964,93 @@ def _handle_merge_gate_rollback(
)
# ---------------------------------------------------------------------------
# ORCH-022: security sub-gate (secret-scan + dependency audit) on the
# deploy-staging -> deploy edge
# ---------------------------------------------------------------------------
def _handle_security_gate(
task_id, current_stage, repo, work_item_id, branch, agent, result: AdvanceResult
) -> bool:
"""Run check_security_gate on the deploy-staging -> deploy edge (ORCH-022).
Runs FIRST among the edge sub-gates — BEFORE the merge-gate and the
image-freshness rebuild — because it is a cheap read-only scan and we want to
fail BEFORE the expensive rebase/rebuild (07-infra I-6). Deterministic (no LLM):
gitleaks (offline secret-scan, fail-closed) + pip-audit (CVE audit, fail-open
degrade). The machine verdict lives in 17-security-report.md frontmatter.
Returns True if the gate INTERVENED (the caller must return without advancing):
* FAIL (secret found / blocking CVE / fail-closed) -> ROLLBACK to development
(+ developer retry, capped by MAX_DEVELOPER_RETRIES). No merge-lease release
here: the security-gate runs BEFORE the merge-gate, so the lease is not held
yet (distinct from the image-freshness rollback). The verbatim findings are
embedded into the developer's task_desc (ORCH-046 pattern, TC-17).
Returns False when the gate PASSED (clean, or N/A for a non-self repo with an
empty scope) so advance_stage proceeds to the merge-gate.
"""
passed, reason = _run_qg("check_security_gate", repo, work_item_id, branch)
if passed:
logger.info(f"Task {task_id}: security-gate passed ({reason})")
return False
result.qg_name = "check_security_gate"
result.qg_passed = False
result.qg_reason = reason
update_task_stage(task_id, "development")
notify_stage_change(task_id, current_stage, "development")
plane_notify_stage(work_item_id, current_stage, "development")
result.rolled_back_to = "development"
set_issue_in_progress(work_item_id)
notify_qg_failure(task_id, current_stage, "check_security_gate", reason)
plane_add_comment(
work_item_id,
f"❌ Security-гейт провален ({reason}). Откат на development. "
f"Developer нужен для фикса (секреты/уязвимые зависимости).",
author="deployer",
)
retry_count = _developer_retry_count(task_id)
if retry_count < MAX_DEVELOPER_RETRIES:
# ORCH-046: embed the verbatim findings into task_desc so the developer
# agent sees the must-fix substance directly (not just a link).
# extract_security_findings never raises; "" -> graceful link-only fallback.
report_ref = f"docs/work-items/{work_item_id}/17-security-report.md"
report_path = os.path.join(get_worktree_path(repo, branch), report_ref)
findings = extract_security_findings(report_path)
head = (
f"Work item: {work_item_id}\nRepo: {repo}\nBranch: {branch}\n"
f"Stage: development\nNote: Security-гейт провален "
f"(attempt {retry_count + 1}/{MAX_DEVELOPER_RETRIES}). "
f"Причина: {reason}."
)
if findings:
task_desc = (
f"{head}\nFindings:\n{findings}\n"
f"Полный контекст: {report_ref}"
)
else:
task_desc = f"{head} Fix findings in {report_ref}"
new_job = enqueue_job("developer", repo, task_desc, task_id=task_id)
result.enqueued_agent = "developer"
result.enqueued_job_id = new_job
logger.info(
f"Task {task_id}: security-gate FAILED, enqueued developer (job_id={new_job})"
)
else:
set_issue_blocked(work_item_id)
send_telegram(
f"\U0001f6a8 {work_item_id}: Security-гейт still failing after "
f"{MAX_DEVELOPER_RETRIES} developer retries ({reason}). "
f"Manual intervention needed."
)
result.alerted = True
logger.error(
f"Task {task_id}: security-gate FAILED, rolled back deploy-staging -> "
f"development ({reason})"
)
return True
# ---------------------------------------------------------------------------
# ORCH-058: staging-image freshness sub-gate on the deploy-staging -> deploy edge
# ---------------------------------------------------------------------------
@@ -1018,9 +1138,11 @@ def _handle_self_deploy_phase_a(
Staging is green and the branch is mergeable; for the self-hosting repo we do
NOT auto-deploy to prod. Move the task onto the `deploy` stage (so a later
human Approved lands there -> Phase B), set the issue approval-pending and ask
the human to flip the status to Approved. A restart-safe `approve-requested`
marker records that Phase A ran. The merge lease stays HELD.
human "Confirm Deploy" lands there -> Phase B), set the issue approval-pending
and ask the human to flip the status to "Confirm Deploy" (ORCH-059: the
dedicated prod-deploy trigger, distinct from the human-gate "Approved"). A
restart-safe `approve-requested` marker records that Phase A ran. The merge
lease stays HELD.
"""
update_task_stage(task_id, "deploy")
notify_stage_change(task_id, current_stage, "deploy")
@@ -1046,13 +1168,14 @@ def _handle_self_deploy_phase_a(
if work_item_id:
plane_add_comment(
work_item_id,
"\U0001f7e1 Staging зелёный. Требуется ручной approve для ПРОД-деплоя: "
"смените статус задачи на «Approved», чтобы запустить деплой в прод (8500).",
"\U0001f7e1 Staging зелёный. Требуется ручное подтверждение ПРОД-деплоя: "
"смените статус задачи на «Confirm Deploy», чтобы запустить деплой в прод "
"(8500). Статус «Approved» прод-деплой НЕ запускает.",
author="deployer",
)
send_telegram(
f"\U0001f7e1 {work_item_id}: staging OK. Ждёт approve на ПРОД-деплой "
f"(смените статус на Approved)."
f"\U0001f7e1 {work_item_id}: staging OK. Ждёт подтверждения ПРОД-деплоя "
f"(смените статус на «Confirm Deploy»)."
)
logger.info(
f"Task {task_id}: self-deploy Phase A — advanced to deploy, "

View File

@@ -155,8 +155,16 @@ async def handle_issue_updated(data: dict, project_id: str = ""):
# `to_analyse` aliases to the project's own `in_progress` UUID, so moving an
# enduro issue to In Progress still triggers start/resume (AC-17).
proj_states = get_project_states(project_id)
# ORCH-059: the dedicated "Confirm Deploy" status is the prod-deploy trigger.
# fail-closed via .get — environments without the status (enduro / API
# fallback) resolve to None, so the branch simply never activates (no KeyError,
# no blind deploy). Checked before `approved` so the two gestures never alias.
confirm_state = proj_states.get("confirm_deploy")
# ORCH-066: start/resume trigger is `To Analyse` (human entry-point).
if new_state == proj_states["to_analyse"]:
await handle_status_start(data, project_id)
elif confirm_state and new_state == confirm_state:
await handle_confirm_deploy(data, project_id)
elif new_state == proj_states["approved"]:
await handle_verdict(data, project_id, approved=True)
elif new_state == proj_states["rejected"]:
@@ -165,6 +173,45 @@ async def handle_issue_updated(data: dict, project_id: str = ""):
logger.info(f"issue {plane_id} updated to state {new_state[:8]}..., no pipeline action")
async def handle_confirm_deploy(data: dict, project_id: str = ""):
"""ORCH-059: a human flipped the issue to the dedicated "Confirm Deploy"
status — the explicit trigger for the self-hosting prod deploy (Phase B).
Guarded to the `deploy` stage: "Confirm Deploy" is only meaningful on the
approval-pending `deploy` stage (Phase A advanced the task there). On any
other stage it is a no-op-with-log, so a stray Confirm Deploy can never
perturb another gate.
Routes to the unified stage engine with ``confirm_deploy=True`` so ONLY this
path initiates Phase B; a plain Approved on `deploy` stays a no-op (TRZ-3).
"""
plane_id = str(data.get("id") or "")
task = get_task_by_plane_id(plane_id)
if not task:
logger.warning(f"Confirm Deploy for {plane_id} but no task found, ignoring")
return
task_id = task["id"]
current_stage = task["stage"]
repo = task["repo"]
work_item_id = task.get("work_item_id", "")
branch = task.get("branch", "")
if current_stage != "deploy":
logger.info(
f"Confirm Deploy for {plane_id} but stage is '{current_stage}' "
f"(not 'deploy'); no-op"
)
return
logger.info(
f"Task {task_id}: Confirm Deploy status on `deploy` -> initiate Phase B prod deploy"
)
await _try_advance_stage(
task_id, current_stage, repo, work_item_id, branch, confirm_deploy=True
)
async def handle_status_start(data: dict, project_id: str = ""):
"""An issue moved into In Progress.
@@ -649,7 +696,8 @@ async def _rollback_stage(
async def _try_advance_stage(
task_id: int, current_stage: str, repo: str, work_item_id: str, branch: str
task_id: int, current_stage: str, repo: str, work_item_id: str, branch: str,
confirm_deploy: bool = False,
):
"""Thin async wrapper over the unified stage engine (ORCH-4 / M-3).
@@ -658,10 +706,15 @@ async def _try_advance_stage(
is synchronous. We run it off the event loop via asyncio.to_thread so there
is exactly one implementation shared with the launcher.
finished_agent is None on this webhook path (a human Approved status change,
not a finished agent), so the agent-specific rollback branches inside the
engine intentionally do not trigger — the webhook path only runs the QG and
either advances or reports the failure.
finished_agent is None on this webhook path (a human status change, not a
finished agent), so the agent-specific rollback branches inside the engine
intentionally do not trigger — the webhook path only runs the QG and either
advances or reports the failure.
ORCH-059: ``confirm_deploy`` is threaded through (keyword-only on
advance_stage). It is True ONLY on the "Confirm Deploy" path
(handle_confirm_deploy) and gates Phase B of the self-hosting prod deploy; the
plain Approved path (handle_verdict) leaves it at the default False.
"""
import asyncio
from ..stage_engine import advance_stage
@@ -674,6 +727,7 @@ async def _try_advance_stage(
work_item_id,
branch,
None,
confirm_deploy=confirm_deploy,
)

View File

@@ -235,6 +235,7 @@ def test_tc19_qg_checks_registry_unchanged():
"check_staging_status",
"check_branch_mergeable",
"check_staging_image_fresh",
"check_security_gate",
}

View File

@@ -0,0 +1,187 @@
"""ORCH-059 TC-10/11/12: end-to-end routing from a Plane webhook payload through
handle_issue_updated into the stage engine, with the host deploy mocked.
Contract (AC-2, AC-3, AC-8):
* TC-10 — task on `deploy` + webhook "Confirm Deploy" -> initiate_deploy called,
`deploy-finalizer` enqueued, `initiated` marker written.
* TC-11 — task on `deploy` + webhook "Approved" -> NO prod deploy initiated, the
task stays on `deploy` (no rollback, no advance to done).
* TC-12 — non-self repo: verdict statuses on `deploy` do not change deploy
behaviour (self_deploy_applies == False; the confirm-deploy branch is inert).
"""
import os
import tempfile
import pytest
_test_db = os.path.join(tempfile.gettempdir(), "test_orch_confirm_e2e.db")
os.environ["ORCH_DB_PATH"] = _test_db
os.environ["ORCH_REPOS_DIR"] = tempfile.gettempdir()
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_GITEA_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
import src.plane_sync as plane_sync # noqa: E402
import src.webhooks.plane as wh # noqa: E402
IN_PROGRESS = "11111111-1111-1111-1111-111111111111"
APPROVED = "22222222-2222-2222-2222-222222222222"
REJECTED = "33333333-3333-3333-3333-333333333333"
CONFIRM = "44444444-4444-4444-4444-444444444444"
# ORCH project: Confirm Deploy resolved. enduro-like project: NO confirm_deploy key.
_STATES_SELF = {
"in_progress": IN_PROGRESS,
"to_analyse": IN_PROGRESS, # ORCH-066 integ: always present (alias)
# ORCH-066 integ: full status-model keys alias to base UUIDs,
# mirroring get_project_states() _STATE_ALIAS_FALLBACK in production.
"analysis": IN_PROGRESS,
"code_review": APPROVED,
"awaiting_deploy": IN_PROGRESS,
"deploying": IN_PROGRESS,
"monitoring": APPROVED,
"approved": APPROVED,
"rejected": REJECTED,
"confirm_deploy": CONFIRM,
}
_STATES_NONSELF = {
"in_progress": IN_PROGRESS,
"to_analyse": IN_PROGRESS, # ORCH-066 integ: always present (alias)
# ORCH-066 integ: full status-model keys alias to base UUIDs,
# mirroring get_project_states() _STATE_ALIAS_FALLBACK in production.
"analysis": IN_PROGRESS,
"code_review": APPROVED,
"awaiting_deploy": IN_PROGRESS,
"deploying": IN_PROGRESS,
"monitoring": APPROVED,
"approved": APPROVED,
"rejected": REJECTED,
}
@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()
monkeypatch.setattr(self_deploy.settings, "repos_dir", str(tmp_path))
monkeypatch.setattr(self_deploy.settings, "host_repos_dir", str(tmp_path))
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
yield
@pytest.fixture(autouse=True)
def silence_engine(monkeypatch):
for name in (
"notify_stage_change", "notify_qg_failure", "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",
):
monkeypatch.setattr(stage_engine, name, MagicMock(), raising=False)
def _make_task(stage, repo, branch, wi, plane_id):
conn = get_db()
cur = conn.execute(
"INSERT INTO tasks (plane_id, work_item_id, repo, branch, stage) "
"VALUES (?, ?, ?, ?, ?)",
(plane_id, 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 FROM jobs ORDER BY id").fetchall()
conn.close()
return [r[0] for r in rows]
def _payload(state_uuid, plane_id):
return {"id": plane_id, "state": {"id": state_uuid}}
# ---------------------------------------------------------------------------
# TC-10: E2E Confirm Deploy -> prod deploy initiated
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc10_confirm_deploy_e2e_initiates(monkeypatch):
monkeypatch.setattr(plane_sync, "get_project_states", lambda pid: _STATES_SELF)
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
task_id = _make_task("deploy", "orchestrator", "feature/ORCH-059-x",
"ORCH-059", "plane-ORCH-059")
await wh.handle_issue_updated(_payload(CONFIRM, "plane-ORCH-059"), "orch-proj")
initiate.assert_called_once()
assert "deploy-finalizer" in _jobs()
assert self_deploy.has_marker("orchestrator", "ORCH-059", self_deploy.INITIATED)
# Verdict comes later via the finalizer — still on `deploy`.
assert _stage(task_id) == "deploy"
# ---------------------------------------------------------------------------
# TC-11: E2E Approved -> no prod deploy, task stays on deploy
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc11_approved_e2e_noop(monkeypatch):
monkeypatch.setattr(plane_sync, "get_project_states", lambda pid: _STATES_SELF)
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
task_id = _make_task("deploy", "orchestrator", "feature/ORCH-059-x",
"ORCH-059", "plane-ORCH-059")
await wh.handle_issue_updated(_payload(APPROVED, "plane-ORCH-059"), "orch-proj")
initiate.assert_not_called()
assert "deploy-finalizer" not in _jobs()
assert _stage(task_id) == "deploy" # no rollback, no advance to done
assert not self_deploy.has_marker("orchestrator", "ORCH-059", self_deploy.INITIATED)
# ---------------------------------------------------------------------------
# TC-12: non-self repo -> confirm-deploy branch inert (fail-closed, no key)
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc12_non_self_repo_unaffected(monkeypatch):
# Non-self project has no confirm_deploy key at all -> the branch never fires.
monkeypatch.setattr(plane_sync, "get_project_states", lambda pid: _STATES_NONSELF)
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
# Stub the deploy gate so the legacy non-self path stays deterministic (no
# real git/network); its verdict is irrelevant to this test's assertions.
monkeypatch.setattr(
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS, "check_deploy_status": lambda *a, **k: (True, "ok")},
)
task_id = _make_task("deploy", "enduro-trails", "feature/ET-009-x",
"ET-009", "plane-ET-009")
# An Approved on a non-self deploy task does not initiate self-deploy logic.
await wh.handle_issue_updated(_payload(APPROVED, "plane-ET-009"), "enduro-proj")
initiate.assert_not_called()
# The (absent) Confirm Deploy status simply maps to no pipeline action.
assert self_deploy.self_deploy_applies("enduro-trails") is False

View File

@@ -104,6 +104,7 @@ def test_tc05_no_approve_does_not_call_prod_hook(monkeypatch):
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},
)
@@ -145,12 +146,14 @@ def test_tc06_approved_calls_prod_hook_exactly_once(monkeypatch):
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 Approved
task_id = _make_task("deploy") # already on deploy, awaiting Confirm Deploy
# 1st human Approved -> Phase B initiates the detached 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,
"feature/ORCH-036-x", finished_agent=None, confirm_deploy=True,
)
assert res1.note == "self-deploy-initiated"
assert ssh_run.call_count == 1
@@ -160,10 +163,10 @@ def test_tc06_approved_calls_prod_hook_exactly_once(monkeypatch):
# ORCH-066 AC-7: Phase B indicates `Deploying` on a successful initiate.
stage_engine.set_issue_deploying.assert_called_once_with("ORCH-036")
# 2nd (duplicate) Approved -> idempotent no-op, hook NOT called again.
# 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,
"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

View File

@@ -0,0 +1,162 @@
"""ORCH-059 TC-04/05/06: webhook routing for the dedicated "Confirm Deploy"
status vs. the overloaded "Approved".
Contract (AC-2, AC-3, AC-4):
* TC-04 — handle_issue_updated routes a "Confirm Deploy" status on a `deploy`
task to the Phase B path (handle_confirm_deploy -> advance_stage with
confirm_deploy=True), NOT the plain approve/advance path.
* TC-05 — an "Approved" status on a `deploy` task does NOT initiate the prod
deploy (self_deploy.initiate_deploy is never called).
* TC-06 — an "Approved" status on an `analysis` task still advances
analysis -> architecture (the approved-via-status human gate is intact).
"""
import os
import tempfile
import pytest
_test_db = os.path.join(tempfile.gettempdir(), "test_orch_confirm_routing.db")
os.environ["ORCH_DB_PATH"] = _test_db
os.environ["ORCH_REPOS_DIR"] = tempfile.gettempdir()
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
from unittest.mock import AsyncMock, 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
import src.plane_sync as plane_sync # noqa: E402
import src.webhooks.plane as wh # noqa: E402
IN_PROGRESS = "11111111-1111-1111-1111-111111111111"
APPROVED = "22222222-2222-2222-2222-222222222222"
REJECTED = "33333333-3333-3333-3333-333333333333"
CONFIRM = "44444444-4444-4444-4444-444444444444"
_STATES = {
"in_progress": IN_PROGRESS,
# ORCH-066 integ: real get_project_states always exposes to_analyse
# (aliased to in_progress on projects without the dedicated status).
"to_analyse": IN_PROGRESS,
# ORCH-066 integ: full status-model keys alias to base UUIDs,
# mirroring get_project_states() _STATE_ALIAS_FALLBACK in production.
"analysis": IN_PROGRESS,
"code_review": APPROVED,
"awaiting_deploy": IN_PROGRESS,
"deploying": IN_PROGRESS,
"monitoring": APPROVED,
"approved": APPROVED,
"rejected": REJECTED,
"confirm_deploy": CONFIRM,
}
@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()
# Deterministic per-project states (no network). handle_issue_updated imports
# get_project_states locally from ..plane_sync, so patch it at the source.
monkeypatch.setattr(plane_sync, "get_project_states", lambda pid: _STATES)
# Isolate sentinel dirs.
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_engine(monkeypatch):
for name in (
"notify_stage_change", "notify_qg_failure", "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",
):
monkeypatch.setattr(stage_engine, name, MagicMock(), raising=False)
def _make_task(stage, repo="orchestrator", branch="feature/ORCH-059-x",
wi="ORCH-059", plane_id="plane-ORCH-059"):
conn = get_db()
cur = conn.execute(
"INSERT INTO tasks (plane_id, work_item_id, repo, branch, stage) "
"VALUES (?, ?, ?, ?, ?)",
(plane_id, wi, repo, branch, stage),
)
task_id = cur.lastrowid
conn.commit()
conn.close()
return task_id
def _payload(state_uuid, plane_id="plane-ORCH-059"):
return {"id": plane_id, "state": {"id": state_uuid}}
# ---------------------------------------------------------------------------
# TC-04: "Confirm Deploy" routes to the Phase B path with confirm_deploy=True
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc04_confirm_deploy_routes_phase_b(monkeypatch):
_make_task("deploy")
spy = AsyncMock()
monkeypatch.setattr(wh, "_try_advance_stage", spy)
# handle_verdict must NOT be taken for the confirm-deploy status.
verdict_spy = AsyncMock()
monkeypatch.setattr(wh, "handle_verdict", verdict_spy)
await wh.handle_issue_updated(_payload(CONFIRM), "proj")
spy.assert_awaited_once()
# confirm_deploy=True must be threaded through.
assert spy.await_args.kwargs.get("confirm_deploy") is True
verdict_spy.assert_not_awaited()
@pytest.mark.asyncio
async def test_tc04b_confirm_deploy_off_deploy_stage_is_noop(monkeypatch):
"""Guard: a stray "Confirm Deploy" on a non-deploy stage is a no-op (no advance)."""
_make_task("analysis")
spy = AsyncMock()
monkeypatch.setattr(wh, "_try_advance_stage", spy)
await wh.handle_confirm_deploy(_payload(CONFIRM), "proj")
spy.assert_not_awaited()
# ---------------------------------------------------------------------------
# TC-05: "Approved" on `deploy` does NOT initiate the prod deploy
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc05_approved_on_deploy_does_not_initiate(monkeypatch):
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
_make_task("deploy")
initiate = MagicMock()
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
# Real routing: Approved -> handle_verdict -> _try_advance_stage(confirm_deploy=False)
# -> advance_stage -> the deploy block no-ops (does not initiate).
await wh.handle_issue_updated(_payload(APPROVED), "proj")
initiate.assert_not_called()
# ---------------------------------------------------------------------------
# TC-06: "Approved" on `analysis` still advances analysis -> architecture
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_tc06_approved_on_analysis_still_advances(monkeypatch):
task_id = _make_task("analysis")
await wh.handle_issue_updated(_payload(APPROVED), "proj")
conn = get_db()
stage = conn.execute("SELECT stage FROM tasks WHERE id=?", (task_id,)).fetchone()[0]
conn.close()
assert stage == "architecture"

120
tests/test_plane_states.py Normal file
View File

@@ -0,0 +1,120 @@
"""ORCH-059 TC-01/02/03: resolver registration of the dedicated "Confirm Deploy"
status and its fail-closed absence in fallback environments.
Contract (AC-1, AC-7):
* TC-01 — _PLANE_NAME_TO_KEY maps the board name "Confirm Deploy" to the logical
key "confirm_deploy".
* TC-02 — get_project_states for an ORCH-like project (Plane API mocked to
include a "Confirm Deploy" state) returns a NON-empty uuid under
"confirm_deploy", distinct from "approved".
* TC-03 — fail-closed: when the status is absent (API fallback to
_DEFAULT_STATES / unreachable Plane), the key is simply missing and a .get
access yields None WITHOUT raising — the confirm-deploy branch never activates.
"""
import os
import tempfile
import pytest
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
os.environ["ORCH_DB_PATH"] = os.path.join(tempfile.gettempdir(), "test_orch_plane_states.db")
import src.plane_sync as plane_sync # noqa: E402
from src.plane_sync import ( # noqa: E402
_PLANE_NAME_TO_KEY,
_DEFAULT_STATES,
get_project_states,
reload_project_states,
)
@pytest.fixture(autouse=True)
def fresh_cache():
reload_project_states()
yield
reload_project_states()
# ---------------------------------------------------------------------------
# TC-01: name -> key mapping is registered
# ---------------------------------------------------------------------------
def test_tc01_confirm_deploy_name_to_key_mapping():
assert _PLANE_NAME_TO_KEY.get("Confirm Deploy") == "confirm_deploy"
def test_tc01_confirm_deploy_not_in_default_states():
"""Fail-closed by construction: NO fallback UUID exists for confirm_deploy, so
enduro / API-fallback environments never resolve a (wrong) deploy trigger."""
assert "confirm_deploy" not in _DEFAULT_STATES
# ---------------------------------------------------------------------------
# TC-02: live API resolves a real, distinct uuid for an ORCH-like project
# ---------------------------------------------------------------------------
def test_tc02_get_project_states_resolves_confirm_deploy(monkeypatch):
confirm_uuid = "cfd00000-0000-0000-0000-000000000059"
approved_uuid = "a519a341-dada-4a91-8910-7604f82b79c5"
class _Resp:
def raise_for_status(self):
pass
def json(self):
return {
"results": [
{"name": "In Progress", "id": "b873d9eb-993c-48cd-97ac-99a9b1623967"},
{"name": "Approved", "id": approved_uuid},
{"name": "Confirm Deploy", "id": confirm_uuid},
]
}
monkeypatch.setattr(plane_sync.httpx, "get", lambda *a, **k: _Resp())
states = get_project_states("orch-project-uuid")
assert states.get("confirm_deploy") == confirm_uuid
# Distinct gestures: confirm-deploy must NOT alias the human "Approved" gate.
assert states["confirm_deploy"] != states["approved"]
# ---------------------------------------------------------------------------
# TC-03: fail-closed when the status is absent (API fallback / unreachable)
# ---------------------------------------------------------------------------
def test_tc03_fail_closed_when_api_unreachable(monkeypatch):
"""A Plane outage -> get_project_states falls back to _DEFAULT_STATES, which
has no confirm_deploy key. .get must yield None, never raise."""
def _boom(*a, **k):
raise RuntimeError("plane down")
monkeypatch.setattr(plane_sync.httpx, "get", _boom)
states = get_project_states("any-project-uuid")
# No KeyError, branch never activates.
assert states.get("confirm_deploy") is None
# The human gate "Approved" still resolves (fallback is intact).
assert states.get("approved") == _DEFAULT_STATES["approved"]
def test_tc03_fail_closed_when_status_not_on_board(monkeypatch):
"""Project whose board lacks "Confirm Deploy": the key is filled by NEITHER the
API loop NOR the _DEFAULT_STATES backfill -> absent -> fail-closed."""
class _Resp:
def raise_for_status(self):
pass
def json(self):
return {
"results": [
{"name": "In Progress", "id": "b873d9eb-993c-48cd-97ac-99a9b1623967"},
{"name": "Approved", "id": "a519a341-dada-4a91-8910-7604f82b79c5"},
]
}
monkeypatch.setattr(plane_sync.httpx, "get", lambda *a, **k: _Resp())
states = get_project_states("board-without-confirm")
assert states.get("confirm_deploy") is None
assert states.get("approved") == "a519a341-dada-4a91-8910-7604f82b79c5"

View File

@@ -0,0 +1,180 @@
"""ORCH-068 (TR-4): tests for the Plane states cache TTL self-heal.
The per-project ``_STATES_CACHE`` used to live for the whole process lifetime,
so a status added to Plane after start was never seen without a restart
("stale set -> no pipeline action"). ORCH-068 adds a TTL: an entry is
re-fetched once it is older than ``plane_states_ttl_s`` (default 300s); ``0``
disables the TTL (strictly the previous lifetime cache).
All tests are offline: the Plane API (httpx) and the monotonic clock are mocked.
"""
import os
import tempfile
from unittest.mock import MagicMock, patch
import pytest
os.environ.setdefault("ORCH_PLANE_API_URL", "http://plane.local")
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_PLANE_WORKSPACE_SLUG", "test-ws")
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
_test_db = os.path.join(tempfile.gettempdir(), "test_plane_states_cache.db")
os.environ["ORCH_DB_PATH"] = _test_db
import src.plane_sync as ps # noqa: E402
_PROJECT = "proj-ttl"
_ET_PROJECT = "7a79f0a9-5278-49cd-9007-9a338f238f9c"
def _resp(data: dict, status: int = 200):
m = MagicMock()
m.status_code = status
m.json.return_value = data
if status >= 400:
from httpx import HTTPStatusError
m.raise_for_status.side_effect = HTTPStatusError(
"error", request=MagicMock(), response=MagicMock()
)
else:
m.raise_for_status.return_value = None
return m
def _states_response(in_progress_uuid: str) -> dict:
"""A minimal /states/ payload; In Progress carries the given UUID."""
return {
"results": [
{"id": in_progress_uuid, "name": "In Progress", "group": "started"},
{"id": "uuid-done", "name": "Done", "group": "completed"},
]
}
@pytest.fixture(autouse=True)
def reset_cache():
ps.reload_project_states()
yield
ps.reload_project_states()
# ---------------------------------------------------------------------------
# TC-11 (AC-12): a stale cache entry self-heals after the TTL — no restart.
# ---------------------------------------------------------------------------
def test_tc11_stale_cache_refreshes_after_ttl(monkeypatch):
monkeypatch.setattr(ps.settings, "plane_states_ttl_s", 300)
clock = {"t": 1000.0}
monkeypatch.setattr(ps.time, "monotonic", lambda: clock["t"])
responses = iter([
_resp(_states_response("uuid-A")), # first fetch: old set
_resp(_states_response("uuid-B")), # second fetch: new status appeared
])
mock_get = MagicMock(side_effect=lambda *a, **k: next(responses))
monkeypatch.setattr(ps.httpx, "get", mock_get)
# t=1000: first call -> fetch set A.
s1 = ps.get_project_states(_PROJECT)
assert s1["in_progress"] == "uuid-A"
assert mock_get.call_count == 1
# t=1100: within TTL -> served from cache, no new fetch.
clock["t"] = 1100.0
s2 = ps.get_project_states(_PROJECT)
assert s2["in_progress"] == "uuid-A"
assert mock_get.call_count == 1
# t=1400: TTL (300s) elapsed -> re-fetch -> fresh set B (self-heal).
clock["t"] = 1400.0
s3 = ps.get_project_states(_PROJECT)
assert s3["in_progress"] == "uuid-B"
assert mock_get.call_count == 2
def test_tc11_ttl_zero_keeps_lifetime_cache(monkeypatch):
"""plane_states_ttl_s=0 -> strictly the previous lifetime cache (back-compat)."""
monkeypatch.setattr(ps.settings, "plane_states_ttl_s", 0)
clock = {"t": 1000.0}
monkeypatch.setattr(ps.time, "monotonic", lambda: clock["t"])
responses = iter([
_resp(_states_response("uuid-A")),
_resp(_states_response("uuid-B")),
])
mock_get = MagicMock(side_effect=lambda *a, **k: next(responses))
monkeypatch.setattr(ps.httpx, "get", mock_get)
assert ps.get_project_states(_PROJECT)["in_progress"] == "uuid-A"
clock["t"] = 1_000_000.0 # far in the future
# TTL disabled -> still the cached A, never re-fetched.
assert ps.get_project_states(_PROJECT)["in_progress"] == "uuid-A"
assert mock_get.call_count == 1
def test_tc11_groups_exposed_via_accessor(monkeypatch):
"""get_project_state_groups returns {uuid -> group} from the same record."""
monkeypatch.setattr(ps.settings, "plane_states_ttl_s", 300)
monkeypatch.setattr(ps.httpx, "get", lambda *a, **k: _resp(_states_response("uuid-A")))
ps.get_project_states(_PROJECT)
groups = ps.get_project_state_groups(_PROJECT)
assert groups["uuid-A"] == "started"
assert groups["uuid-done"] == "completed"
def test_tc11_groups_empty_when_uncached(monkeypatch):
"""No cache record (e.g. API fell back to defaults) -> groups == {}."""
assert ps.get_project_state_groups("never-fetched") == {}
# ---------------------------------------------------------------------------
# TC-12 (AC-13): default-config compatibility — enduro UUIDs + API-error fallback.
# ---------------------------------------------------------------------------
def test_tc12_enduro_uuids_unchanged(monkeypatch):
"""enduro project still resolves its own UUIDs (return shape unchanged)."""
body = {
"results": [
{"id": "b873d9eb-993c-48cd-97ac-99a9b1623967",
"name": "In Progress", "group": "started"},
]
}
monkeypatch.setattr(ps.httpx, "get", lambda *a, **k: _resp(body))
states = ps.get_project_states(_ET_PROJECT)
assert states["in_progress"] == "b873d9eb-993c-48cd-97ac-99a9b1623967"
# Missing keys are still backfilled from _DEFAULT_STATES (complete mapping).
assert states["done"] == ps._DEFAULT_STATES["done"]
def test_tc12_api_error_falls_back_to_defaults(monkeypatch):
"""API failure with nothing cached -> _DEFAULT_STATES (fallback preserved)."""
monkeypatch.setattr(
ps.httpx, "get", MagicMock(side_effect=Exception("network error"))
)
states = ps.get_project_states(_PROJECT)
assert states is ps._DEFAULT_STATES
def test_tc12_stale_served_when_refresh_fails(monkeypatch):
"""TTL expiry + transient API failure -> serve the stale (project-correct)
set rather than reverting to enduro defaults."""
monkeypatch.setattr(ps.settings, "plane_states_ttl_s", 300)
clock = {"t": 1000.0}
monkeypatch.setattr(ps.time, "monotonic", lambda: clock["t"])
calls = {"n": 0}
def flaky_get(*a, **k):
calls["n"] += 1
if calls["n"] == 1:
return _resp(_states_response("uuid-A"))
raise Exception("transient outage")
monkeypatch.setattr(ps.httpx, "get", flaky_get)
assert ps.get_project_states(_PROJECT)["in_progress"] == "uuid-A"
clock["t"] = 2000.0 # past TTL -> refresh attempt fails
states = ps.get_project_states(_PROJECT)
assert states["in_progress"] == "uuid-A" # stale-but-correct, not defaults
assert states is not ps._DEFAULT_STATES

View File

@@ -138,6 +138,7 @@ def test_tc23_qg_checks_registry_unchanged():
"check_ci_green", "check_review_approved", "check_tests_passed",
"check_reviewer_verdict", "check_tests_local", "check_deploy_status",
"check_staging_status", "check_branch_mergeable", "check_staging_image_fresh",
"check_security_gate", # ORCH-022 integ: security-gate registered
}

View File

@@ -30,6 +30,7 @@ _EXPECTED_QGS = {
"check_staging_status",
"check_branch_mergeable", # ORCH-043 merge-gate (deploy-staging -> deploy edge)
"check_staging_image_fresh", # ORCH-058 image-freshness sub-gate (same edge)
"check_security_gate", # ORCH-022 security sub-gate (same edge, run FIRST)
}

113
tests/test_qg_security.py Normal file
View File

@@ -0,0 +1,113 @@
"""ORCH-022 / TC-13..TC-15: the security-gate QG wrapper + registry wiring.
Covers the thin ``check_security_gate`` registry wrapper in src/qg/checks.py (its
conditionality fast-paths) and that the new check is registered + dispatched by
``_run_qg``. The deterministic core (scan / verdict / frontmatter) is covered in
tests/test_security_gate.py.
"""
import os
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
from src import security_gate as sg # noqa: E402
from src.qg import checks as qg # noqa: E402
from src.qg.checks import QG_CHECKS, check_security_gate # noqa: E402
_WI = "ORCH-022"
_BRANCH = "feature/ORCH-022-x"
# ---------------------------------------------------------------------------
# TC-13 — non-self repo with empty scope -> N/A fast pass (no scanner run).
# ---------------------------------------------------------------------------
def test_tc13_non_self_repo_empty_scope_is_na(monkeypatch):
"""TC-13: a non-self repo with an empty scope -> (True, 'security-gate N/A
for <repo>') immediately, WITHOUT invoking the scanners."""
monkeypatch.setattr(sg.settings, "security_gate_enabled", True)
monkeypatch.setattr(sg.settings, "security_gate_repos", "")
called = {"scan": False}
def _should_not_run(*a, **k):
called["scan"] = True
raise AssertionError("scanner must not run for an N/A repo")
monkeypatch.setattr(sg, "scan_secrets", _should_not_run)
monkeypatch.setattr(sg, "audit_dependencies", _should_not_run)
ok, reason = check_security_gate("enduro-trails", _WI, _BRANCH)
assert ok is True
assert "N/A" in reason
assert "enduro-trails" in reason
assert called["scan"] is False
# ---------------------------------------------------------------------------
# TC-14 — kill-switch disabled -> no-op pass.
# ---------------------------------------------------------------------------
def test_tc14_disabled_is_noop_pass(monkeypatch):
"""TC-14: ORCH_SECURITY_GATE_ENABLED=false -> no-op pass (True), scanners untouched."""
monkeypatch.setattr(sg.settings, "security_gate_enabled", False)
def _should_not_run(*a, **k):
raise AssertionError("scanner must not run when the gate is disabled")
monkeypatch.setattr(sg, "scan_secrets", _should_not_run)
monkeypatch.setattr(sg, "audit_dependencies", _should_not_run)
ok, reason = check_security_gate("orchestrator", _WI, _BRANCH)
assert ok is True
assert "disabled" in reason.lower()
# ---------------------------------------------------------------------------
# TC-15 — registered in QG_CHECKS + dispatched by _run_qg.
# ---------------------------------------------------------------------------
def test_tc15_registered_in_qg_checks():
"""TC-15a: the new check is registered and callable."""
assert "check_security_gate" in QG_CHECKS
assert QG_CHECKS["check_security_gate"] is check_security_gate
assert callable(QG_CHECKS["check_security_gate"])
def test_tc15_dispatched_by_run_qg(monkeypatch):
"""TC-15b: _run_qg routes 'check_security_gate' with the (repo, work_item_id,
branch) signature to the registered wrapper."""
from src import stage_engine
captured = {}
def _fake(repo, work_item_id, branch):
captured["args"] = (repo, work_item_id, branch)
return True, "ok"
monkeypatch.setitem(stage_engine.QG_CHECKS, "check_security_gate", _fake)
passed, reason = stage_engine._run_qg("check_security_gate", "orchestrator", _WI, _BRANCH)
assert passed is True
assert captured["args"] == ("orchestrator", _WI, _BRANCH)
def test_security_gate_applies_scope(monkeypatch):
"""Conditionality matrix mirrors merge_gate_applies / image_freshness_applies."""
monkeypatch.setattr(sg.settings, "security_gate_enabled", True)
# Empty scope -> only the self-hosting repo.
monkeypatch.setattr(sg.settings, "security_gate_repos", "")
assert sg.security_gate_applies("orchestrator") is True
assert sg.security_gate_applies("enduro-trails") is False
# Explicit CSV scope -> only the listed repos (case-insensitive).
monkeypatch.setattr(sg.settings, "security_gate_repos", "enduro-trails, foo")
assert sg.security_gate_applies("enduro-trails") is True
assert sg.security_gate_applies("orchestrator") is False
# Kill-switch wins over everything.
monkeypatch.setattr(sg.settings, "security_gate_enabled", False)
assert sg.security_gate_applies("orchestrator") is False
def test_qg_wrapper_delegates(monkeypatch):
"""The QG wrapper delegates to security_gate.check_security_gate verbatim."""
monkeypatch.setattr(sg, "check_security_gate", lambda r, w, b: (False, "delegated FAIL"))
ok, reason = check_security_gate("orchestrator", _WI, _BRANCH)
assert ok is False
assert reason == "delegated FAIL"

View File

@@ -341,3 +341,346 @@ def test_tc17_polls_all_projects_resolves_states_per_project(monkeypatch):
# state uuids are resolved per-project (not hardcoded): each call carries them.
for _pid, states in issues_calls:
assert set(states) == {_IN_PROGRESS, _APPROVED, _REJECTED}
# ===========================================================================
# ORCH-068: livelock-fix — terminal exclusion (D1) + confirmed-change unblock
# (D2) + dedup (TR-3). The old code spammed `_note_unblock` every ~120s for a
# fully synchronized Done task (incident: ET-002, 191+ Telegram messages/night).
# ===========================================================================
_DONE = "uuid-done"
_CANCELLED = "uuid-cancelled"
def _patch_states_with_terminals(monkeypatch, *, alias_done_to_approved=False):
"""Patch F-2 state resolution to include terminals + their groups.
``alias_done_to_approved`` models the regression trigger (ORCH-066): the
project "collapses" Done onto the approved UUID, so a genuinely-Done issue
would enter the ``approved`` branch by UUID. Only the state GROUP
(``completed``) disentangles it — the heart of D1.
"""
done_uuid = _APPROVED if alias_done_to_approved else _DONE
states = {
"in_progress": _IN_PROGRESS,
# ORCH-066 integ: real get_project_states() always exposes to_analyse,
# aliased to in_progress on projects without the dedicated status.
"to_analyse": _IN_PROGRESS,
"approved": _APPROVED,
"rejected": _REJECTED,
"done": done_uuid,
"cancelled": _CANCELLED,
}
groups = {
_IN_PROGRESS: "started",
_APPROVED: "started",
_REJECTED: "started",
done_uuid: "completed", # genuinely-done issue -> completed group
_CANCELLED: "cancelled",
}
monkeypatch.setattr(reconciler_mod, "get_project_states", lambda pid: states)
monkeypatch.setattr(
reconciler_mod, "get_project_state_groups", lambda pid: groups
)
return states, groups
def _spy_telegram(monkeypatch):
sent = []
monkeypatch.setattr(reconciler_mod, "send_telegram", lambda msg: sent.append(msg))
return sent
def _job_count():
conn = get_db()
n = conn.execute("SELECT COUNT(*) FROM jobs").fetchone()[0]
conn.close()
return n
# ---------------------------------------------------------------------------
# TC-01 (AC-1, AC-7): synchronized Done task -> total silence, 0 jobs.
# ---------------------------------------------------------------------------
def test_tc01_synced_done_is_silent(monkeypatch, single_project):
start, verdict = _patch_handlers(monkeypatch)
_patch_states_with_terminals(monkeypatch)
sent = _spy_telegram(monkeypatch)
_make_task("iss-done", stage="done", wi="ET-002")
_patch_issues(monkeypatch, [
{"id": "iss-done", "state": {"id": _DONE}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
start.assert_not_called()
verdict.assert_not_called()
assert sent == []
assert recon.unblocked_total == 0
assert recon.skipped_terminal_total == 1
assert _job_count() == 0
# ---------------------------------------------------------------------------
# TC-02 (AC-2): Done UUID aliased onto approved -> still excluded by GROUP.
# ---------------------------------------------------------------------------
def test_tc02_terminal_aliased_to_approved_excluded(monkeypatch, single_project):
start, verdict = _patch_handlers(monkeypatch)
_patch_states_with_terminals(monkeypatch, alias_done_to_approved=True)
sent = _spy_telegram(monkeypatch)
# Task is Done; its Plane state UUID equals the approved UUID (aliasing).
_make_task("iss-alias", stage="done", wi="ET-002")
_patch_issues(monkeypatch, [
{"id": "iss-alias", "state": {"id": _APPROVED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
# Without the group check this would enter the approved branch and notify.
start.assert_not_called()
verdict.assert_not_called()
assert sent == []
assert recon.unblocked_total == 0
assert recon.skipped_terminal_total == 1
# ---------------------------------------------------------------------------
# TC-03 (AC-2): Cancelled terminal is also excluded.
# ---------------------------------------------------------------------------
def test_tc03_cancelled_excluded(monkeypatch, single_project):
start, verdict = _patch_handlers(monkeypatch)
_patch_states_with_terminals(monkeypatch)
sent = _spy_telegram(monkeypatch)
_make_task("iss-cancel", stage="done", wi="ET-003")
_patch_issues(monkeypatch, [
{"id": "iss-cancel", "state": {"id": _CANCELLED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
start.assert_not_called()
verdict.assert_not_called()
assert sent == []
assert recon.unblocked_total == 0
assert recon.skipped_terminal_total == 1
# ---------------------------------------------------------------------------
# TC-04 (AC-3): no-op dispatch (stage unchanged) -> no notification.
# ---------------------------------------------------------------------------
def test_tc04_noop_dispatch_no_unblock(monkeypatch, single_project):
# handle_verdict is a no-op AsyncMock -> the task stage never moves.
start, verdict = _patch_handlers(monkeypatch)
sent = _spy_telegram(monkeypatch)
_make_task("iss-noop", stage="review")
_patch_issues(monkeypatch, [
{"id": "iss-noop", "state": {"id": _APPROVED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
# The handler was replayed (idempotent), but nothing changed -> silence.
assert verdict.call_count == 1
assert sent == []
assert recon.unblocked_total == 0
# ---------------------------------------------------------------------------
# TC-05 (AC-4): two consecutive ticks on a synced task -> 0 repeat unblocks;
# plus a direct check of the in-memory dedup guard.
# ---------------------------------------------------------------------------
def test_tc05_dedup_no_repeat_notification(monkeypatch, single_project):
start, verdict = _patch_handlers(monkeypatch)
_patch_states_with_terminals(monkeypatch)
sent = _spy_telegram(monkeypatch)
_make_task("iss-dedup", stage="done", wi="ET-004")
_patch_issues(monkeypatch, [
{"id": "iss-dedup", "state": {"id": _DONE}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
recon.reconcile_plane_once()
assert sent == []
assert recon.unblocked_total == 0
# Direct dedup-guard exercise: the same issue+state notifies at most once.
recon._note_unblock("ET-004", "review", "state-x")
recon._note_unblock("ET-004", "review", "state-x")
assert recon.unblocked_total == 1
assert recon.deduped_total == 1
# ---------------------------------------------------------------------------
# TC-06 (AC-5): legit lost Approved webhook -> replayed, advanced, ONE unblock.
# ---------------------------------------------------------------------------
def test_tc06_legit_approved_unblock_once(monkeypatch, single_project):
_patch_states_with_terminals(monkeypatch) # non-terminal approved -> actionable
sent = _spy_telegram(monkeypatch)
_make_task("iss-appr", stage="review", wi="ET-005")
async def fake_verdict(issue_data, project_id, approved=True):
# Simulate the real handler advancing the stage (review -> testing).
conn = get_db()
conn.execute(
"UPDATE tasks SET stage='testing' WHERE plane_id=?",
(issue_data["id"],),
)
conn.commit()
conn.close()
monkeypatch.setattr(reconciler_mod, "handle_verdict", fake_verdict)
monkeypatch.setattr(reconciler_mod, "handle_status_start", AsyncMock())
_patch_issues(monkeypatch, [
{"id": "iss-appr", "state": {"id": _APPROVED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
assert recon.unblocked_total == 1
assert len(sent) == 1
assert "ET-005" in sent[0]
# ---------------------------------------------------------------------------
# TC-07 (AC-6): lost In Progress start (task appears) and lost Rejected
# rollback (stage moves) each fire exactly one unblock.
# ---------------------------------------------------------------------------
def test_tc07_in_progress_start_and_rejected_each_one_unblock(
monkeypatch, single_project
):
_patch_states_with_terminals(monkeypatch)
sent = _spy_telegram(monkeypatch)
async def fake_start(issue_data, project_id):
# Simulate the real start handler creating the task.
_make_task(issue_data["id"], stage="analysis", wi="ET-006")
async def fake_verdict(issue_data, project_id, approved=True):
conn = get_db()
conn.execute(
"UPDATE tasks SET stage='development' WHERE plane_id=?",
(issue_data["id"],),
)
conn.commit()
conn.close()
monkeypatch.setattr(reconciler_mod, "handle_status_start", fake_start)
monkeypatch.setattr(reconciler_mod, "handle_verdict", fake_verdict)
# Rejected task already exists at review; In Progress one has no task yet.
_make_task("iss-rej", stage="review", wi="ET-007")
_patch_issues(monkeypatch, [
{"id": "iss-start", "state": {"id": _IN_PROGRESS}, "updated_at": _OLD_TS},
{"id": "iss-rej", "state": {"id": _REJECTED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once()
assert recon.unblocked_total == 2
assert len(sent) == 2
# ---------------------------------------------------------------------------
# TC-08 (AC-8): never-raise — a failing dependency isolates to its unit of work.
# ---------------------------------------------------------------------------
def test_tc08_never_raise_isolation(monkeypatch, single_project):
_patch_states_with_terminals(monkeypatch)
monkeypatch.setattr(reconciler_mod, "send_telegram", lambda msg: None)
# _dispatch blows up for one issue -> isolated; the tick must not crash.
def boom_dispatch(*a, **k):
raise RuntimeError("handler exploded")
monkeypatch.setattr(Reconciler, "_dispatch", staticmethod(boom_dispatch))
_make_task("iss-boom", stage="review", wi="ET-008")
_patch_issues(monkeypatch, [
{"id": "iss-boom", "state": {"id": _APPROVED}, "updated_at": _OLD_TS},
])
recon = Reconciler()
recon.reconcile_plane_once() # must NOT raise
assert recon.unblocked_total == 0
# list_issues_by_state raising -> per-project isolation, still no crash.
def boom_list(pid, states):
raise RuntimeError("plane down")
monkeypatch.setattr(reconciler_mod, "list_issues_by_state", boom_list)
recon.reconcile_plane_once() # must NOT raise
# ---------------------------------------------------------------------------
# TC-09 (AC-9): kill-switches mute F-2.
# ---------------------------------------------------------------------------
def test_tc09_kill_switches(monkeypatch, single_project):
start, verdict = _patch_handlers(monkeypatch)
_patch_states_with_terminals(monkeypatch)
called = {"list": 0}
def counting_list(pid, states):
called["list"] += 1
return [{"id": "iss-x", "state": {"id": _APPROVED}, "updated_at": _OLD_TS}]
monkeypatch.setattr(reconciler_mod, "list_issues_by_state", counting_list)
monkeypatch.setattr(reconciler_mod.settings, "reconcile_enabled", False)
Reconciler().reconcile_plane_once()
assert called["list"] == 0 # global switch off -> F-2 never runs
monkeypatch.setattr(reconciler_mod.settings, "reconcile_enabled", True)
monkeypatch.setattr(reconciler_mod.settings, "reconcile_plane_enabled", False)
Reconciler().reconcile_plane_once()
assert called["list"] == 0 # F-2 switch off -> still no poll
# ---------------------------------------------------------------------------
# TC-10 (AC-1, AC-2): end-to-end on BOTH registry projects (enduro AND
# orchestrator): a Done task on each -> 0 notifications / 0 jobs, regardless
# of per-project status aliasing. The headline regression test.
# ---------------------------------------------------------------------------
def test_tc10_done_silent_on_all_projects(monkeypatch):
from src import projects as projects_mod
projects_mod.reload_projects()
assert len({p.plane_project_id for p in projects_mod.PROJECTS}) >= 2
start, verdict = _patch_handlers(monkeypatch)
sent = _spy_telegram(monkeypatch)
states = {
"in_progress": _IN_PROGRESS,
"to_analyse": _IN_PROGRESS, # ORCH-066 integ: always present
"approved": _APPROVED,
"rejected": _REJECTED,
"done": _DONE,
"cancelled": _CANCELLED,
}
groups = {_DONE: "completed", _CANCELLED: "cancelled"}
monkeypatch.setattr(reconciler_mod, "get_project_states", lambda pid: states)
monkeypatch.setattr(
reconciler_mod, "get_project_state_groups", lambda pid: groups
)
# Each project returns a Done issue (unique id per project).
monkeypatch.setattr(
reconciler_mod, "list_issues_by_state",
lambda pid, st: [
{"id": f"done-{pid}", "state": {"id": _DONE}, "updated_at": _OLD_TS}
],
)
recon = Reconciler()
recon.reconcile_plane_once()
start.assert_not_called()
verdict.assert_not_called()
assert sent == []
assert recon.unblocked_total == 0
assert recon.skipped_terminal_total >= 2 # one per project
assert _job_count() == 0

324
tests/test_security_gate.py Normal file
View File

@@ -0,0 +1,324 @@
"""ORCH-022 / TC-01..TC-12: the security-gate leaf module (src/security_gate.py).
These exercise the DETERMINISTIC core: the pure classifier / verdict / frontmatter
helpers (no binaries needed) plus scan_secrets / audit_dependencies with the
external scanners (gitleaks / pip-audit) mocked at subprocess.run. The integration
of the gate into advance_stage is covered in tests/test_stage_engine_security_gate.py;
the QG registry wiring in tests/test_qg_security.py.
Contract under test (ADR-001 §7):
* secrets are UNCONDITIONAL + offline -> a found secret blocks; a tool error is
fail-closed (FAIL);
* dependency audit is best-effort -> blocking only at/over the severity threshold;
UNKNOWN / below-threshold -> warning; an unreachable feed degrades fail-open +
warning by default, fail-closed only when configured;
* the machine verdict lives ONLY in the YAML frontmatter (read-back == written);
* never-raise: any internal error -> (False, reason), no exception escapes.
"""
import os
import subprocess
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
import pytest # noqa: E402
from src import security_gate as sg # noqa: E402
_REPO = "orchestrator"
_BRANCH = "feature/ORCH-022-x"
_WI = "ORCH-022"
# ---------------------------------------------------------------------------
# Builders for the result containers (no binaries needed).
# ---------------------------------------------------------------------------
def _clean_secret():
return sg.SecretScanResult(status="clean", detail="no secrets found")
def _found_secret(n=1):
findings = [
{"file": "src/config.py", "rule": "generic-api-key", "line": 12 + i, "match": "abcd…yz"}
for i in range(n)
]
return sg.SecretScanResult(status="found", findings=findings, detail=f"{n} secret(s)")
def _ok_deps(findings=None):
return sg.DepAuditResult(status="ok", findings=findings or [], detail="ok")
def _degraded_deps():
return sg.DepAuditResult(status="degraded", detail="pip-audit feed unavailable")
def _verdict(secret, dep, *, secrets_block=True, dep_block_severity="HIGH", dep_fail_closed=False):
return sg.compute_verdict(
secret, dep,
secrets_block=secrets_block,
dep_block_severity=dep_block_severity,
dep_fail_closed=dep_fail_closed,
)
# ---------------------------------------------------------------------------
# TC-01 / TC-02 / TC-03 — secret-scanning (FR-1 / AC-1..AC-3)
# ---------------------------------------------------------------------------
def test_tc01_secret_in_diff_fails():
"""TC-01: a planted secret -> FAIL, secrets_found>=1, reason names the finding."""
fields = _verdict(_found_secret(1), _ok_deps())
assert fields["security_status"] == "FAIL"
assert fields["secrets_found"] >= 1
# The reason must name the finding substance (rule + file), not just "FAIL".
assert "generic-api-key" in fields["reason"]
assert "src/config.py" in fields["reason"]
def test_tc02_clean_branch_passes():
"""TC-02: a clean branch -> PASS, secrets_found=0."""
fields = _verdict(_clean_secret(), _ok_deps())
assert fields["security_status"] == "PASS"
assert fields["secrets_found"] == 0
assert fields["deps_blocking"] == 0
def test_tc03_allowlisted_match_does_not_fail(monkeypatch, tmp_path):
"""TC-03: an allowlisted match (placeholder / fixture) is filtered by gitleaks
(rc=0) -> scan_secrets reports clean -> PASS. The allowlist lives in the
versioned .gitleaks.toml; here we assert the gate honours gitleaks' rc=0."""
wt = tmp_path / "wt"
wt.mkdir()
monkeypatch.setattr(sg, "ensure_worktree", lambda r, b: str(wt))
def _fake_run(cmd, **kwargs):
# `git fetch` and `gitleaks detect` both routed here; both "succeed clean".
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(sg.subprocess, "run", _fake_run)
res = sg.scan_secrets(_REPO, _BRANCH)
assert res.status == "clean"
fields = _verdict(res, _ok_deps())
assert fields["security_status"] == "PASS"
# ---------------------------------------------------------------------------
# TC-04..TC-07 — dependency audit + thresholds (FR-2 / AC-4..AC-7)
# ---------------------------------------------------------------------------
def test_tc04_high_cve_at_high_threshold_blocks():
"""TC-04: a HIGH/CRITICAL CVE at threshold HIGH -> FAIL, deps_blocking>=1."""
deps = _ok_deps([
{"package": "requests", "version": "2.0.0", "id": "CVE-1", "severity": "HIGH", "fix": "2.1"},
{"package": "urllib3", "version": "1.0.0", "id": "CVE-2", "severity": "CRITICAL", "fix": "1.1"},
])
fields = _verdict(_clean_secret(), deps, dep_block_severity="HIGH")
assert fields["security_status"] == "FAIL"
assert fields["deps_blocking"] >= 1
assert "CVE-1" in fields["reason"] or "CVE-2" in fields["reason"]
def test_tc05_only_medium_low_warns_passes():
"""TC-05: only MEDIUM/LOW vulns -> PASS, deps_warning>=1, findings in the body."""
deps = _ok_deps([
{"package": "jinja2", "version": "2.0", "id": "CVE-M", "severity": "MEDIUM", "fix": "2.1"},
{"package": "click", "version": "7.0", "id": "CVE-L", "severity": "LOW", "fix": ""},
])
fields = _verdict(_clean_secret(), deps, dep_block_severity="HIGH")
assert fields["security_status"] == "PASS"
assert fields["deps_warning"] >= 1
assert fields["deps_blocking"] == 0
body = sg.render_security_report(_WI, fields)
assert "CVE-M" in body and "CVE-L" in body
def test_tc06_threshold_config_changes_classification():
"""TC-06: severity=CRITICAL makes a HIGH CVE a warning; severity=HIGH blocks it."""
assert sg.classify_severity("HIGH", "CRITICAL") == "warning"
assert sg.classify_severity("HIGH", "HIGH") == "block"
assert sg.classify_severity("CRITICAL", "CRITICAL") == "block"
# UNKNOWN is ALWAYS a warning, never an auto-block (anti-loop, Р-4).
assert sg.classify_severity("UNKNOWN", "LOW") == "warning"
assert sg.classify_severity("", "HIGH") == "warning"
deps = _ok_deps([
{"package": "x", "version": "1", "id": "CVE-H", "severity": "HIGH", "fix": ""},
])
at_critical = _verdict(_clean_secret(), deps, dep_block_severity="CRITICAL")
at_high = _verdict(_clean_secret(), deps, dep_block_severity="HIGH")
assert at_critical["security_status"] == "PASS"
assert at_critical["deps_warning"] == 1
assert at_high["security_status"] == "FAIL"
assert at_high["deps_blocking"] == 1
def test_tc07_degraded_feed_failopen_default_failclosed_strict():
"""TC-07: an unreachable CVE feed degrades fail-open + warning by default (no
exception, no false FAIL); fail-closed -> FAIL only when configured."""
default = _verdict(_clean_secret(), _degraded_deps(), dep_fail_closed=False)
assert default["security_status"] == "PASS"
assert default["deps_audit_degraded"] is True
strict = _verdict(_clean_secret(), _degraded_deps(), dep_fail_closed=True)
assert strict["security_status"] == "FAIL"
assert strict["deps_audit_degraded"] is True
assert "fail-closed" in strict["reason"]
# ---------------------------------------------------------------------------
# TC-08..TC-10 — verdict / frontmatter parser + artefact (FR-3 / AC-8..AC-10)
# ---------------------------------------------------------------------------
def test_tc08_verdict_only_from_frontmatter():
"""TC-08: the verdict is read ONLY from the YAML frontmatter; prose in the body
does not influence it; the negative (FAIL) token is authoritative."""
# Frontmatter PASS but body screams FAIL -> still PASS (prose ignored).
pass_fm = (
"---\nsecurity_status: PASS\nsecrets_found: 0\n---\n"
"# Report\nThis build totally FAILED everything, FAIL FAIL.\n"
)
ok, reason = sg.parse_security_status(pass_fm)
assert ok is True
assert "PASS" in reason
# Frontmatter FAIL but body says PASS -> FAIL (negative token authoritative).
fail_fm = "---\nsecurity_status: FAIL\n---\nEverything PASS, looks great!\n"
ok, reason = sg.parse_security_status(fail_fm)
assert ok is False
assert "FAIL" in reason
def test_tc09_missing_or_broken_frontmatter_failclosed():
"""TC-09: no frontmatter / broken YAML / missing field -> (False, reason)."""
# No frontmatter at all.
ok, reason = sg.parse_security_status("# Just a body, no frontmatter\nPASS\n")
assert ok is False and reason
# Frontmatter present but no security_status field.
ok, reason = sg.parse_security_status("---\nother: 1\n---\nbody\n")
assert ok is False
# Broken YAML in the frontmatter.
ok, reason = sg.parse_security_status("---\nsecurity_status: : : [bad\n---\nbody\n")
assert ok is False
def test_tc10_artifact_has_valid_frontmatter_and_body(tmp_path, monkeypatch):
"""TC-10: 17-security-report.md is written with valid frontmatter (all machine
fields) and a body listing the findings; read-back == the written verdict."""
wt = tmp_path / "wt"
wt.mkdir()
monkeypatch.setattr(sg, "get_worktree_path", lambda r, b: str(wt))
monkeypatch.setattr(sg, "ensure_worktree", lambda r, b: str(wt))
deps = _ok_deps([
{"package": "requests", "version": "2.0", "id": "CVE-X", "severity": "HIGH", "fix": "2.1"},
{"package": "click", "version": "7.0", "id": "CVE-L", "severity": "LOW", "fix": ""},
])
fields = _verdict(_found_secret(1), deps, dep_block_severity="HIGH")
path = sg.write_security_report(_REPO, _WI, _BRANCH, fields)
assert os.path.isfile(path)
with open(path, encoding="utf-8") as f:
content = f.read()
# Frontmatter carries every machine field.
for key in ("security_status", "secrets_found", "deps_blocking", "deps_warning",
"deps_audit_degraded"):
assert f"{key}:" in content
# Body lists findings.
assert "CVE-X" in content and "CVE-L" in content
# Read-back agrees with the computed status (single source of truth, AC-8).
ok, _ = sg.parse_security_status(content)
assert ok is (fields["security_status"] == "PASS")
assert ok is False # this fixture is a FAIL (secret + HIGH CVE)
# ---------------------------------------------------------------------------
# TC-11 / TC-12 — never-raise / timeout (FR-5/FR-6 / AC-14..AC-17)
# ---------------------------------------------------------------------------
def test_tc11_missing_binary_failclosed_never_raises(monkeypatch, tmp_path):
"""TC-11: a missing scanner binary / internal exception -> error -> FAIL
(fail-closed for secrets), and the exception never propagates."""
wt = tmp_path / "wt"
wt.mkdir()
monkeypatch.setattr(sg, "ensure_worktree", lambda r, b: str(wt))
def _raise_fnf(cmd, **kwargs):
# git fetch ok, gitleaks missing.
if cmd[:1] == ["git"]:
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
raise FileNotFoundError("gitleaks")
monkeypatch.setattr(sg.subprocess, "run", _raise_fnf)
res = sg.scan_secrets(_REPO, _BRANCH)
assert res.status == "error"
fields = _verdict(res, _ok_deps())
assert fields["security_status"] == "FAIL" # fail-closed, BR-2
assert "fail-closed" in fields["reason"]
# check_security_gate as a whole never raises even if everything explodes.
monkeypatch.setattr(sg, "security_gate_applies", lambda r: True)
def _boom(*a, **k):
raise RuntimeError("kaboom")
monkeypatch.setattr(sg, "scan_secrets", _boom)
ok, reason = sg.check_security_gate(_REPO, _WI, _BRANCH)
assert ok is False
assert "error" in reason.lower()
def test_tc12_timeout_is_deterministic_failclosed(monkeypatch, tmp_path):
"""TC-12: exceeding the scan timeout -> a deterministic error verdict, no hang."""
wt = tmp_path / "wt"
wt.mkdir()
monkeypatch.setattr(sg, "ensure_worktree", lambda r, b: str(wt))
def _timeout(cmd, **kwargs):
if cmd[:1] == ["git"]:
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 1))
monkeypatch.setattr(sg.subprocess, "run", _timeout)
res = sg.scan_secrets(_REPO, _BRANCH)
assert res.status == "error"
assert "timeout" in res.detail.lower()
fields = _verdict(res, _ok_deps())
assert fields["security_status"] == "FAIL"
# pip-audit timeout -> degrade (best-effort), not a hard error.
monkeypatch.setattr(sg, "get_worktree_path", lambda r, b: str(wt))
(wt / "requirements.txt").write_text("requests==2.0\n")
dep = sg.audit_dependencies(_REPO, _BRANCH)
assert dep.status == "degraded"
assert "timeout" in dep.detail.lower()
# ---------------------------------------------------------------------------
# Parser robustness (supports the above; pure, never raises)
# ---------------------------------------------------------------------------
def test_parse_gitleaks_report_tolerant():
assert sg.parse_gitleaks_report("") == []
assert sg.parse_gitleaks_report("not json") == []
assert sg.parse_gitleaks_report("{}") == []
parsed = sg.parse_gitleaks_report(
'[{"File":"a.py","RuleID":"key","StartLine":3,"Secret":"supersecretvalue"}]'
)
assert parsed[0]["file"] == "a.py"
assert parsed[0]["rule"] == "key"
# The secret value is masked, never re-leaked verbatim.
assert "supersecretvalue" not in parsed[0]["match"]
def test_parse_pip_audit_report_tolerant():
assert sg.parse_pip_audit_report("") == []
assert sg.parse_pip_audit_report("garbage") == []
doc = (
'{"dependencies":[{"name":"requests","version":"2.0",'
'"vulns":[{"id":"CVE-1","severity":"HIGH","fix_versions":["2.1"]}]}]}'
)
parsed = sg.parse_pip_audit_report(doc)
assert parsed[0]["package"] == "requests"
assert parsed[0]["severity"] == "HIGH"
# Missing severity -> UNKNOWN.
doc2 = '{"dependencies":[{"name":"x","version":"1","vulns":[{"id":"CVE-2"}]}]}'
assert sg.parse_pip_audit_report(doc2)[0]["severity"] == "UNKNOWN"

View File

@@ -832,6 +832,7 @@ class TestMergeGate:
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},
)
@@ -856,6 +857,7 @@ class TestMergeGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _fail("merge-lock busy")},
)
monkeypatch.setattr(stage_engine.settings, "merge_defer_delay_s", 30)
@@ -883,6 +885,7 @@ class TestMergeGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _fail("merge-lock busy")},
)
monkeypatch.setattr(stage_engine.settings, "merge_defer_max_attempts", 3)
@@ -916,6 +919,7 @@ class TestMergeGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _fail("rebase conflict: src/db.py")},
)
task_id = _make_task("deploy-staging", repo="orchestrator", wi="ORCH-043",
@@ -939,6 +943,7 @@ class TestMergeGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _fail("re-test failed after rebase: 1 failed")},
)
task_id = _make_task("deploy-staging", repo="orchestrator", wi="ORCH-043",
@@ -962,6 +967,7 @@ class TestMergeGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _fail("rebase conflict: src/db.py")},
)
task_id = _make_task("deploy-staging", repo="orchestrator", wi="ORCH-043",
@@ -1014,6 +1020,7 @@ class TestImageFreshnessGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _pass,
"check_staging_image_fresh": _fail(
"staging rebuild failed: health FAILED")},
@@ -1041,6 +1048,7 @@ class TestImageFreshnessGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _pass,
"check_staging_image_fresh": _fail("provenance mismatch")},
)
@@ -1064,6 +1072,7 @@ class TestImageFreshnessGate:
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},
)
@@ -1089,6 +1098,7 @@ class TestImageFreshnessGate:
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_security_gate": _pass,
"check_branch_mergeable": _pass},
) # check_staging_image_fresh left REAL -> N/A for enduro-trails
task_id = _make_task("deploy-staging", repo="enduro-trails", wi="ET-099",
@@ -1160,6 +1170,7 @@ class TestStagingInfraTolerance:
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},
)
@@ -1232,6 +1243,7 @@ class TestStagingInfraTolerance:
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,
"check_deploy_status": _pass},

View File

@@ -0,0 +1,105 @@
"""ORCH-059 TC-09: the Phase A CTA asks the operator for "Confirm Deploy".
Contract (AC-6): when Phase A advances `deploy-staging` -> `deploy` and requests
manual approval, both the Plane comment and the Telegram notification must
instruct the operator to flip the status to "Confirm Deploy" (the dedicated
prod-deploy trigger) — and must NOT present "Approved" as the deploy trigger.
"""
import os
import tempfile
import pytest
_test_db = os.path.join(tempfile.gettempdir(), "test_orch_phase_a_cta.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
def _pass(*a, **k):
return (True, "ok")
@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()
monkeypatch.setattr(self_deploy.settings, "repos_dir", str(tmp_path))
monkeypatch.setattr(self_deploy.settings, "host_repos_dir", str(tmp_path))
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
# Pass the staging / merge / freshness sub-gates so the edge reaches Phase A.
monkeypatch.setattr(
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS,
"check_staging_status": _pass,
"check_branch_mergeable": _pass,
# ORCH-022 integ: security-gate now runs on the deploy-staging->deploy
# edge; this 059 test exercises the CTA, not the scanner (022 has its
# own tests), so pass it like the other deploy sub-gates.
"check_security_gate": _pass,
"check_staging_image_fresh": _pass},
)
yield
def _make_task(stage="deploy-staging", repo="orchestrator",
branch="feature/ORCH-059-x", wi="ORCH-059"):
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 test_tc09_phase_a_cta_requests_confirm_deploy(monkeypatch):
# Silence everything EXCEPT the two CTA channels we want to inspect.
for name in (
"notify_stage_change", "notify_qg_failure", "plane_notify_stage",
"plane_notify_qg", "set_issue_in_review", "set_issue_needs_input",
"set_issue_in_progress", "set_issue_blocked", "set_issue_done",
):
monkeypatch.setattr(stage_engine, name, MagicMock(), raising=False)
plane_comment = MagicMock()
telegram = MagicMock()
monkeypatch.setattr(stage_engine, "plane_add_comment", plane_comment)
monkeypatch.setattr(stage_engine, "send_telegram", telegram)
task_id = _make_task()
res = advance_stage(
task_id, "deploy-staging", "orchestrator", "ORCH-059",
"feature/ORCH-059-x", finished_agent="deployer",
)
assert res.note == "self-deploy-approval-pending"
# The Plane comment CTA mentions "Confirm Deploy" as the trigger.
plane_comment.assert_called_once()
comment_text = plane_comment.call_args.args[1]
assert "Confirm Deploy" in comment_text
# The Telegram CTA mentions "Confirm Deploy" too.
telegram.assert_called_once()
tg_text = telegram.call_args.args[0]
assert "Confirm Deploy" in tg_text
# Neither CTA presents bare "Approved" as the deploy trigger. (The comment may
# mention Approved only to clarify it does NOT trigger; assert no instruction
# to "set status to Approved".)
assert "статус задачи на «Approved»" not in comment_text
assert "на Approved" not in tg_text

View File

@@ -0,0 +1,141 @@
"""ORCH-059 TC-07/08: the Phase B block in stage_engine.advance_stage initiates
the prod deploy ONLY on the confirm-deploy signal.
Contract (AC-2, AC-3, AC-5):
* TC-07 — on (current_stage=="deploy", finished_agent is None) for the
self-hosting repo: confirm_deploy=True -> Phase B initiates; confirm_deploy
omitted/False (a plain Approved) -> a no-op that neither initiates the deploy
nor runs check_deploy_status (no false БАГ-8 rollback).
* TC-08 — idempotency: with the `initiated` marker already present, a repeated
confirm-deploy does NOT initiate again.
"""
import os
import tempfile
import pytest
_test_db = os.path.join(tempfile.gettempdir(), "test_orch_phase_b.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()
monkeypatch.setattr(self_deploy.settings, "repos_dir", str(tmp_path))
monkeypatch.setattr(self_deploy.settings, "host_repos_dir", str(tmp_path))
monkeypatch.setattr(stage_engine.settings, "deploy_require_manual_approve", True)
yield
@pytest.fixture(autouse=True)
def silence_side_effects(monkeypatch):
for name in (
"notify_stage_change", "notify_qg_failure", "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",
):
monkeypatch.setattr(stage_engine, name, MagicMock(), raising=False)
def _make_task(stage, repo="orchestrator", branch="feature/ORCH-059-x", wi="ORCH-059"):
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]
# ---------------------------------------------------------------------------
# TC-07: confirm-deploy initiates; plain Approved is a no-op
# ---------------------------------------------------------------------------
def test_tc07_confirm_deploy_initiates(monkeypatch):
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
task_id = _make_task("deploy")
res = advance_stage(
task_id, "deploy", "orchestrator", "ORCH-059",
"feature/ORCH-059-x", finished_agent=None, confirm_deploy=True,
)
assert res.note == "self-deploy-initiated"
initiate.assert_called_once()
assert self_deploy.has_marker("orchestrator", "ORCH-059", self_deploy.INITIATED)
# Did NOT advance off deploy — the finalizer records the verdict later.
assert _stage(task_id) == "deploy"
def test_tc07_approved_without_confirm_is_noop(monkeypatch):
"""A plain Approved on `deploy` (confirm_deploy defaults to False): no
initiate_deploy, no rollback, no advance — a deterministic no-op (AC-3)."""
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
# If check_deploy_status were (wrongly) run, it would intervene; spy to prove
# it is never invoked on this no-op path.
gate = MagicMock(return_value=(False, "FAILED"))
monkeypatch.setattr(
stage_engine, "QG_CHECKS",
{**stage_engine.QG_CHECKS, "check_deploy_status": gate},
)
task_id = _make_task("deploy")
res = advance_stage(
task_id, "deploy", "orchestrator", "ORCH-059",
"feature/ORCH-059-x", finished_agent=None, # confirm_deploy omitted -> False
)
assert res.note == "approved-on-deploy-noop"
initiate.assert_not_called()
gate.assert_not_called() # check_deploy_status NOT run -> no false БАГ-8
assert res.advanced is False
assert res.rolled_back_to is None
assert _stage(task_id) == "deploy" # stays put, no rollback to development
assert not self_deploy.has_marker("orchestrator", "ORCH-059", self_deploy.INITIATED)
# ---------------------------------------------------------------------------
# TC-08: idempotency — existing `initiated` marker -> repeat is a no-op
# ---------------------------------------------------------------------------
def test_tc08_idempotent_repeat_confirm_deploy(monkeypatch):
initiate = MagicMock(return_value=(True, "ok"))
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", initiate)
task_id = _make_task("deploy")
# Pre-seed the initiated marker (a deploy already in flight).
self_deploy.write_marker("orchestrator", "ORCH-059", self_deploy.INITIATED, content="1")
res = advance_stage(
task_id, "deploy", "orchestrator", "ORCH-059",
"feature/ORCH-059-x", finished_agent=None, confirm_deploy=True,
)
assert res.note == "self-deploy-already-initiated"
initiate.assert_not_called()

View File

@@ -0,0 +1,270 @@
"""ORCH-022 / TC-16..TC-19, TC-21: the security sub-gate wired into advance_stage.
These are integration tests over src.stage_engine.advance_stage on the
deploy-staging -> deploy edge. The security verdict is injected by patching the
QG_CHECKS registry entry (the leaf scanner logic is unit-tested in
tests/test_security_gate.py), so we exercise the ENGINE behaviour:
* FAIL -> rollback to development + enqueue developer + Plane comment + notify;
* the rollback task_desc carries the verbatim findings (ORCH-046 pattern);
* after MAX_DEVELOPER_RETRIES -> set_issue_blocked + Telegram, no bounce;
* PASS -> the pipeline advances normally (no rollback, no noisy notify);
* self-hosting safety: a FAIL never calls the deploy hook / restarts prod.
Network/Plane/Telegram side effects are mocked at the src.stage_engine level.
"""
import os
import tempfile
import pytest
_test_db = os.path.join(tempfile.gettempdir(), "test_orchestrator_security_gate.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.stage_engine import advance_stage # noqa: E402
_BRANCH = "feature/ORCH-022-x"
# ---------------------------------------------------------------------------
# Fixtures (mirror tests/test_stage_engine.py)
# ---------------------------------------------------------------------------
@pytest.fixture(autouse=True)
def fresh_db(monkeypatch):
monkeypatch.setattr(_db.settings, "db_path", _test_db)
if os.path.exists(_test_db):
os.unlink(_test_db)
init_db()
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",
):
monkeypatch.setattr(stage_engine, name, MagicMock())
def _make_task(stage, repo, branch=_BRANCH, wi="ORCH-022"):
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 _job_contents():
conn = get_db()
rows = conn.execute("SELECT task_content FROM jobs ORDER BY id").fetchall()
conn.close()
return [r[0] for r in rows]
def _add_developer_runs(task_id, n):
conn = get_db()
for _ in range(n):
conn.execute(
"INSERT INTO agent_runs (task_id, agent) VALUES (?, 'developer')",
(task_id,),
)
conn.commit()
conn.close()
def _pass(*a, **k):
return (True, "ok")
def _fail(reason):
def _f(*a, **k):
return (False, reason)
return _f
def _qg_with_security(monkeypatch, security_result):
"""Patch QG_CHECKS so every gate passes EXCEPT the security gate, which returns
``security_result``. Keeps the deploy-staging edge reachable (check_staging_status
passes) and isolates the security verdict under test."""
patched = {k: _pass for k in stage_engine.QG_CHECKS}
patched["check_security_gate"] = security_result
monkeypatch.setattr(stage_engine, "QG_CHECKS", patched)
# ---------------------------------------------------------------------------
# TC-16 — FAIL -> rollback to development + enqueue developer + notify.
# ---------------------------------------------------------------------------
def test_tc16_fail_rolls_back_and_enqueues_developer(monkeypatch):
"""TC-16: security_status FAIL -> rollback deploy-staging -> development,
enqueue developer, Plane comment + notify_qg_failure."""
_qg_with_security(monkeypatch, _fail("2 secret(s): aws-key in src/x.py:3"))
task_id = _make_task("deploy-staging", repo="enduro-trails")
res = advance_stage(
task_id, "deploy-staging", "enduro-trails", "ORCH-022", _BRANCH,
finished_agent="deployer",
)
assert res.advanced is False
assert res.rolled_back_to == "development"
assert _stage(task_id) == "development"
jobs = _jobs()
assert len(jobs) == 1
assert jobs[0]["agent"] == "developer"
assert res.qg_name == "check_security_gate"
# The deployer-authored Plane comment + the QG-failure notification fired.
assert stage_engine.plane_add_comment.called
assert stage_engine.notify_qg_failure.called
# ---------------------------------------------------------------------------
# TC-17 — the rollback task_desc carries the verbatim findings (ORCH-046).
# ---------------------------------------------------------------------------
def test_tc17_task_desc_has_verbatim_findings(monkeypatch, tmp_path):
"""TC-17: the re-launched developer's task_desc embeds the verbatim finding
substance (not just a link), following the ORCH-046 pattern."""
reason = "2 secret(s): aws-access-key in src/config.py:12"
_qg_with_security(monkeypatch, _fail(reason))
task_id = _make_task("deploy-staging", repo="enduro-trails")
# Isolate the worktree base under tmp_path so this test never touches the real
# shared /repos/_wt host path (PermissionError in CI). Mirrors the pattern in
# tests/test_git_worktree.py / test_merge_gate.py.
from src import git_worktree
monkeypatch.setattr(git_worktree.settings, "worktrees_dir", str(tmp_path / "_wt"))
# Seed a real 17-security-report.md in the worktree so extract_security_findings
# has a verbatim body to excerpt.
wt = stage_engine.get_worktree_path("enduro-trails", _BRANCH)
report_dir = os.path.join(wt, "docs", "work-items", "ORCH-022")
os.makedirs(report_dir, exist_ok=True)
with open(os.path.join(report_dir, "17-security-report.md"), "w", encoding="utf-8") as f:
f.write(
"---\nsecurity_status: FAIL\nsecrets_found: 1\n---\n"
"# Security Report — ORCH-022\n\n"
"## Verdict\n1 secret(s): aws-access-key in src/config.py:12\n\n"
"## Secrets\n- `src/config.py:12` — aws-access-key (match `AKIA…YZ`)\n\n"
"## Dependencies (blocking)\n- None\n"
)
advance_stage(
task_id, "deploy-staging", "enduro-trails", "ORCH-022", _BRANCH,
finished_agent="deployer",
)
contents = _job_contents()
assert len(contents) == 1
desc = contents[0]
# The verbatim reason AND the excerpted finding line are present.
assert "aws-access-key in src/config.py:12" in desc
assert "src/config.py:12" in desc
# Plus the link to the full artefact.
assert "17-security-report.md" in desc
# ---------------------------------------------------------------------------
# TC-18 — after MAX_DEVELOPER_RETRIES -> block + Telegram, no bounce.
# ---------------------------------------------------------------------------
def test_tc18_retry_cap_blocks_and_alerts(monkeypatch):
"""TC-18: after MAX_DEVELOPER_RETRIES developer attempts -> set_issue_blocked +
Telegram alert; no infinite bounce (no new developer job)."""
_qg_with_security(monkeypatch, _fail("blocking CVE"))
task_id = _make_task("deploy-staging", repo="enduro-trails")
_add_developer_runs(task_id, stage_engine.MAX_DEVELOPER_RETRIES)
res = advance_stage(
task_id, "deploy-staging", "enduro-trails", "ORCH-022", _BRANCH,
finished_agent="deployer",
)
assert res.rolled_back_to == "development"
assert res.alerted is True
assert stage_engine.set_issue_blocked.called
assert stage_engine.send_telegram.called
# No further developer job past the cap.
assert _jobs() == []
# ---------------------------------------------------------------------------
# TC-19 — PASS -> the pipeline advances normally.
# ---------------------------------------------------------------------------
def test_tc19_pass_advances_normally(monkeypatch):
"""TC-19: security_status PASS -> advance deploy-staging -> deploy with the
deployer launched, no rollback, no QG-failure notification."""
_qg_with_security(monkeypatch, lambda *a, **k: (True, "security clean"))
task_id = _make_task("deploy-staging", repo="enduro-trails")
res = advance_stage(
task_id, "deploy-staging", "enduro-trails", "ORCH-022", _BRANCH,
finished_agent="deployer",
)
assert res.advanced is True
assert res.to_stage == "deploy"
assert _stage(task_id) == "deploy"
assert res.rolled_back_to is None
# No noisy QG-failure notification on the happy path.
assert not stage_engine.notify_qg_failure.called
# ---------------------------------------------------------------------------
# TC-21 — self-hosting safety: a FAIL never deploys / restarts prod.
# ---------------------------------------------------------------------------
def test_tc21_fail_never_triggers_deploy(monkeypatch):
"""TC-21: on a security FAIL the gate only rolls back + enqueues developer; it
never calls the deploy hook / restarts the prod container (self-hosting safety)."""
_qg_with_security(monkeypatch, _fail("secret found"))
# Spy on the self-deploy entrypoints — none must be invoked on a FAIL.
monkeypatch.setattr(stage_engine.self_deploy, "initiate_deploy", MagicMock())
monkeypatch.setattr(stage_engine.self_deploy, "self_deploy_applies", MagicMock(return_value=True))
task_id = _make_task("deploy-staging", repo="orchestrator")
res = advance_stage(
task_id, "deploy-staging", "orchestrator", "ORCH-022", _BRANCH,
finished_agent="deployer",
)
assert res.rolled_back_to == "development"
# The security FAIL returns BEFORE the self-deploy block -> no deploy initiated.
assert not stage_engine.self_deploy.initiate_deploy.called
# Only the developer is re-enqueued; no deployer job.
jobs = _jobs()
assert all(j["agent"] == "developer" for j in jobs)