# Test Case: Structured invocation retries when the artifact fails schema validation.
#
# This test validates the SI retry mechanism for schema validation failures:
#
# 1. Workflow dispatches structured invocation to TestPeerAgentA (node "validate_order")
#    with output_schema_override requiring: customer_name, order_id, amount, status
# 2. TestPeerAgentA saves an artifact missing the required "status" field
# 3. Handler detects the schema validation failure and sends retry feedback
# 4. TestPeerAgentA corrects its output with all required fields
# 5. Workflow proceeds to node "process_order" and completes

test_case_id: "workflow_si_retry_schema_001"
description: "Tests that structured invocation retries when artifact fails output schema validation."
tags: ["all", "workflows", "validation"]
skip_intermediate_events: true
expected_completion_timeout_seconds: 30

test_runner_config_overrides:
  agent_config:
    artifact_scope: "namespace"

gateway_input:
  target_agent_name: "StructuredTestWorkflow"
  user_identity: "retry_schema_test@example.com"
  external_context:
    a2a_session_id: "retry_schema_test_session"
  parts:
    - type: "data"
      data:
        customer_name: "Bob Johnson"
        order_id: "ORD-55555"
        amount: 350

llm_interactions:
  # Turn 1: TestPeerAgentA saves artifact MISSING the required "status" field.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step1_output.json" mime_type="application/json" description="Validated order"
              {"customer_name": "Bob Johnson", "order_id": "ORD-55555", "amount": 350}
              »»»
    expected_request:
      tools_present: ["load_artifact"]

  # Turn 2: Agent outputs result embed (artifact passes embed check but will fail schema).
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Order validated. «result:artifact=step1_output.json:v0 status=success»"
    expected_request:
      expected_tool_responses_in_llm_messages:
        - tool_name: "_notify_artifact_save"
          response_json_matches:
            filename: "step1_output.json"
            status: "success"

  # Turn 3: RETRY — Handler sends schema validation feedback.
  # Agent now saves a corrected artifact with the "status" field included.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step1_output.json" mime_type="application/json" description="Validated order"
              {"customer_name": "Bob Johnson", "order_id": "ORD-55555", "amount": 350, "status": "validated"}
              »»»
    expected_request:
      messages_contain:
        - role: "user"
          content_contains: "failed schema validation"

  # Turn 4: Agent outputs result embed after corrected artifact save.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Order validated with all fields. «result:artifact=step1_output.json:v0 status=success»"
    expected_request:
      expected_tool_responses_in_llm_messages:
        - tool_name: "_notify_artifact_save"
          response_json_matches:
            filename: "step1_output.json"
            status: "success"

  # Turn 5: TestPeerAgentB (node "process_order") processes step 1 output.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step2_output.json" mime_type="application/json" description="Processed order"
              {"customer_name": "Bob Johnson", "order_id": "ORD-55555", "amount": 350, "status": "validated", "processed": true}
              »»»
    expected_request:
      tools_present: ["load_artifact"]

  # Turn 6: TestPeerAgentB outputs the result embed after artifact save.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Order processed. «result:artifact=step2_output.json:v0 status=success»"
    expected_request:
      expected_tool_responses_in_llm_messages:
        - tool_name: "_notify_artifact_save"
          response_json_matches:
            filename: "step2_output.json"
            status: "success"

expected_gateway_output:
  - type: "final_response"
    task_state: "completed"
