Skip to content

richfrem/agent-plugins-skills / plugins/agent-agentic-os/.apm/skills/running-control-plane

running-control-plane

Flagged

CRITICAL INTAKE GATEWAY: use at the start of any non-trivial engineering task, feature, refactor, multi-file bugfix, work package or new idea, including requests to explore, brainstorm, build, fix or change something, before planning or code changes. Enforces read-only discovery, 1-3 structured questions with defaults, control-plane registration and the immutable four-pillar TASK_SPEC.md. Also runs the rest of the control plane: health ("run health check", "check os metrics", "system monitor", event bus, locks, memory, "is my signing identity ready" status check), transition simulation ("dry run transition", "simulate transition", "check edge X -> Y"), worktree selection, and gating on/off ("disable the control plane", "turn off the push guard", "stop gating my commits"). Not for project setup or key creation (use setting-up-agentic-os) or improving skills (use improving-skills-in-lab).

FileKindSize
SKILL.mdskill md5.4 KB
evals/evals.jsonconfig8.3 KB
references/acceptance-criteria.mdmarkdown4.0 KB
references/agentic-os-skill-map.mdmarkdown2.3 KB
references/architecture/architecture.mdmarkdown2.4 KB
references/architecture/context-folder-patterns.mdmarkdown3.3 KB
references/architecture/vision.mdmarkdown56.6 KB
references/cheap-agent-transition-simulation.mdmarkdown6.1 KB
references/control-plane-architecture.mdmarkdown9.0 KB
references/control-plane-rebuild-recovery.mdmarkdown6.2 KB
references/control-plane-simulation-protocol.mdmarkdown5.7 KB
references/diagrams/agentic-os-memory-subsystem.mmdother1.4 KB
references/diagrams/architecture-overview.mmdother3.1 KB
references/diagrams/control-plane-architecture.mermaidother6.3 KB
references/diagrams/control-plane-lifecycle-hardened-with-ciba-rar.mermaidother4.5 KB
references/diagrams/control-plane-pipeline-happy-path.mermaidother5.0 KB
references/diagrams/control-plane-pipeline.mermaidother9.3 KB
references/diagrams/event-bus-architecture.mmdother2.4 KB
references/diagrams/increment-b-human-only-transition-ssh-signing.mermaidother5.3 KB
references/edge-matrix.mdmarkdown13.3 KB
references/fallback-tree.mdmarkdown2.9 KB
references/health-detailed-reference.mdmarkdown7.1 KB
references/healthy-transcript.mdmarkdown3.8 KB
references/implementation-ledger-contract.mdmarkdown851 B
references/intake-detailed-reference.mdmarkdown3.1 KB
references/modes/health.mdmarkdown3.4 KB
references/modes/intake.mdmarkdown3.8 KB
references/modes/mode.mdmarkdown5.8 KB
references/modes/simulator.mdmarkdown6.2 KB
references/modes/worktree.mdmarkdown2.9 KB
references/multi-round-external-review-protocol.mdmarkdown2.6 KB
references/worktree-reconciliation-and-multi-worktree-practices.mdmarkdown5.8 KB
requirements-dev.inother741 B
requirements-dev.txtmarkdown603 B
requirements.inother196 B
requirements.txtmanifest202 B
scripts/agent_control.pyscript97.7 KB
scripts/audit_map_debt.pyscript6.1 KB
scripts/capability_probe.pyscript6.4 KB
scripts/check_plan_links.pyscript1.8 KB
scripts/control_plane/__init__.pyscript842 B
scripts/control_plane/adapters.pyscript144.9 KB
scripts/control_plane/approver_policy.pyscript20.0 KB
scripts/control_plane/baseline_transition_report.pyscript4.0 KB
scripts/control_plane/constants.pyscript13.5 KB
scripts/control_plane/coordinator.pyscript57.3 KB
scripts/control_plane/edge_matrix.pyscript3.1 KB
scripts/control_plane/gate1_approval.pyscript10.7 KB
scripts/control_plane/gate_evidence.pyscript9.9 KB
scripts/control_plane/identity_layout.pyscript2.9 KB
scripts/control_plane/identity_setup.pyscript11.0 KB
scripts/control_plane/implementation.pyscript3.7 KB
scripts/control_plane/implementation_loop.pyscript11.3 KB
scripts/control_plane/inspection.pyscript3.9 KB
scripts/control_plane/installation_probe.pyscript5.7 KB
scripts/control_plane/isolation_check.pyscript14.0 KB
scripts/control_plane/measurement.pyscript39.1 KB
scripts/control_plane/pipeline_simulator.pyscript30.0 KB
scripts/control_plane/policy.pyscript32.5 KB
scripts/control_plane/ports.pyscript20.3 KB
scripts/control_plane/proof_edges.pyscript4.6 KB
scripts/control_plane/proof_policy.pyscript2.2 KB
scripts/control_plane/receipt_provenance.pyscript4.7 KB
scripts/control_plane/registry.pyscript32.9 KB
scripts/control_plane/review_options.pyscript8.2 KB
scripts/control_plane/review_selection.pyscript3.2 KB
scripts/control_plane/run_transition_simulation.pyscript12.0 KB
scripts/control_plane/signing_selftest.pyscript6.8 KB
scripts/control_plane/simulation_identity.pyscript10.4 KB
scripts/control_plane/snapshot.pyscript10.8 KB
scripts/control_plane/ssh_signing.pyscript21.8 KB
scripts/control_plane/state_machine.pyscript5.8 KB
scripts/control_plane/transition_request.pyscript19.5 KB
scripts/control_plane/transition_simulation_cases.pyscript24.6 KB
scripts/control_plane/transition_templates.yamlconfig159.1 KB
scripts/control_plane/tty_signer.pyscript3.0 KB
scripts/control_plane/wrappers/__init__.pyscript24 B
scripts/control_plane/wrappers/record_interview_question.pyscript8.0 KB
scripts/control_plane/wrappers/record_retrospective.pyscript2.3 KB
scripts/control_plane/wrappers/run_exit_verification.pyscript6.4 KB
scripts/control_plane/wrappers/run_verify_exit_bundle.pyscript5.4 KB
scripts/control_plane/wrappers/write_plan_document.pyscript9.6 KB
scripts/control_plane_hooks.pyscript10.5 KB
scripts/control_plane_mode.pyscript12.0 KB
scripts/distill_playbook.pyexecutablescript9.9 KB
scripts/evolution_state.pyscript29.4 KB
scripts/export_upstream_pr.pyscript4.4 KB
scripts/interview_spec_engine.pyscript9.1 KB
scripts/kernel.pyscript14.6 KB
scripts/pre-commit-evolution-guardexecutablescript3.7 KB
scripts/pre-commit-pipeline-guardexecutablescript13.6 KB
scripts/pre-push-review-guardexecutablescript10.5 KB
scripts/record_trace.pyscript7.2 KB
scripts/smoke_test.pyexecutablescript22.6 KB
scripts/test_capability_probe.pyscript3.9 KB
scripts/test_worktree_manager.pyscript2.8 KB
scripts/verify_evolution_receipt.pyscript7.5 KB
scripts/verify_gate_evidence.pyscript1.6 KB
scripts/worktree_manager.pyscript3.0 KB
tests/conftest.pyscript7.7 KB
tests/helpers/__init__.pyscript0 B
tests/helpers/control_plane_fixtures.pyscript2.6 KB
tests/helpers/control_plane_repo.pyscript4.0 KB
tests/helpers/gate1_fixtures.pyscript5.8 KB
tests/helpers/human_signer.pyscript7.3 KB
tests/helpers/implementation_ledger.pyscript1.1 KB
tests/interview_helpers.pyscript5.2 KB
tests/test_actionable_rollback.pyscript2.2 KB
tests/test_agent_control.pyscript190.6 KB
tests/test_agent_control_gate_characterization.pyscript14.7 KB
tests/test_approve_transition.pyscript10.5 KB
tests/test_approver_identity_guidance.pyscript3.4 KB
tests/test_approver_isolation.pyscript12.3 KB
tests/test_audit_map_debt.pyscript4.7 KB
tests/test_authorized_actor_enforcement.pyscript22.9 KB
tests/test_background_document_priority.pyscript924 B
tests/test_check_plan_links.pyscript1.1 KB
tests/test_content_snapshot.pyscript11.8 KB
tests/test_control_plane_architecture_diagram_contract.pyscript9.0 KB
tests/test_control_plane_architecture_overview_contract.pyscript2.1 KB
tests/test_control_plane_critic_review.pyscript3.1 KB
tests/test_control_plane_crypto_adapter.pyscript5.0 KB
tests/test_control_plane_delegation_contract.pyscript5.7 KB
tests/test_control_plane_facade_composition.pyscript15.9 KB
tests/test_control_plane_filesystem_adapter.pyscript4.9 KB
tests/test_control_plane_hooks.pyscript7.3 KB
tests/test_control_plane_interview_guidance.pyscript3.8 KB
tests/test_control_plane_measurement_contract.pyscript28.0 KB
tests/test_control_plane_mode.pyscript7.5 KB
tests/test_control_plane_mode_guards.pyscript3.5 KB
tests/test_control_plane_model_catalog_adapter.pyscript9.2 KB
tests/test_control_plane_pipeline_diagram_contract.pyscript2.3 KB
tests/test_control_plane_pipeline_simulator.pyscript7.5 KB
tests/test_control_plane_policy.pyscript13.0 KB
tests/test_control_plane_ports.pyscript3.2 KB
tests/test_control_plane_reset_transition.pyscript13.6 KB
tests/test_control_plane_schema_rebuild_atomicity.pyscript20.0 KB
tests/test_control_plane_simulation_protocol_contract.pyscript959 B
tests/test_control_plane_sqlite_persistence_adapter.pyscript5.5 KB
tests/test_control_plane_state_machine.pyscript15.2 KB
tests/test_control_plane_trigger_enforcement.pyscript14.6 KB
tests/test_control_plane_worktree_sharing.pyscript4.5 KB
tests/test_domain_namespaces.pyscript6.7 KB
tests/test_done_closure_proof.pyscript6.9 KB
tests/test_draft_plan_interactive_outline_gap.pyscript2.2 KB
tests/test_dual_identity_onboarding.pyscript7.3 KB
tests/test_edge_matrix.pyscript3.2 KB
tests/test_ensure_schema_fastpath.pyscript4.4 KB
tests/test_evolution_guards.pyscript14.9 KB
tests/test_evolution_scripts.pyscript55.3 KB
tests/test_gate1_guidance.pyscript1.8 KB
tests/test_gate3_code_acceptance.pyscript13.5 KB
tests/test_graph_state_machine.pyscript16.0 KB
tests/test_guidance_readiness.pyscript2.2 KB
tests/test_health_check_signing_status.pyscript3.5 KB
tests/test_human_decision_trigger_enforcement.pyscript37.3 KB
tests/test_human_only_wiring.pyscript22.3 KB
tests/test_human_signer_isolation.pyscript4.0 KB
tests/test_identity_verification_readiness.pyscript5.8 KB
tests/test_implementation_ledger.pyscript3.4 KB
tests/test_implementation_loop.pyscript6.9 KB
tests/test_implementation_runtime_bridge.pyscript2.6 KB
tests/test_init_agentic_os_scaffolding.pyscript10.0 KB
tests/test_init_respects_control_plane_mode.pyscript4.3 KB
tests/test_inspect_verbs.pyscript3.6 KB
tests/test_installation_probe.pyscript3.1 KB
tests/test_intake_scenarios.pyscript7.2 KB
tests/test_interview_plan_outline.pyscript2.8 KB
tests/test_isolation_check.pyscript12.5 KB
tests/test_isolation_setup_docs.pyscript2.7 KB
tests/test_kernel_security.pyscript4.0 KB
tests/test_legacy_input_fallback.pyscript6.0 KB
tests/test_list_review_options.pyscript3.1 KB
tests/test_multi_agent_bundle_gate.pyscript5.5 KB
tests/test_one_approver_per_pipeline.pyscript15.8 KB
tests/test_os_init_dual_identity.pyscript3.9 KB
tests/test_os_init_identity_bootstrap.pyscript8.8 KB
tests/test_os_init_identity_status.pyscript3.9 KB
tests/test_os_init_single_instruction_file.pyscript7.3 KB
tests/test_p00_live_baseline.pyscript10.2 KB
tests/test_p01_intake_persistence.pyscript6.7 KB
tests/test_post_run_metrics_protocol.pyscript3.3 KB
tests/test_pre_commit_pipeline_guard.pyscript29.1 KB
tests/test_pre_push_review_guard.pyscript5.7 KB
tests/test_receipt_provenance.pyscript6.6 KB
tests/test_retrospective_pipeline.pyscript5.3 KB
tests/test_review_option_menus.pyscript6.6 KB
tests/test_review_selection.pyscript8.2 KB
tests/test_round3_fixes.pyscript8.3 KB
tests/test_rules_compliance.pyscript1.3 KB
tests/test_run_transition_simulation_formatting.pyscript1.7 KB
tests/test_run_verify_exit_bundle.pyscript10.2 KB
tests/test_setup_ciba_identity.pyscript11.2 KB
tests/test_signed_approval_human_receipt.pyscript3.9 KB
tests/test_signing_mechanics_selftest.pyscript6.9 KB
tests/test_signing_skill_selfcontained.pyscript3.4 KB
tests/test_skip_receipt_guidance.pyscript2.7 KB
tests/test_ssh_signing.pyscript23.9 KB
tests/test_strategy_artifacts.pyscript21.8 KB
tests/test_transition_answer_normalization.pyscript1.1 KB

tests/test_human_decision_trigger_enforcement.py

View on GitHub
"""
test_human_decision_trigger_enforcement.py
===========================================

Deterministic TDD test suite validating the second-layer database trigger enforcement:
1. Replicates the exact live bypass: programmatic self-approval with actor="human"
   without real interactive stdin pause, confirming it gets rejected at the DB level.
2. Parity test: asserts set-equality between registry edges with human_questions and
   edges verified by the test matrix (fails CI if new question edge added to YAML without test).
3. Full Matrix Coverage across all 52 edges in ALLOWED_TRANSITIONS:
   - 36 deterministic-only edges: confirmed unaffected by question trigger.
   - 16 human-question edges: each tested across 5 discrete permutations:
     * Valid: actor='human', matching question_id, non-empty answer -> SUCCEEDS.
     * Invalid 1: zero decision rows -> REJECTED & LOGGED.
     * Invalid 2: actor='agent' (synthetic/agent bypass) -> REJECTED & LOGGED.
     * Invalid 3: wrong question_id -> REJECTED & LOGGED.
     * Invalid 4: null/empty answer -> REJECTED & LOGGED.
"""

import sys
from pathlib import Path
import pytest
import sqlite3

SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts"
if str(SCRIPTS_DIR) not in sys.path:
    sys.path.insert(0, str(SCRIPTS_DIR))

from control_plane.adapters import SqlitePersistenceAdapter, FilesystemAdapter, SCHEMA_SQL, _split_schema_sql_statements
from control_plane.registry import TransitionRegistry
from control_plane.state_machine import ALLOWED_TRANSITIONS
from agent_control import ControlPlane, PersistenceInvariantViolation, ConcurrentModificationError
from control_plane.coordinator import TransitionCoordinator, TransitionCoordinatorError
from control_plane.constants import (
    STATE_INTAKE, STATE_DRAFT_PLAN, STATE_AWAITING_APPROVAL, STATE_APPROVED, STATE_VERIFY_EXIT, STATE_RETROSPECTIVE, STATE_DONE, STATE_ESCALATED,
)


@pytest.fixture
def test_env(tmp_path):
    db_path = tmp_path / "context" / "control_plane.db"
    adapter = SqlitePersistenceAdapter(db_path, FilesystemAdapter())
    adapter.ensure_schema()
    cp = ControlPlane(db_path=db_path, persistence_adapter=adapter)
    return {"db_path": db_path, "adapter": adapter, "cp": cp, "tmp_path": tmp_path}


def _seed_task_at_state(conn, task_id, state):
    """Directly sets a task row at a given state with clean transition history,
    bypassing triggers during test fixture setup, and recreates the exact production triggers
    from SCHEMA_SQL (eliminating any test/production DDL divergence)."""
    conn.execute("DROP TRIGGER IF EXISTS enforce_valid_initial_state;")
    conn.execute("DROP TRIGGER IF EXISTS enforce_valid_transition;")
    conn.execute("INSERT OR REPLACE INTO tasks (task_id, title, state, task_type, runtime_tool) VALUES (?, ?, ?, 'GENERAL', 'claude')",
                 (task_id, f"Task {task_id}", state))
    conn.execute("INSERT INTO task_transitions (task_id, from_state, to_state, actor, reason) VALUES (?, 'NONE', ?, 'system', 'fixture')",
                 (task_id, state))
    conn.commit()
    # Recreate production triggers directly from SCHEMA_SQL
    for stmt in _split_schema_sql_statements(SCHEMA_SQL):
        if "CREATE TRIGGER" in stmt.upper():
            conn.execute(stmt)
    conn.commit()


def test_valid_transitions_table_matches_registry(test_env):
    """Verifies that the valid_transitions and required_transition_questions tables
    in SQLite are dynamically synchronized with TransitionRegistry."""
    db_path = test_env["db_path"]
    reg = TransitionRegistry.load_default()
    conn = sqlite3.connect(db_path)

    # 1. Compare valid_transitions table
    db_transitions = {
        (r[0], r[1]) for r in conn.execute(
            "SELECT from_state, to_state FROM valid_transitions WHERE from_state IS NOT NULL"
        ).fetchall()
    }
    registry_transitions = set(reg._templates_by_edge.keys())
    assert db_transitions == registry_transitions, "valid_transitions table must match registry transitions"

    # 2. Compare required_transition_questions table
    db_questions = {
        (r[0], r[1], r[2]) for r in conn.execute(
            "SELECT from_state, to_state, question_id FROM required_transition_questions"
        ).fetchall()
    }
    expected_questions = {
        (edge[0], edge[1], q["question_id"])
        for edge, tmpl in reg._templates_by_edge.items()
        for q in tmpl.human_questions
        if not q.get("asked_when")  # conditional review-selection questions are enforced by review_selection.py, not the DB
    } | {
        (edge[0], edge[1], question_id)
        for edge, tmpl in reg._templates_by_edge.items()
        for question_id in (tmpl.stage_question_ids or [])
    } | {
        (edge[0], edge[1], f"approval_{tmpl.transition_id}")
        for edge, tmpl in reg._templates_by_edge.items()
        if tmpl.approval.get("required") and tmpl.approval.get("approver_role", "human") == "human"
    }
    assert db_questions == expected_questions, "required_transition_questions table must match registry questions and approvals"
    conn.close()


def test_all_deterministic_edges_from_valid_transitions_table(test_env):
    """Dynamically tests ALL deterministic edges directly queried from the valid_transitions table.
    Any edge present in valid_transitions without entries in required_transition_questions
    is verified to transition smoothly without requiring decision rows.
    If new valid state transitions are added to the table, they are tested automatically
    without modifying this test suite."""
    db_path = test_env["db_path"]
    conn = sqlite3.connect(db_path)

    deterministic_edges = conn.execute("""
        SELECT vt.from_state, vt.to_state
        FROM valid_transitions vt
        WHERE vt.from_state IS NOT NULL
          AND vt.requires_proof = 0
          AND NOT EXISTS (
              SELECT 1 FROM required_transition_questions rq
              WHERE rq.from_state = vt.from_state AND rq.to_state = vt.to_state
          )
        ORDER BY vt.from_state, vt.to_state
    """).fetchall()
    assert len(deterministic_edges) > 0, "Source table empty or unsynced: valid_transitions has 0 deterministic edges"
    # Proof edges (requires_proof = 1: APPROVED, VERIFY_EXIT and DONE inbound) are not "deterministic": they are
    # cryptographic gates, blocked by the trigger without a consumed transition_request (asserted below).
    # Floor lowered 28 -> 20 (2026-09-20): the closure edges into DONE joined the proof set.
    assert len(deterministic_edges) >= 20, f"Expected at least 20 deterministic edges in valid_transitions, got {len(deterministic_edges)}"

    tested_edges = 0
    for idx, (from_s, to_s) in enumerate(deterministic_edges):
        tested_edges += 1
        task_id = f"task-det-{idx:03d}"
        _seed_task_at_state(conn, task_id, from_s)

        # Update to to_s via raw SQL
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, task_id))
        conn.commit()

        row = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()
        assert row[0] == to_s, f"Deterministic edge {from_s} -> {to_s} from valid_transitions must not be blocked by question trigger!"

        # Assert no violation was logged
        v = conn.execute(
            "SELECT COUNT(*) FROM transition_violations WHERE task_id = ? AND attempted_from_state = ? AND attempted_to_state = ?",
            (task_id, from_s, to_s)
        ).fetchone()[0]
        assert v == 0, f"No violation should be logged for valid deterministic edge {from_s} -> {to_s}"

    assert tested_edges == len(deterministic_edges)
    assert tested_edges > 0, "Zero deterministic edges were executed"
    conn.close()

    conn = sqlite3.connect(db_path)
    proof_edges = conn.execute(
        "SELECT from_state, to_state FROM valid_transitions WHERE from_state IS NOT NULL AND requires_proof = 1 ORDER BY 1, 2"
    ).fetchall()
    assert len(proof_edges) > 0
    for idx, (from_s, to_s) in enumerate(proof_edges):
        task_id = f"task-proof-{idx:03d}"
        _seed_task_at_state(conn, task_id, from_s)
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, task_id))
        conn.commit()
        assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == from_s, (
            f"Proof edge {from_s} -> {to_s} must be reverted by the trigger without a consumed transition_request"
        )
        assert conn.execute(
            "SELECT COUNT(*) FROM transition_violations WHERE task_id = ? AND attempted_from_state = ? AND attempted_to_state = ?",
            (task_id, from_s, to_s),
        ).fetchone()[0] >= 1

def test_all_question_edges_from_valid_transitions_table(test_env):
    """Dynamically tests ALL question-requiring edges directly queried from SQLite tables.
    For every edge in valid_transitions that has required_transition_questions,
    tests all 5 permutations dynamically:
    1. Zero decision rows -> REJECTED & LOGGED
    2. actor='agent' (synthetic bypass) -> REJECTED & LOGGED
    3. Wrong question_id -> REJECTED & LOGGED
    4. Empty/null answer -> REJECTED & LOGGED
    5. actor='human', matching question_id, non-empty answer -> SUCCEEDS
    If transitions or questions are added or adjusted, this test suite requires zero changes."""
    db_path = test_env["db_path"]
    conn = sqlite3.connect(db_path)

    question_edges = conn.execute("""
        SELECT DISTINCT vt.from_state, vt.to_state
        FROM valid_transitions vt
        JOIN required_transition_questions rq
          ON vt.from_state = rq.from_state AND vt.to_state = rq.to_state
        ORDER BY vt.from_state, vt.to_state
    """).fetchall()
    assert len(question_edges) > 0, "Source table empty or unsynced: valid_transitions has 0 question edges"
    assert len(question_edges) >= 15, f"Expected at least 15 question edges in valid_transitions, got {len(question_edges)}"

    tested_question_edges = 0
    for from_s, to_s in question_edges:
        tested_question_edges += 1
        required_qids = [
            r[0] for r in conn.execute(
                "SELECT question_id FROM required_transition_questions WHERE from_state = ? AND to_state = ?",
                (from_s, to_s)
            ).fetchall()
        ]
        assert len(required_qids) > 0, f"Question edge {from_s} -> {to_s} has 0 required questions in required_transition_questions"

        # Permutation 1: Zero decision rows -> REJECTED
        t1 = f"t1-{from_s}-{to_s}"
        _seed_task_at_state(conn, t1, from_s)
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, t1))
        conn.commit()
        assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t1,)).fetchone()[0] == from_s
        assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (t1,)).fetchone()[0] >= 1

        # Permutation 2: actor='agent' (synthetic agent bypass) -> REJECTED, except for
        # the retrospective decision gate, which explicitly permits either actor.
        t2 = f"t2-{from_s}-{to_s}"
        _seed_task_at_state(conn, t2, from_s)
        for qid in required_qids:
            conn.execute(
                """
                INSERT INTO transition_decisions (
                    task_id, source_occupancy_transition_id, from_state, to_state,
                    question_id, answer, decision_type, actor, recorded_at
                ) VALUES (?, 1, ?, ?, ?, 'Agent answer', 'ANSWER', 'agent', 12345.0)
                """,
                (t2, from_s, to_s, qid)
            )
        conn.commit()
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, t2))
        conn.commit()
        if (from_s, to_s) == (STATE_RETROSPECTIVE, STATE_DONE) and "retrospective_decision" in required_qids:
            assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t2,)).fetchone()[0] == to_s
        else:
            assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t2,)).fetchone()[0] == from_s
            assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (t2,)).fetchone()[0] >= 1

        # Permutation 3: Wrong question_id -> REJECTED
        t3 = f"t3-{from_s}-{to_s}"
        _seed_task_at_state(conn, t3, from_s)
        conn.execute(
            """
            INSERT INTO transition_decisions (
                task_id, source_occupancy_transition_id, from_state, to_state,
                question_id, answer, decision_type, actor, recorded_at
            ) VALUES (?, 1, ?, ?, 'totally_wrong_question_id', 'Human answer', 'ANSWER', 'human', 12345.0)
            """,
            (t3, from_s, to_s)
        )
        conn.commit()
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, t3))
        conn.commit()
        assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t3,)).fetchone()[0] == from_s
        assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (t3,)).fetchone()[0] >= 1

        # Permutation 4: Empty answer -> REJECTED
        t4 = f"t4-{from_s}-{to_s}"
        _seed_task_at_state(conn, t4, from_s)
        for qid in required_qids:
            conn.execute(
                """
                INSERT INTO transition_decisions (
                    task_id, source_occupancy_transition_id, from_state, to_state,
                    question_id, answer, decision_type, actor, recorded_at
                ) VALUES (?, 1, ?, ?, ?, '   ', 'ANSWER', 'human', 12345.0)
                """,
                (t4, from_s, to_s, qid)
            )
        conn.commit()
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, t4))
        conn.commit()
        assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t4,)).fetchone()[0] == from_s
        assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (t4,)).fetchone()[0] >= 1

        # Permutation 5: Valid human decision for ALL required questions -> SUCCEEDS
        t5 = f"t5-{from_s}-{to_s}"
        _seed_task_at_state(conn, t5, from_s)
        for qid in required_qids:
            conn.execute(
                """
                INSERT INTO transition_decisions (
                    task_id, source_occupancy_transition_id, from_state, to_state,
                    question_id, answer, decision_type, actor, recorded_at
                ) VALUES (?, 1, ?, ?, ?, 'Valid Human Answer', 'ANSWER', 'human', 12345.0)
                """,
                (t5, from_s, to_s, qid)
            )
        conn.commit()
        conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, t5))
        conn.commit()
        assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (t5,)).fetchone()[0] == to_s, \
            f"Valid human decision must allow {from_s} -> {to_s} to commit!"

    assert tested_question_edges == len(question_edges)
    assert tested_question_edges > 0, "Zero question edges were executed"
    conn.close()


def test_dynamically_added_transitions_enforced_without_test_suite_changes(test_env):
    """Verifies that when new transitions are inserted into the valid_transitions table
    (and optional required_transition_questions), the database trigger enforces them
    automatically without requiring any modifications to the test suite."""
    db_path = test_env["db_path"]
    conn = sqlite3.connect(db_path)

    # 1. Dynamically add a new deterministic transition: VERIFY_EXIT -> DRAFT_PLAN
    conn.execute("INSERT INTO valid_transitions (from_state, to_state) VALUES ('VERIFY_EXIT', 'DRAFT_PLAN')")
    conn.commit()

    task_det = "task-dyn-det-01"
    _seed_task_at_state(conn, task_det, STATE_VERIFY_EXIT)
    conn.execute("UPDATE tasks SET state = 'DRAFT_PLAN' WHERE task_id = ?", (task_det,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_det,)).fetchone()[0] == STATE_DRAFT_PLAN
    assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (task_det,)).fetchone()[0] == 0

    # 2. Dynamically add a new question-required transition: VERIFY_EXIT -> APPROVED
    conn.execute("INSERT INTO valid_transitions (from_state, to_state) VALUES ('VERIFY_EXIT', 'APPROVED')")
    conn.execute("INSERT INTO required_transition_questions (from_state, to_state, question_id) VALUES ('VERIFY_EXIT', 'APPROVED', 'q_dynamic_approval')")
    conn.commit()

    task_q = "task-dyn-q-01"
    _seed_task_at_state(conn, task_q, STATE_VERIFY_EXIT)

    # Attempt transition with no decision -> REJECTED
    conn.execute("UPDATE tasks SET state = 'APPROVED' WHERE task_id = ?", (task_q,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_q,)).fetchone()[0] == STATE_VERIFY_EXIT
    assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (task_q,)).fetchone()[0] >= 1

    # Attempt transition with actor='agent' -> REJECTED
    conn.execute(
        """
        INSERT INTO transition_decisions (
            task_id, source_occupancy_transition_id, from_state, to_state,
            question_id, answer, decision_type, actor, recorded_at
        ) VALUES (?, 1, 'VERIFY_EXIT', 'APPROVED', 'q_dynamic_approval', 'agent bypass', 'ANSWER', 'agent', 12345.0)
        """,
        (task_q,)
    )
    conn.commit()
    conn.execute("UPDATE tasks SET state = 'APPROVED' WHERE task_id = ?", (task_q,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_q,)).fetchone()[0] == STATE_VERIFY_EXIT

    # Provide valid human decision -> SUCCEEDS
    conn.execute(
        """
        INSERT INTO transition_decisions (
            task_id, source_occupancy_transition_id, from_state, to_state,
            question_id, answer, decision_type, actor, recorded_at
        ) VALUES (?, 1, 'VERIFY_EXIT', 'APPROVED', 'q_dynamic_approval', 'human approval confirmed', 'ANSWER', 'human', 12345.0)
        """,
        (task_q,)
    )
    conn.commit()
    conn.execute("UPDATE tasks SET state = 'APPROVED' WHERE task_id = ?", (task_q,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_q,)).fetchone()[0] == STATE_APPROVED

    # 3. Invalid transition not in valid_transitions table: VERIFY_EXIT -> MULTI_AGENT_REVIEW -> REJECTED
    task_inv = "task-dyn-inv-01"
    _seed_task_at_state(conn, task_inv, STATE_VERIFY_EXIT)
    conn.execute("UPDATE tasks SET state = 'MULTI_AGENT_REVIEW' WHERE task_id = ?", (task_inv,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_inv,)).fetchone()[0] == STATE_VERIFY_EXIT
    assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (task_inv,)).fetchone()[0] >= 1

    conn.close()


@pytest.mark.no_auto_signer
def test_reproduce_live_self_approval_bypass_rejected(test_env):
    """Slice: Literal reproduction of the live bypass.
    An agent programmatically executes record-human-approval / transition to APPROVED
    with actor='human' but without genuine interactive stdin input.
    The database trigger MUST reject the transition, restore AWAITING_APPROVAL,
    and record a violation in transition_violations.
    """
    cp = test_env["cp"]
    db_path = test_env["db_path"]
    task_id = "task-repro-bypass-001"

    # Setup task at AWAITING_APPROVAL
    conn = sqlite3.connect(db_path)
    _seed_task_at_state(conn, task_id, STATE_AWAITING_APPROVAL)

    row = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()
    assert row[0] == STATE_AWAITING_APPROVAL

    # ATTEMPT 1: Raw SQL UPDATE bypassing coordinator with zero decisions recorded
    conn.execute("UPDATE tasks SET state = 'APPROVED' WHERE task_id = ?", (task_id,))
    conn.commit()

    # Verify that database trigger silently reverted state and logged violation
    row_after_raw = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()
    assert row_after_raw[0] == STATE_AWAITING_APPROVAL, "Raw SQL update to APPROVED without decision must be reverted by trigger!"

    violations = conn.execute(
        "SELECT task_id, attempted_from_state, attempted_to_state FROM transition_violations WHERE task_id = ?",
        (task_id,)
    ).fetchall()
    assert len(violations) >= 1
    assert violations[-1] == (task_id, STATE_AWAITING_APPROVAL, STATE_APPROVED)

    # ATTEMPT 2: Programmatic cp.transition call with actor='human' without recorded human decision
    with pytest.raises((PersistenceInvariantViolation, ConcurrentModificationError)):
        cp.transition(task_id, STATE_APPROVED, actor="human", reason="Self-approved without decision row")

    row_after_call = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()
    assert row_after_call[0] == STATE_AWAITING_APPROVAL

    # ATTEMPT 3: Non-interactive coordinate_transition passing programmatic answers dict
    reg = TransitionRegistry.load_default()
    coord = TransitionCoordinator(control_plane=cp, registry=reg)

    from control_plane.coordinator import HumanProofRequired
    from control_plane.snapshot import gate1_artifact_paths
    for _label, _path in gate1_artifact_paths(coord._resolve_repo_root(), task_id):  # reviewed content exists, so the refusal is about authority
        _path.parent.mkdir(parents=True, exist_ok=True)
        _path.write_text("reviewed\n")

    # Programmatic answers and an actor string are not authority: the coordinator halts and asks for a signature.
    with pytest.raises(HumanProofRequired):
        coord.coordinate_transition(
            task_id=task_id,
            to_state=STATE_APPROVED,
            actor="human",
            reason="Synthetic approval",
            interactive=False,
            provided_answers={"human_implementation_approval": "Yes, approve implementation [Recommended]"},
            approval_decision="APPROVAL"
        )

    row_after_coord = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()
    assert row_after_coord[0] == STATE_AWAITING_APPROVAL
    conn.close()


def test_interactive_coordinator_commits_approval_via_a_signed_request_and_writes_no_decision_rows(test_env):
    """auth-ciba-increment-b: APPROVED is a cryptographic gate. An interactive coordinator session completes it
    only through a signed transition_request (the test human signs for real); the consumed request is the
    authority, so no transition_decisions rows (and no actor='human' strings) are written for the edge."""
    cp = test_env["cp"]
    db_path = test_env["db_path"]
    task_id = "task-interactive-success-003"
    conn = sqlite3.connect(db_path)
    _seed_task_at_state(conn, task_id, STATE_AWAITING_APPROVAL)
    conn.close()

    reg = TransitionRegistry.load_default()
    coord = TransitionCoordinator(control_plane=cp, registry=reg)  # signer supplied by the test human (conftest)

    record = coord.coordinate_transition(
        task_id=task_id,
        to_state=STATE_APPROVED,
        actor="human",
        reason="Interactive approval from human",
        interactive=True
    )
    assert record.to_state == STATE_APPROVED

    conn = sqlite3.connect(db_path)
    state = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0]
    assert state == STATE_APPROVED

    consumed = conn.execute(
        "SELECT status FROM transition_request WHERE task_id = ? AND to_state = 'APPROVED'", (task_id,)
    ).fetchall()
    assert consumed == [("CONSUMED",)]
    decisions = conn.execute(
        "SELECT COUNT(*) FROM transition_decisions WHERE task_id = ? AND to_state = 'APPROVED'", (task_id,)
    ).fetchone()[0]
    assert decisions == 0
    conn.close()

def test_schema_rebuild_preserves_augmented_trigger_and_required_questions(test_env):
    """Slice: Verifies that _rebuild_schema_transactional drops and recreates the
    augmented trigger cleanly without trigger loss or schema drift (Issue #552 parity)."""
    db_path = test_env["db_path"]
    adapter = test_env["adapter"]
    conn = adapter.get_connection()

    # Force a schema rebuild
    adapter._rebuild_schema_transactional(conn)
    adapter._sync_valid_transitions(conn)

    # Verify table and trigger exist
    req_q_count = conn.execute("SELECT COUNT(*) FROM required_transition_questions").fetchone()[0]
    assert req_q_count > 0, "required_transition_questions must be populated after rebuild"

    triggers = conn.execute(
        "SELECT name, sql FROM sqlite_master WHERE type='trigger' AND name='enforce_valid_transition'"
    ).fetchone()
    assert triggers is not None
    assert "required_transition_questions" in triggers[1]
    conn.close()


def test_approval_only_edge_rejects_forged_agent_approval(test_env):
    """Regression test (a): An edge with approval.required=True and no human_questions.
    Confirms a forged actor='agent' APPROVAL decision is rejected by the database trigger,
    and only an actor='human' APPROVAL decision is allowed to commit."""
    db_path = test_env["db_path"]
    conn = sqlite3.connect(db_path)

    from_s, to_s = STATE_VERIFY_EXIT, STATE_APPROVED
    approval_qid = "approval_verify_exit_to_approved"

    # Seed transition in valid_transitions and required_transition_questions without human_questions
    conn.execute("INSERT INTO valid_transitions (from_state, to_state) VALUES (?, ?)", (from_s, to_s))
    conn.execute("INSERT INTO required_transition_questions (from_state, to_state, question_id) VALUES (?, ?, ?)",
                 (from_s, to_s, approval_qid))
    conn.commit()

    task_id = "task-approval-only-001"
    _seed_task_at_state(conn, task_id, from_s)

    # 1. Attempt transition with actor='agent' APPROVAL decision -> MUST BE REJECTED
    conn.execute(
        """
        INSERT INTO transition_decisions (
            task_id, source_occupancy_transition_id, from_state, to_state,
            question_id, answer, decision_type, actor, recorded_at
        ) VALUES (?, 1, ?, ?, ?, 'APPROVAL', 'APPROVAL', 'agent', 12345.0)
        """,
        (task_id, from_s, to_s, approval_qid)
    )
    conn.commit()
    conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, task_id))
    conn.commit()

    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == from_s
    violations = conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ?", (task_id,)).fetchone()[0]
    assert violations >= 1, "Violations must be logged when actor='agent' attempts approval-gated transition"

    # 2. Attempt transition with actor='human' APPROVAL decision -> SUCCEEDS
    conn.execute(
        """
        INSERT INTO transition_decisions (
            task_id, source_occupancy_transition_id, from_state, to_state,
            question_id, answer, decision_type, actor, recorded_at
        ) VALUES (?, 1, ?, ?, ?, 'APPROVAL', 'APPROVAL', 'human', 12345.0)
        """,
        (task_id, from_s, to_s, approval_qid)
    )
    conn.commit()
    conn.execute("UPDATE tasks SET state = ? WHERE task_id = ?", (to_s, task_id))
    conn.commit()

    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == to_s
    conn.close()


def test_non_interactive_approval_always_records_actor_agent(test_env):
    """Regression test (b): Confirms the non-interactive approval path in coordinator.py
    always records actor='agent', never 'human', regardless of approver_role."""
    cp = test_env["cp"]
    db_path = test_env["db_path"]
    reg = TransitionRegistry.load_default()

    task_id = "task-non-interactive-approval"
    conn = sqlite3.connect(db_path)
    _seed_task_at_state(conn, task_id, STATE_AWAITING_APPROVAL)
    conn.close()

    coord = TransitionCoordinator(control_plane=cp, registry=reg)

    # When non-interactive coordination is called with programmatic approval,
    # coordinator must record decision with actor='agent' (which then fails at DB trigger)
    with pytest.raises((PersistenceInvariantViolation, TransitionCoordinatorError)):
        coord.coordinate_transition(
            task_id=task_id,
            to_state=STATE_APPROVED,
            actor="agent",
            reason="Non-interactive approval attempt",
            interactive=False,
            provided_answers={"human_implementation_approval": "Approved"},
            approval_decision="APPROVAL",
        )

    conn = sqlite3.connect(db_path)
    # Check that any recorded/staged approval decision was stamped with actor='agent'
    decisions = conn.execute(
        "SELECT actor FROM transition_decisions WHERE task_id = ? AND decision_type = 'APPROVAL'",
        (task_id,)
    ).fetchall()
    for row in decisions:
        assert row[0] == "agent", f"Non-interactive approval must record actor='agent', got {row[0]}"
    conn.close()


def test_stale_decision_replay_across_occupancies_fails(test_env):
    """RED test: A task visits a human-gated edge, records a valid decision, and completes the transition.
    The task then leaves and re-enters the human-gated state (new occupancy).
    Attempting to transition out of the human-gated state without recording a NEW decision must FAIL:
    the prior occupancy's decision must NOT satisfy the database trigger!
    """
    cp = test_env["cp"]
    db_path = test_env["db_path"]
    reg = TransitionRegistry.load_default()
    conn = sqlite3.connect(db_path)

    task_id = "task-stale-replay-001"
    # Seed task at AWAITING_APPROVAL (initial occupancy, e.g. trans_id 1)
    _seed_task_at_state(conn, task_id, STATE_AWAITING_APPROVAL)
    conn.close()

    # Step (a): Complete valid human-approved transition to APPROVED
    inputs = iter(["1", "y", "YES"])
    coord = TransitionCoordinator(control_plane=cp, registry=reg, input_fn=lambda prompt: next(inputs))
    record1 = coord.coordinate_transition(
        task_id=task_id,
        to_state=STATE_APPROVED,
        actor="human",
        reason="Initial valid human approval in occupancy 1",
        interactive=True
    )
    assert record1.to_state == STATE_APPROVED

    conn = sqlite3.connect(db_path)
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == STATE_APPROVED

    # Step (b): Task leaves APPROVED and re-enters AWAITING_APPROVAL (occupancy changes)
    _seed_task_at_state(conn, task_id, STATE_AWAITING_APPROVAL)
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == STATE_AWAITING_APPROVAL

    # Step (c): Attempt transition from AWAITING_APPROVAL -> APPROVED WITHOUT recording new decisions!
    # 1. Raw SQL UPDATE test
    conn.execute("UPDATE tasks SET state = 'APPROVED' WHERE task_id = ?", (task_id,))
    conn.commit()

    # If trigger permits stale replay, state becomes APPROVED. The test asserts that trigger MUST revert it back to AWAITING_APPROVAL!
    state_after_raw = conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0]
    assert state_after_raw == STATE_AWAITING_APPROVAL, (
        "CRITICAL VULNERABILITY: Stale decision from prior occupancy satisfied trigger on re-entry! "
        "State was transitioned to APPROVED without fresh human decision."
    )
    violations = conn.execute(
        "SELECT COUNT(*) FROM transition_violations WHERE task_id = ? AND attempted_from_state = 'AWAITING_APPROVAL' AND attempted_to_state = 'APPROVED'",
        (task_id,)
    ).fetchone()[0]
    assert violations >= 1, "Expected violation to be logged when stale decision is replayed"
    conn.close()


def test_recovery_approval_alignment_and_security_guarantees(test_env):
    """Regression test proving that recovery approval alignment with static template question IDs
    strictly preserves all #529 anti-replay, occupancy-binding, and single-use token guarantees:
    1. Static question alignment: decisions recorded match required_transition_questions.
    2. Token integrity: cryptographic token is stored in the answer column.
    3. Token uniqueness: successive approvals generate distinct cryptographic tokens.
    4. Occupancy binding: approval issued in occupancy 1 cannot be used in occupancy 2.
    5. Trigger enforcement: transition without unconsumed approval decision is reverted by trigger.
    6. Atomic consumption: applying recovery transitions marks decisions consumed and binds transition_id.
    7. Anti-replay: consumed token cannot be reused sequentially.
    """
    adapter = test_env["adapter"]
    db_path = test_env["db_path"]
    task_id = "task-recovery-guarantee-001"

    # Setup: create task and move to ESCALATED
    conn = sqlite3.connect(db_path)
    _seed_task_at_state(conn, task_id, STATE_ESCALATED)
    # Determine initial occupancy ID
    last_trans = conn.execute("SELECT transition_id FROM task_transitions WHERE task_id = ? ORDER BY transition_id DESC LIMIT 1", (task_id,)).fetchone()
    occ1 = last_trans[0]
    conn.close()

    # 1. Trigger blocks raw recovery transition without approval decision
    conn = sqlite3.connect(db_path)
    conn.execute("UPDATE tasks SET state = 'INTAKE' WHERE task_id = ?", (task_id,))
    conn.commit()
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == STATE_ESCALATED, \
        "Raw transition to INTAKE without recovery approval must be reverted by trigger!"
    assert conn.execute("SELECT COUNT(*) FROM transition_violations WHERE task_id = ? AND attempted_from_state = 'ESCALATED' AND attempted_to_state = 'INTAKE'", (task_id,)).fetchone()[0] >= 1
    conn.close()

    # 2. Issue approval and test token uniqueness
    token1 = adapter.record_recovery_approval(
        task_id=task_id,
        expected_source_state=STATE_ESCALATED,
        destination_state=STATE_INTAKE,
        source_occupancy_transition_id=occ1,
        approver="admin",
        actor="human",
        decision="APPROVAL",
        reason="First approval"
    )
    token2 = adapter.record_recovery_approval(
        task_id=task_id,
        expected_source_state=STATE_ESCALATED,
        destination_state=STATE_INTAKE,
        source_occupancy_transition_id=occ1,
        approver="admin",
        actor="human",
        decision="APPROVAL",
        reason="Second approval"
    )
    assert token1 != token2, "Successive recovery approvals must generate distinct tokens (token uniqueness)"

    # 3. Verify static question alignment and answer column contents in transition_decisions
    conn = sqlite3.connect(db_path)
    decisions = conn.execute(
        "SELECT question_id, answer, actor, consumed_at FROM transition_decisions WHERE task_id = ? AND answer = ?",
        (task_id, token1)
    ).fetchall()
    qids = {r[0] for r in decisions}
    assert "recovery_approval_escalated_to_intake" in qids, "Must contain template human_questions question_id"
    assert "approval_escalated_to_intake" in qids, "Must contain template approval question_id"
    for r in decisions:
        assert r[1] == token1, "Token must be preserved in answer column"
        assert r[2] == "human", "Recovery approval actor must be human"
        assert r[3] is None, "Newly issued approval decision must have consumed_at IS NULL"
    conn.close()

    # 4. Test atomic recovery execution
    rec = adapter.apply_recovery_transition(
        task_id=task_id,
        expected_source_state=STATE_ESCALATED,
        destination_state=STATE_INTAKE,
        source_occupancy_transition_id=occ1,
        approval_receipt_token=token1,
        actor="admin",
        reason="De-escalate to INTAKE"
    )
    assert rec.to_state == STATE_INTAKE

    # 5. Verify decisions are now marked consumed
    conn = sqlite3.connect(db_path)
    assert conn.execute("SELECT state FROM tasks WHERE task_id = ?", (task_id,)).fetchone()[0] == STATE_INTAKE
    consumed_decisions = conn.execute(
        "SELECT consumed_at, bound_transition_id FROM transition_decisions WHERE task_id = ? AND answer = ?",
        (task_id, token1)
    ).fetchall()
    for c_at, b_tid in consumed_decisions:
        assert c_at is not None, "Applied approval decisions must be marked consumed"
        assert b_tid == rec.transition_id, "Applied approval decisions must be bound to new transition"
    conn.close()

    # 6. Anti-replay: sequential replay attempt with current state fails
    with pytest.raises(ValueError):
        adapter.apply_recovery_transition(
            task_id=task_id,
            expected_source_state=STATE_INTAKE,
            destination_state=STATE_INTAKE,
            source_occupancy_transition_id=occ1,
            approval_receipt_token=token1,
            actor="admin",
            reason="Replay attempt"
        )

    # 7. Occupancy binding: token2 was issued in occ1; re-entering ESCALATED (occ2) rejects token2
    conn = sqlite3.connect(db_path)
    _seed_task_at_state(conn, task_id, STATE_ESCALATED)
    last_trans2 = conn.execute("SELECT transition_id FROM task_transitions WHERE task_id = ? ORDER BY transition_id DESC LIMIT 1", (task_id,)).fetchone()
    occ2 = last_trans2[0]
    assert occ2 > occ1
    conn.close()

    # Attempting to use token2 (issued for occ1) while task is in occ2 must fail
    with pytest.raises(ValueError, match="Stale occupancy ID"):
        adapter.apply_recovery_transition(
            task_id=task_id,
            expected_source_state=STATE_ESCALATED,
            destination_state=STATE_INTAKE,
            source_occupancy_transition_id=occ1,
            approval_receipt_token=token2,
            actor="admin",
            reason="Attempt using token from old occupancy"
        )

    # Attempting to use token1 (already consumed in occ1) in occ2 must fail
    with pytest.raises(ValueError, match="Stale occupancy ID"):
        adapter.apply_recovery_transition(
            task_id=task_id,
            expected_source_state=STATE_ESCALATED,
            destination_state=STATE_INTAKE,
            source_occupancy_transition_id=occ1,
            approval_receipt_token=token1,
            actor="admin",
            reason="Attempt reusing consumed token from old occupancy"
        )
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall