@acms @acms_fusion
Feature: ACMS Strategy Coordinator and Fusion Engine
  As a CleverAgents developer
  I want named facades for strategy coordination and fragment fusion
  So that the ACMS pipeline has clean public APIs for parallel strategy
  execution with proportional budget allocation and fragment fusion
  with dedup, conflict resolution, and knapsack packing

  # ---------------------------------------------------------------------------
  # StrategyCoordinator — Parallel execution
  # ---------------------------------------------------------------------------

  @fusion @coordinator
  Scenario: StrategyCoordinator coordinates a single strategy
    Given the fusion modules are available
    And fusion test strategies with confidences 0.8 and 0.6
    And fusion test fragments with token counts 50 and 50
    When I coordinate with a budget of 200 tokens
    Then the coordination result should contain fragments
    And the coordination result should list strategies used

  @fusion @coordinator
  Scenario: StrategyCoordinator handles empty strategies list
    Given the fusion modules are available
    And fusion test fragments with token counts 50 and 50
    When I coordinate with no strategies and a budget of 200 tokens
    Then the coordination result should contain 0 fragments

  @fusion @coordinator
  Scenario: StrategyCoordinator reports circuit-broken strategies
    Given the fusion modules are available
    And a coordinator with a pre-broken circuit for "broken_strat"
    And fusion test fragments with token counts 50 and 50
    When I coordinate with the circuit-broken strategy
    Then the coordination result should report circuit-broken strategies

  # ---------------------------------------------------------------------------
  # StrategyCoordinator — Budget allocation proportional to confidence
  # ---------------------------------------------------------------------------

  @fusion @coordinator @budget
  Scenario: StrategyCoordinator allocates budget proportionally
    Given the fusion modules are available
    And fusion test strategies with confidences 0.8 and 0.2
    And fusion test fragments with token counts 50 and 50
    When I coordinate with a budget of 1000 tokens
    Then the allocation for the first strategy should be approximately 800
    And the allocation for the second strategy should be approximately 200
    And the total allocation should equal 1000

  # ---------------------------------------------------------------------------
  # StrategyCoordinator — Per-strategy max caps
  # ---------------------------------------------------------------------------

  @fusion @coordinator @caps
  Scenario: StrategyCoordinator enforces per-strategy max caps
    Given the fusion modules are available
    And a coordinator with per-strategy max cap of 300
    And fusion test strategies with confidences 0.9 and 0.1
    And fusion test fragments with token counts 50 and 50
    When I coordinate with a budget of 1000 tokens using the capped coordinator
    Then no strategy allocation should exceed 300 tokens

  # ---------------------------------------------------------------------------
  # FusionEngine — Dedup by URI + hash
  # ---------------------------------------------------------------------------

  @fusion @engine @dedup
  Scenario: FusionEngine deduplicates by UKO URI and content hash
    Given the fusion modules are available
    And fusion fragments with duplicates by URI and content
    When I fuse with a budget of 500 tokens
    Then the fusion result dedup count should be greater than 0
    And the fusion result should contain fewer fragments than input

  # ---------------------------------------------------------------------------
  # FusionEngine — Detail conflict resolution
  # ---------------------------------------------------------------------------

  @fusion @engine @depth
  Scenario: FusionEngine resolves detail depth conflicts to max depth
    Given the fusion modules are available
    And fusion fragments with same UKO node at depths 2 and 5
    When I fuse with a budget of 500 tokens
    Then the fusion result should retain the depth 5 fragment
    And the fusion result depth resolved count should be greater than 0

  # ---------------------------------------------------------------------------
  # FusionEngine — Greedy knapsack packing with tie-breakers
  # ---------------------------------------------------------------------------

  @fusion @engine @packing
  Scenario: FusionEngine packs fragments within budget using greedy knapsack
    Given the fusion modules are available
    And fusion fragments totaling 600 tokens
    When I fuse with a budget of 400 tokens
    Then the fusion result total tokens should be at most 400
    And the fusion result should contain fewer fragments than input

  @fusion @engine @packing
  Scenario: FusionEngine uses deterministic tie-breakers
    Given the fusion modules are available
    And fusion fragments with equal relevance but different depths
    When I fuse with a budget of 500 tokens
    Then the fusion result fragments should be ordered by depth descending then token count ascending

  # ---------------------------------------------------------------------------
  # FusionEngine — Budget overage guard
  # ---------------------------------------------------------------------------

  @fusion @engine @overage
  Scenario: FusionEngine budget overage guard drops lowest-relevance fragments
    Given the fusion modules are available
    And a fusion engine with overage guard enabled
    And fusion fragments that cause budget overage
    When I fuse with the overage-prone fragments and a budget of 100 tokens
    Then the fusion result total tokens should be at most 100
    And the fusion result dropped by overage guard should be greater than 0

  @fusion @engine @overage
  Scenario: FusionEngine overage guard does not drop when within budget
    Given the fusion modules are available
    And a fusion engine with overage guard enabled
    And fusion fragments totaling exactly 100 tokens
    When I fuse with the exact fragments and a budget of 100 tokens
    Then the fusion result dropped by overage guard should be 0

  # ---------------------------------------------------------------------------
  # FusionEngine — Empty input
  # ---------------------------------------------------------------------------

  @fusion @engine
  Scenario: FusionEngine handles empty input gracefully
    Given the fusion modules are available
    When I fuse with no fragments and a budget of 500 tokens
    Then the fusion result should contain 0 fragments
    And the fusion result input count should be 0

  # ---------------------------------------------------------------------------
  # Integration: Coordinator + FusionEngine
  # ---------------------------------------------------------------------------

  @fusion @integration
  Scenario: StrategyCoordinator output feeds into FusionEngine
    Given the fusion modules are available
    And fusion test strategies with confidences 0.8 and 0.6
    And fusion test fragments with token counts 50 and 50
    When I coordinate then fuse with a budget of 200 tokens
    Then the final fused result should contain fragments within budget

  # ---------------------------------------------------------------------------
  # Configuration
  # ---------------------------------------------------------------------------

  @fusion @config
  Scenario: CoordinatorConfig defaults are sensible
    Given the fusion modules are available
    When I create a default CoordinatorConfig
    Then the config min_useful_budget should be 64
    And the config executor_timeout should be 30.0
    And the config per_strategy_max_cap should be None

  @fusion @config
  Scenario: FusionConfig defaults are sensible
    Given the fusion modules are available
    When I create a default FusionConfig
    Then the fusion config overage_guard_enabled should be True
    And the fusion config min_fragment_tokens should be 10
