# Test Case: Workflow node agent calls a peer agent during structured invocation.
#
# This test validates the fix for the structured invocation handler firing
# too early when the agent makes async peer-agent calls. The sequence is:
#
# 1. Workflow dispatches structured invocation to TestPeerAgentA (node "research")
# 2. TestPeerAgentA calls peer_TestPeerAgentD (its allowed peer) to get additional info
# 3. TestPeerAgentD responds
# 4. TestPeerAgentA receives the peer response and emits the result embed
# 5. Workflow collects the result and completes
#
# Without the fix, step 2 would cause the structured invocation handler to
# immediately check for the result embed (which doesn't exist yet) and
# return an error, preventing step 4 from ever happening.

test_case_id: "workflow_node_peer_delegation_001"
description: "Tests that a workflow node agent can call peer agents during structured invocation without premature finalization."
tags: ["all", "workflows", "delegation"]
skip_intermediate_events: true
expected_completion_timeout_seconds: 30

test_runner_config_overrides:
  agent_config:
    artifact_scope: "namespace"

gateway_input:
  target_agent_name: "SimpleTestWorkflow"
  user_identity: "workflow_peer_test@example.com"
  external_context:
    a2a_session_id: "workflow_peer_test_session"
  parts:
    - type: "data"
      data:
        input_text: "Research and process this data"

llm_interactions:
  # Turn 1: TestPeerAgentA (workflow node "step_1") receives the structured invocation
  # and decides to call a peer agent for help.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "I need additional information. Let me ask TestPeerAgentD for help."
            tool_calls:
              - id: "call_peer_b_for_research"
                type: "function"
                function:
                  name: "peer_TestPeerAgentD"
                  arguments: '{"task_description": "Look up details for: Research and process this data"}'
    expected_request:
      tools_present:
        - "peer_TestPeerAgentD"

  # Turn 2: TestPeerAgentD receives the delegation and responds.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Here are the research results: The data has been analyzed and the key finding is X=42."
    expected_request:
      messages_contain:
        - role: "user"
          content_contains: "Look up details for"

  # Turn 3: TestPeerAgentA receives the peer response and produces the final output
  # with the mandatory result embed for structured invocation.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step1_output.json" mime_type="application/json" description="Research output"
              {"processed": "Research completed", "data": "X=42"}
              »»»
    expected_request:
      # Verify that the peer response was injected back into the conversation
      expected_tool_responses_in_llm_messages:
        - tool_call_id_matches_prior_request_index: 0
          response_contains: "key finding is X=42"

  # Turn 4: After artifact save notification, TestPeerAgentA outputs the result embed
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Research complete with peer input. «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 (workflow node "step_2") processes the output from step 1.
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: |
              «««save_artifact: filename="step2_output.json" mime_type="application/json" description="Final workflow output"
              {"final_result": "Workflow completed with peer-researched data: X=42"}
              »»»
    expected_request:
      tools_present: ["load_artifact"]

  # Turn 6: TestPeerAgentB outputs the result embed after artifact save
  - static_response:
      choices:
        - message:
            role: "assistant"
            content: "Final processing complete. «result:artifact=step2_output.json:0 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"
