@phase1 @domain @repository @skill_registry @m4_persist
Feature: Skill Registry Flattened Tool Persistence
  As a system operator managing skills
  I want flattened tool sets persisted alongside skill definitions
  So that the execution layer can skip redundant resolution and detect stale caches

  Background:
    Given a clean in-memory database with the skill persistence schema
    And a skill persistence repository

  # ---------------------------------------------------------------------------
  # Persisting flattened tools
  # ---------------------------------------------------------------------------

  @skill_persist_create
  Scenario: Skill creation persists flattened tool set
    Given a skill "local/code-tools" with description "Code helper tools"
    When the skill is created via the persistence repository
    And the flattened tools are updated with tools json '["tool-a","tool-b"]' and hash "abc123"
    Then the stored flattened tools json should be '["tool-a","tool-b"]'
    And the stored flattening hash should be "abc123"

  # ---------------------------------------------------------------------------
  # Update invalidation
  # ---------------------------------------------------------------------------

  @skill_persist_invalidate
  Scenario: Skill update invalidates cached capability summary
    Given a skill "local/editor" with description "Editor tools"
    And the skill is created via the persistence repository
    And the flattened tools are updated with tools json '["edit"]' and hash "hash1"
    When the skill "local/editor" is updated with description "Updated editor"
    Then the stored flattened tools json for "local/editor" should be null
    And the stored capability summary json for "local/editor" should be null
    And the stored flattening hash for "local/editor" should be null

  # ---------------------------------------------------------------------------
  # Hash-based staleness detection
  # ---------------------------------------------------------------------------

  @skill_persist_hash
  Scenario: Flattening hash detects stale cached data
    Given a skill "local/lint" with description "Lint tools"
    And the skill is created via the persistence repository
    And the flattened tools are updated with tools json '["lint"]' and hash "hash_v1"
    Then the skill "local/lint" should not need refresh for hash "hash_v1"
    And the skill "local/lint" should need refresh for hash "hash_v2"

  # ---------------------------------------------------------------------------
  # Refresh recomputation
  # ---------------------------------------------------------------------------

  @skill_persist_refresh
  Scenario: Skill refresh recomputes flattened tools
    Given a skill "local/refresh-me" with description "Refreshable"
    And the skill is created via the persistence repository
    When the flattening hash for "local/refresh-me" is recomputed from yaml "name: local/refresh-me"
    Then the stored flattening hash for "local/refresh-me" should not be null
    And the skill "local/refresh-me" should not need refresh for the recomputed hash

  # ---------------------------------------------------------------------------
  # Uniqueness constraint
  # ---------------------------------------------------------------------------

  @skill_persist_unique
  Scenario: Uniqueness constraint prevents duplicate skill names
    Given a skill "local/unique-test" with description "First"
    And the skill is created via the persistence repository
    When a second skill "local/unique-test" with description "Second" is created
    Then the second creation should raise a duplicate error

  # ---------------------------------------------------------------------------
  # Namespace index
  # ---------------------------------------------------------------------------

  @skill_persist_namespace
  Scenario: Namespace index supports filtered listing
    Given a skill "local/tool-a" with description "Tool A"
    And the skill is created via the persistence repository
    And a skill "devops/deploy" with description "Deploy"
    And the skill is created via the persistence repository
    When skills are listed with namespace "local"
    Then the listed skills should contain "local/tool-a"
    And the listed skills should not contain "devops/deploy"
