Feature: Coverage boost for domain models and services
  As a developer
  I want comprehensive tests for domain model default factories and validators
  So that overall test coverage meets the 97% threshold

  # ---- PlanSettings default factories (5 lines: 17, 21, 25, 29, 33) ----

  Scenario: PlanSettings with minimal fields uses default factories
    Given I import PlanSettings from domain models
    When I create a PlanSettings with only modelPackName
    Then the plan settings should have empty default lists

  # ---- PlanFiles default factories (3 lines: 16, 20, 24) ----

  Scenario: PlanFiles models use default factories
    Given I import planfiles domain models
    When I create CurrentPlanFiles with minimal fields
    Then the plan files should have empty default lists

  # ---- AiModels custom default factories (5 lines: 14, 18, 22, 26, 30) ----

  Scenario: AiModels custom models use default factories
    Given I import aimodels_custom domain models
    When I create ClientModelsInput with minimal fields
    Then the client models input should have empty default lists

  # ---- Conversation default factories (3 lines: 16, 20, 24) ----

  Scenario: Conversation models use default factories
    Given I import conversation domain models
    When I create a ConvoMessage with minimal fields
    Then the convo message should have default flags

  # ---- Actor name validation error paths (3 lines: 53, 59, 61) ----

  Scenario: Actor name without slash raises ValueError
    Given I import Actor from domain models
    When I try to create an Actor with name "noslash"
    Then a ValueError should be raised with message containing "/"

  Scenario: Actor name with empty prefix raises ValueError
    Given I import Actor from domain models
    When I try to create an Actor with name "/suffix"
    Then a ValueError should be raised with message containing "prefix"

  Scenario: Actor name local with empty id raises ValueError
    Given I import Actor from domain models
    When I try to create an Actor with name "local/ "
    Then a ValueError should be raised with message containing "local"

  # ---- ChangeSet stats for MOVE and applied (3 lines: 91-92, 95) ----

  Scenario: ChangeSet statistics count MOVE and applied changes
    Given I import ChangeSet from domain models
    When I create a ChangeSet with MOVE operations and applied changes
    Then the stats should include moves and applied counts

  # ---- Context model default factories and validator (lines 16, 20, 45, 120-124) ----

  Scenario: Context model uses default factories
    Given I import context domain models
    When I create a Context with minimal fields
    Then the context should have empty default collections

  Scenario: ContextUpdateResult validator rejects negative token diffs
    Given I import context domain models
    When I try to create ContextUpdateResult with negative token diffs
    Then a ValueError should be raised with message containing "non-negative"

  Scenario: ContextFile validates path resolution
    Given I import context domain models
    When I create a ContextFile with a relative path
    Then the path should be resolved to absolute

  # ---- Resource CLI additional coverage for remaining lines ----

  Scenario: type add with update flag re-raises non-already-exists ValidationError
    Given a fresh in-memory resource registry
    And a service whose register_type raises ValidationError with custom message
    When I run type_add with update and the service error
    Then the resource command should fail

  Scenario: resource add with branch but no path
    Given a fresh in-memory resource registry
    And built-in types are bootstrapped
    When I run resource add with branch only type "git-checkout" name "local/br-only" branch "develop"
    Then the resource output should contain "local/br-only"

  Scenario: resource show format yaml
    Given a fresh in-memory resource registry
    And built-in types are bootstrapped
    And I run resource add "git-checkout" "local/yaml-show" with path "/tmp/ys"
    When I run resource show "local/yaml-show" using format "yaml"
    Then the resource output should contain "local/yaml-show"

  Scenario: resource show format plain
    Given a fresh in-memory resource registry
    And built-in types are bootstrapped
    And I run resource add "git-checkout" "local/plain-show" with path "/tmp/ps"
    When I run resource show "local/plain-show" using format "plain"
    Then the resource output should contain "local/plain-show"

  Scenario: resource type add raises generic CleverAgentsError via update
    Given a fresh in-memory resource registry
    And a valid custom resource type YAML file
    And a service whose register_type raises CleverAgentsError
    When I run type_add with the stored config and update false
    Then the resource command should fail
    And the resource output should contain "Error"

  # ---- Action domain model coverage (lines 260-261, 621, 628, 630, 632, 652, 654, 659) ----

  Scenario: Action to_dict includes all optional fields
    Given I import Action from domain models
    When I create an Action with all optional fields populated
    Then the to_dict result should include all optional keys

  Scenario: ActionArgument coerce to float raises ValueError
    Given I import ActionArgument from domain models
    When I try to coerce "not_a_float" to float for an argument
    Then a ValueError should be raised with message containing "float"

  # ---- NamespacedProject validation (line 397) ----

  Scenario: NamespacedProject rejects invalid namespace
    Given I import NamespacedProject from domain models
    When I try to create a NamespacedProject with invalid namespace "123bad"
    Then a ValueError should be raised with message containing "invalid"

  # ---- NamespacedProject linked resource lookup (lines 514, 530, 532-534) ----

  Scenario: NamespacedProject linked resource lookup by id
    Given I import NamespacedProject from domain models
    When I create a NamespacedProject with linked resources
    And I look up a linked resource by id "01HXYZ1234567890ABCDEFGHKJ"
    Then the linked resource should be found

  Scenario: NamespacedProject linked resource lookup by empty id
    Given I import NamespacedProject from domain models
    When I try to look up a linked resource by empty id
    Then a ValueError should be raised with message containing "empty"

  Scenario: NamespacedProject linked resource lookup by alias
    Given I import NamespacedProject from domain models
    When I create a NamespacedProject with linked resources
    And I look up a linked resource by alias "my-alias"
    Then the linked resource should be found

  Scenario: NamespacedProject linked resource lookup by empty alias
    Given I import NamespacedProject from domain models
    When I try to look up a linked resource by empty alias
    Then a ValueError should be raised with message containing "empty"

  Scenario: NamespacedProject linked resource not found by alias
    Given I import NamespacedProject from domain models
    When I create a NamespacedProject with linked resources
    And I look up a linked resource by alias "nonexistent"
    Then the linked resource should not be found

  # ---- Additional database model repr coverage ----

  Scenario: Database model repr methods
    Given I import database models
    When I create database model instances
    Then their repr methods should return strings

  # ---- Plan lifecycle service edge cases ----

  Scenario: Plan lifecycle service uncovered branches
    Given I import plan lifecycle service helpers
    When I test plan lifecycle service edge paths
    Then no errors should occur
