@checkpoint @auto_triggers
Feature: Automatic checkpoint triggers during Execute phase
  As a plan executor
  I want checkpoints to be created automatically at key points during execution
  So that fine-grained rollback is possible without manual intervention

  # ───────────────────────────────────────────────────────────
  # before_tool_execute trigger
  # ───────────────────────────────────────────────────────────

  Scenario: before_tool_execute checkpoint created before a write tool runs
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA1"
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA1"
    Then a checkpoint with phase "before_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA1"

  Scenario: before_tool_execute checkpoint NOT created for read-only tools
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA2"
    And a read-only tool "local/read-file" is registered
    When I execute tool "local/read-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA2"
    Then no checkpoint with phase "before_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA2"

  Scenario: before_tool_execute trigger can be disabled
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA3"
    And the "before_tool_execute" trigger is disabled
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA3"
    Then no checkpoint with phase "before_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA3"

  # ───────────────────────────────────────────────────────────
  # after_tool_execute trigger
  # ───────────────────────────────────────────────────────────

  Scenario: after_tool_execute checkpoint created after a write tool succeeds
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA4"
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA4"
    Then a checkpoint with phase "after_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA4"

  Scenario: after_tool_execute checkpoint NOT created for read-only tools
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA5"
    And a read-only tool "local/read-file" is registered
    When I execute tool "local/read-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA5"
    Then no checkpoint with phase "after_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA5"

  Scenario: after_tool_execute trigger can be disabled
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA6"
    And the "after_tool_execute" trigger is disabled
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA6"
    Then no checkpoint with phase "after_tool_execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA6"

  Scenario: Both before and after checkpoints created for a single write tool execution
    Given a tool runner with a checkpoint service and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA7"
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA7"
    Then 2 checkpoints should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA7"

  # ───────────────────────────────────────────────────────────
  # on_subplan_spawn trigger
  # ───────────────────────────────────────────────────────────

  @tdd_issue @tdd_issue_4236 @tdd_expected_fail
  Scenario: on_subplan_spawn checkpoint created when a child plan is spawned
    Given a subplan execution service with a checkpoint service and parent_plan_id "01ARZ3NDEKTSV4RRFFQ69G5FA8"
    And a subplan "01ARZ3NDEKTSV4RRFFQ69G5FAA" is configured
    When the subplan execution service executes the subplan
    Then a checkpoint with phase "execute" should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FA8"
    And the checkpoint source_tool should be "subplan_execution_service"

  Scenario: on_subplan_spawn trigger can be disabled
    Given a subplan execution service with a checkpoint service and parent_plan_id "01ARZ3NDEKTSV4RRFFQ69G5FAB"
    And the "on_subplan_spawn" trigger is disabled on the subplan service
    And a subplan "01ARZ3NDEKTSV4RRFFQ69G5FAC" is configured
    When the subplan execution service executes the subplan
    Then no checkpoints should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FAB"

  Scenario: on_subplan_spawn checkpoint created for each spawned subplan
    Given a subplan execution service with a checkpoint service and parent_plan_id "01ARZ3NDEKTSV4RRFFQ69G5FAD"
    And 3 subplans are configured
    When the subplan execution service executes all subplans
    Then 3 checkpoints should exist for plan "01ARZ3NDEKTSV4RRFFQ69G5FAD"

  # ───────────────────────────────────────────────────────────
  # on_error trigger
  # ───────────────────────────────────────────────────────────

  Scenario: on_error checkpoint created when Execute phase fails (stub mode)
    Given a plan executor with a checkpoint manager and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FAE"
    And the execute actor raises an error
    When I run the execute phase for plan "01ARZ3NDEKTSV4RRFFQ69G5FAE"
    Then the execute phase should fail
    And a checkpoint with phase "on_error" should have been created for plan "01ARZ3NDEKTSV4RRFFQ69G5FAE"

  Scenario: on_error trigger can be disabled
    Given a plan executor with a checkpoint manager and plan_id "01ARZ3NDEKTSV4RRFFQ69G5FAF"
    And the "on_error" trigger is disabled on the executor
    And the execute actor raises an error
    When I run the execute phase for plan "01ARZ3NDEKTSV4RRFFQ69G5FAF"
    Then the execute phase should fail
    And no on_error checkpoint should have been created for plan "01ARZ3NDEKTSV4RRFFQ69G5FAF"

  # ───────────────────────────────────────────────────────────
  # No checkpoint service — all hooks silently skipped
  # ───────────────────────────────────────────────────────────

  Scenario: No checkpoints created when no checkpoint service is wired to tool runner
    Given a tool runner WITHOUT a checkpoint service
    And a write tool "local/write-file" is registered
    When I execute tool "local/write-file" without a plan_id
    Then no auto-trigger exception should be raised

  Scenario: No checkpoints created when no checkpoint service is wired to subplan service
    Given a subplan execution service WITHOUT a checkpoint service
    And a subplan "01ARZ3NDEKTSV4RRFFQ69G5FAG" is configured
    When the subplan execution service executes the subplan
    Then no auto-trigger exception should be raised

  # ───────────────────────────────────────────────────────────
  # core.checkpoints.auto_create_on config key
  # ───────────────────────────────────────────────────────────

  Scenario: auto_create_on config key is registered with correct default
    Given the config service is initialized
    When I get the config key "core.checkpoints.auto_create_on"
    Then the value should contain "before_tool_execute"
    And the value should contain "after_tool_execute"
    And the value should contain "on_subplan_spawn"
    And the value should contain "on_error"
