@exec-env-precedence
Feature: 6-level execution environment precedence chain (#877)
  Verifies the full 6-level precedence chain: plan override,
  project override, nearest-ancestor devcontainer, plan fallback,
  project fallback, host default.  Also tests DAG walking for
  devcontainer discovery and container:// reference parsing.

  # ── 6-Level Precedence Chain ────────────────────────────

  Scenario: Level 1 — plan override wins over everything for precedence
    When I resolve precedence with plan_env "container" plan_priority "override" project_env "host" project_priority "override" devcontainer true
    Then the precedence resolved environment should be "container"

  Scenario: Level 2 — project override wins over devcontainer for precedence
    When I resolve precedence with plan_env "host" plan_priority "fallback" project_env "container" project_priority "override" devcontainer false
    Then the precedence resolved environment should be "container"

  Scenario: Level 3 — devcontainer wins over plan fallback for precedence
    When I resolve precedence with plan_env "host" plan_priority "fallback" project_env "host" project_priority "fallback" devcontainer true
    Then the precedence resolved environment should be "container"

  Scenario: Level 4 — plan fallback used when no devcontainer for precedence
    When I resolve precedence with plan_env "container" plan_priority "fallback" project_env "host" project_priority "fallback" devcontainer false
    Then the precedence resolved environment should be "container"

  Scenario: Level 5 — project fallback used when no plan env for precedence
    When I resolve precedence with project_env "container" project_priority "fallback" devcontainer false
    Then the precedence resolved environment should be "container"

  Scenario: Level 6 — host default when nothing configured for precedence
    When I resolve precedence with devcontainer false
    Then the precedence resolved environment should be "host"

  Scenario: Tool override trumps all 6 levels for precedence
    When I resolve precedence with tool_env "host" plan_env "container" plan_priority "override" devcontainer true
    Then the precedence resolved environment should be "host"

  Scenario: Plan override beats project override for precedence
    When I resolve precedence with plan_env "host" plan_priority "override" project_env "container" project_priority "override" devcontainer false
    Then the precedence resolved environment should be "host"

  Scenario: Project override beats devcontainer for precedence
    When I resolve precedence with project_env "host" project_priority "override" devcontainer true
    Then the precedence resolved environment should be "host"

  Scenario: Default priority is fallback for precedence
    When I resolve precedence with plan_env "host" project_env "host" devcontainer true
    Then the precedence resolved environment should be "container"

  # ── Devcontainer helpers ────────────────────────────────

  Scenario: has_devcontainer detects devcontainer-instance for precedence
    When I check has_devcontainer with types "git-checkout,devcontainer-instance" for precedence
    Then has_devcontainer should be true for precedence

  Scenario: has_devcontainer returns false without devcontainer for precedence
    When I check has_devcontainer with types "git-checkout,fs-directory" for precedence
    Then has_devcontainer should be false for precedence

  # ── ContextConfig priority field ────────────────────────

  Scenario: ContextConfig supports execution_env_priority field for precedence
    When I create a ContextConfig with priority "override" for precedence
    Then the context config priority should be "override" for precedence

  # ── DAG walk (#877 subtask 2) ───────────────────────────

  Scenario: DAG walk finds nearest devcontainer ancestor for precedence
    When I resolve with DAG walk from "res-file" with devcontainer ancestor for precedence
    Then the precedence resolved environment should be "container"

  Scenario: DAG walk returns host when no devcontainer ancestor for precedence
    When I resolve with DAG walk from "res-file" without devcontainer ancestor for precedence
    Then the precedence resolved environment should be "host"

  # ── Container reference (#877 subtask 4) ────────────────

  Scenario: Container reference parsed correctly for precedence
    When I parse container ref "container://local/my-dc" for precedence
    Then the container ref name should be "local/my-dc" for precedence

  Scenario: Container reference coerces to CONTAINER_REF for precedence
    When I resolve precedence with plan_env "container://local/my-dc" plan_priority "override" project_env "host" project_priority "fallback" devcontainer false
    Then the precedence resolved environment should be "container_ref"
