@phase2 @acms @semantic_chunking
Feature: SemanticChunkingStrategy — embedding-based context chunking
  As a CleverAgents developer
  I want a SemanticChunkingStrategy that uses embedding similarity
  So that the ACMS pipeline can retain the most semantically relevant
  context chunks for complex multi-turn agent workflows

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy has correct name
    Given a SemanticChunkingStrategy with default parameters
    Then the SemanticChunkingStrategy name should be "semantic_chunking"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy supports semantic search capability
    Given a SemanticChunkingStrategy with default parameters
    Then the SemanticChunkingStrategy should support semantic search

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy can_handle returns confidence score
    Given a SemanticChunkingStrategy with default parameters
    When I check can_handle on SemanticChunkingStrategy with query "test query"
    Then the SemanticChunkingStrategy confidence should be greater than 0.0

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy can_handle returns lower confidence without query
    Given a SemanticChunkingStrategy with default parameters
    When I check can_handle on SemanticChunkingStrategy without query
    Then the SemanticChunkingStrategy confidence should be 0.1

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy explain returns description
    Given a SemanticChunkingStrategy with default parameters
    Then the SemanticChunkingStrategy explain should contain "semantic"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy accepts embedding_model parameter
    Given a SemanticChunkingStrategy with embedding_model "text-embedding-ada-002"
    Then the SemanticChunkingStrategy embedding_model should be "text-embedding-ada-002"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy accepts top_k parameter
    Given a SemanticChunkingStrategy with top_k 5
    Then the SemanticChunkingStrategy top_k should be 5

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy has default top_k of 10
    Given a SemanticChunkingStrategy with default parameters
    Then the SemanticChunkingStrategy top_k should be 10

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy returns empty for empty input
    Given a SemanticChunkingStrategy with default parameters
    And an empty semantic chunking fragment list
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy with anchor "test query"
    Then the semantic chunking result should contain 0 fragments

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy ranks fragments by cosine similarity to anchor
    Given a SemanticChunkingStrategy with mock embeddings
    And the following semantic chunking fragments:
      | uko_node              | content                          | score | tokens | depth |
      | project://app/db.py   | database connection pool manager | 0.5   | 20     | 3     |
      | project://app/io.py   | file input output handler        | 0.5   | 15     | 3     |
      | project://app/sql.py  | SQL database query executor      | 0.5   | 25     | 3     |
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy with anchor "database connection"
    Then the first semantic chunking result should have uko_node "project://app/db.py"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy respects top_k limit
    Given a SemanticChunkingStrategy with top_k 2 and mock embeddings
    And the following semantic chunking fragments:
      | uko_node              | content                | score | tokens | depth |
      | project://app/a.py    | alpha beta gamma       | 0.5   | 10     | 3     |
      | project://app/b.py    | delta epsilon zeta     | 0.5   | 10     | 3     |
      | project://app/c.py    | eta theta iota         | 0.5   | 10     | 3     |
      | project://app/d.py    | kappa lambda mu        | 0.5   | 10     | 3     |
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy with anchor "alpha beta"
    Then the semantic chunking result should contain at most 2 fragments

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy respects token budget
    Given a SemanticChunkingStrategy with default parameters
    And the following semantic chunking fragments:
      | uko_node              | content     | score | tokens | depth |
      | project://app/a.py    | hello world | 0.9   | 100    | 3     |
      | project://app/b.py    | hello there | 0.7   | 100    | 3     |
      | project://app/c.py    | hello again | 0.5   | 100    | 3     |
    And a semantic chunking budget with max_tokens 250 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy with anchor "hello"
    Then the semantic chunking result should contain at most 2 fragments

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy falls back to relevance ordering without anchor
    Given a SemanticChunkingStrategy with default parameters
    And the following semantic chunking fragments:
      | uko_node              | content | score | tokens | depth |
      | project://app/a.py    | alpha   | 0.3   | 10     | 3     |
      | project://app/b.py    | beta    | 0.9   | 10     | 3     |
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy without anchor
    Then the first semantic chunking result should have uko_node "project://app/b.py"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy caches embeddings to avoid redundant calls
    Given a SemanticChunkingStrategy with call-counting mock embeddings
    And the following semantic chunking fragments:
      | uko_node              | content          | score | tokens | depth |
      | project://app/a.py    | same content     | 0.5   | 10     | 3     |
      | project://app/b.py    | same content     | 0.5   | 10     | 3     |
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble with the SemanticChunkingStrategy with anchor "test"
    Then the embedding model should be called fewer times than total fragments plus anchor

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy is registered in the ACMS pipeline
    Given an ACMS pipeline for semantic chunking tests
    Then the sc_pipeline should have strategy "semantic_chunking"

  @tdd_issue @tdd_issue_9996
  Scenario: SemanticChunkingStrategy can be used via ACMS pipeline assemble
    Given an ACMS pipeline for semantic chunking tests
    And a valid plan ID for semantic chunking
    And the following semantic chunking fragments:
      | uko_node              | content          | score | tokens | depth |
      | project://app/a.py    | alpha content    | 0.8   | 10     | 3     |
      | project://app/b.py    | beta content     | 0.6   | 10     | 3     |
    And a semantic chunking budget with max_tokens 1000 and reserved_tokens 0
    When I assemble via the pipeline with strategy "semantic_chunking"
    Then the pipeline payload should contain at least 1 fragment
