# Test Case: Structured invocation retries when the agent forgets the result embed.
#
# This test validates the SI retry mechanism for missing result embeds:
#
# 1. Workflow dispatches structured invocation to TestPeerAgentA (node "validate_order")
# 2. TestPeerAgentA saves an artifact but does NOT output the result embed
# 3. Handler detects the missing embed and sends retry feedback
# 4. TestPeerAgentA corrects its output and includes the result embed
# 5. Workflow proceeds to node "process_order" and completes

test_case_id: "workflow_si_retry_missing_embed_001"
description: "Tests that structured invocation retries when agent omits the mandatory result embed."
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_embed_test@example.com"
  external_context:
    a2a_session_id: "retry_embed_test_session"
  parts:
    - type: "data"
      data:
        customer_name: "Jane Smith"
        order_id: "ORD-99999"
        amount: 200

llm_interactions:
  # Turn 1: TestPeerAgentA saves artifact correctly...
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step1_output.json" mime_type="application/json" description="Validated order"
              {"customer_name": "Jane Smith", "order_id": "ORD-99999", "amount": 200, "status": "validated"}
              »»»
    expected_request:
      tools_present: ["load_artifact"]

  # Turn 2: ...but forgets the result embed after artifact save notification.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "I have validated and saved the order data."
    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 feedback about the missing result embed.
  # Agent now saves the artifact again and includes the result embed.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step1_output.json" mime_type="application/json" description="Validated order"
              {"customer_name": "Jane Smith", "order_id": "ORD-99999", "amount": 200, "status": "validated"}
              »»»
    expected_request:
      messages_contain:
        - role: "user"
          content_contains: "You failed to provide the mandatory result embed"

  # Turn 4: Agent includes the result embed after artifact save notification.
  - 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 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": "Jane Smith", "order_id": "ORD-99999", "amount": 200, "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"
