Feature: Plan resume fields round-trip persistence
  As a developer
  I want reversion_count, last_completed_step, and last_checkpoint_id to persist through the database
  So that plan resume and reversion limits work correctly after process restarts

  Background:
    Given a fresh in-memory plan persistence database
    And a prerequisite action "local/persist-action" exists in the database

  Scenario: reversion_count persists through database round-trip
    Given a new lifecycle plan with ID "01HV000000000000000000RP01"
    And the plan has reversion_count 2
    When I persist the plan via the plan repository
    And I retrieve the plan by ID "01HV000000000000000000RP01"
    Then the retrieved plan reversion_count should be 2

  Scenario: last_completed_step persists through database round-trip
    Given a new lifecycle plan with ID "01HV000000000000000000RP02"
    And the plan has last_completed_step 4
    When I persist the plan via the plan repository
    And I retrieve the plan by ID "01HV000000000000000000RP02"
    Then the retrieved plan last_completed_step should be 4

  Scenario: last_checkpoint_id persists through database round-trip
    Given a new lifecycle plan with ID "01HV000000000000000000RP03"
    And the plan has last_checkpoint_id "01HV000000000000000000CP01"
    When I persist the plan via the plan repository
    And I retrieve the plan by ID "01HV000000000000000000RP03"
    Then the retrieved plan last_checkpoint_id should be "01HV000000000000000000CP01"

  Scenario: All three resume fields persist together through database round-trip
    Given a new lifecycle plan with ID "01HV000000000000000000RP04"
    And the plan has reversion_count 1
    And the plan has last_completed_step 3
    And the plan has last_checkpoint_id "01HV000000000000000000CP02"
    When I persist the plan via the plan repository
    And I retrieve the plan by ID "01HV000000000000000000RP04"
    Then the retrieved plan reversion_count should be 1
    And the retrieved plan last_completed_step should be 3
    And the retrieved plan last_checkpoint_id should be "01HV000000000000000000CP02"

  Scenario: Default values persist correctly when fields are not set
    Given a new lifecycle plan with ID "01HV000000000000000000RP05"
    When I persist the plan via the plan repository
    And I retrieve the plan by ID "01HV000000000000000000RP05"
    Then the retrieved plan reversion_count should be 0
    And the retrieved plan last_completed_step should be -1
    And the retrieved plan last_checkpoint_id should be None

  Scenario: Resume fields persist across database reconnection
    Given the plan persistence database is file-based
    And a prerequisite action "local/persist-action" exists in the database
    And a new lifecycle plan with ID "01HV000000000000000000RP06"
    And the plan has reversion_count 3
    And the plan has last_completed_step 5
    And the plan has last_checkpoint_id "01HV000000000000000000CP03"
    When I persist the plan via the plan repository
    And I close and reopen the persistence database
    And I retrieve the plan by ID "01HV000000000000000000RP06"
    Then the retrieved plan reversion_count should be 3
    And the retrieved plan last_completed_step should be 5
    And the retrieved plan last_checkpoint_id should be "01HV000000000000000000CP03"

  Scenario: Updated reversion_count persists after plan update
    Given a new lifecycle plan with ID "01HV000000000000000000RP07"
    And the plan has reversion_count 0
    When I persist the plan via the plan repository
    And I update the plan reversion_count to 2
    And I retrieve the plan by ID "01HV000000000000000000RP07"
    Then the retrieved plan reversion_count should be 2
