Interrupting the Sequence of an Attack
Readied Actions (emphases mine):
First, you decide what perceivable circumstance will trigger your reaction. Then, you choose the action you will take in response to that trigger, or you choose to move up to your speed in response to it. Examples include "If the cultist steps on the trapdoor, I'll pull the lever that opens it," and "If the goblin steps next to me, I move away."
When the trigger occurs, you can either take your reaction right after the trigger finishes or ignore the trigger. Remember that you can take only one reaction per round.
Readied actions occur after their trigger, and what you are asking to do is to move after an attack starts, but before it concludes. To do this, you will need the trigger that indicates the start of the attack to be perceivable (per the quoted rules for readied actions), and also to be distinct from the conclusion of the attack, since your readied action will occur after the trigger finishes. That is, you will need to be able to parse the different parts of an attack at a scale that is apparent to your character, not just from a rules standpoint.
The parts of an attack (emphases mine):
- Choose a target. Pick a target within your attack's range: a creature, an object, or a location.
- Determine modifiers. The DM determines whether the target has cover and whether you have advantage or disadvantage against the target. In addition, spells, special abilities, and other effects can apply penalties or bonuses to your attack roll.
- Resolve the attack. You make the attack roll. On a hit, you roll damage, unless the particular attack has rules that specify otherwise. Some attacks cause special effects in addition to or instead of damage.
Let's assume that Step 2, Determine Modifiers, is completely 'meta' and not something your character could actually perceive or react to in-game. However, Steps 1 and 3 are distinct (and separated by Part 2). Thus, if by "begins the attack" you mean, [the attacker chooses me as a target] and if by "the attack completes" you mean, [the attacker resolves the attack by making an attack roll and damage roll], then potentially you have something to react to. In theory, it might be possible to react to being selected as the target of an attack, and then taking your reaction after the target selection finishes but before the attack roll begins because your character can perceive themselves as being the target of an impending attack.
Most answers to this question say that this is not actually possible1, that you cannot parse an attack so finely, and that 'right after the trigger finishes' means after the entire attack finishes. In the face of this skepticism, then, it is useful to cite a few examples of abilities that do precisely that.
The Goblin Boss has:
Redirect Attack. When a creature the goblin can see targets it with an attack, the goblin chooses another goblin within 5 feet of it. The two goblins swap places, and the chosen goblin becomes the target instead.
The Light Domain Cleric (PHB61) has:
WARDING FLARE Also at 1st level, you can interpose divine light between yourself and an attacking enemy. When you are attacked by a creature within 30 feet of you that you can see, you can use your reaction to impose disadvantage on the attack roll, causing light to flare before the attacker before it hits or misses.
Warding Flare was the only ability I found in the PHB2 that allows you to use your reaction in response to being targeted, but I would not be surprised if a non-PHB (but still official) class or race had such an ability.
There are thus at least two official examples of abilities which use reactions in response to being targeted, and which resolve before a hit roll is made and before the attack concludes. Note that both of the examples are limited to being used only when the user can see the attacker, which supports the idea that what they are responding to is an in-game recognition that a character is the target of an attack before the attack itself is made (rather than a meta-level decision of the player). Thus, there is mechanical support for the idea that your Character A is capable of reacting to being targeted.
While these two abilities allow characters to introduce reactions after one step of the attack sequence that are resolved before another step, many other abilities allow characters to parse the attack sequence even more finely. For example, consider the School of Enchantment Wizard's Instinctive Charm:
When a creature you can see within 30' of you makes an attack roll against you, you can use your reaction to divert the attack, provided that another creature is within the attack's range. The attacker must make a Wisdom saving throw against your wizard spell save DC of 17. On a failed save, the attacker must target the creature that is closest to it, not including you or itself. If multiple creatures are closest, the attacker chooses which one to target. On a successful save, you can't use this feature on the attacker again until you finish a long rest.
You must choose to use this feature before knowing whether the attack hits or misses. Creatures that can't be charmed are immune to this effect.
In the case of Instinctive Charm, we see that the attack has proceeded through Step 1 (Choose a target), Step 2 (Determine modifiers), and has begun Step 3 (Resolve the attack) to the point of making the attack roll. It is only at that point, when an attack roll against the wizard is made (but before the resultant number is known), that the PC uses their reaction to force a save, and on a failed save makes the attacker return to Step 1 (Choose a target). Thus, it is actually possible for a character to react at an even finer scale trigger than simply being the target of an attack; Enchanters can react to having an attack roll made against them and can take their reaction before that attack roll is determined to have hit or not.
Similarly, consider the College of Lore Bard's feature Cutting Words:
When a creature that you can see within 60 feet of you makes an Attack roll, an ability check, or a damage roll, you can use your Reaction to expend one of your uses of Bardic Inspiration, rolling a Bardic Inspiration die and subtracting the number rolled from the creature’s roll. You can choose to use this feature after the creature makes its roll, but before the GM determines whether the Attack roll or ability check succeeds or fails, or before the creature deals its damage. The creature is immune if it can’t hear you or if it’s immune to being Charmed.
Like Instinctive Charm, the use of this ability reacts to an attack roll being made, and thus interrupts the attack sequence within Step 3 (and not between steps). However, the interruption here is slightly later in the sequence, after the roll has 'registered' and its value is known but before it is declared a success or failure.
An even later interruption is produced by a Mark of the Sentinel, where:
Vigilant Guardian. When a creature you can see within 5 feet of you is hit by an attack roll, you can use your reaction to swap places with that creature, and you are hit by the attack instead. Once you use this trait, you can’t do so again until you finish a long rest.
Here the ability interrupts step 3 after the attack has been declared a hit but before damage is done.
All five of the abilities mentioned are powered by reactions, and each one requires the user to be able to see the attack, suggesting that it is based on what the in-game character perceives.
It is abundantly clear that the rules permit such fine parsing of triggers as you wish to do. Many features grant this ability. That is not to say that what you want to do, moving as a readied action in response to an attack, is permitted. We know that in the listed features the mechanical requirements for a readied action have been met - a perceivable circumstance and a level of parsing that allows you to act after an attack is declared but before it resolves. But this does not assure us that a generic readied action may be used to this end - it could be that doing this requires a feature (like the five mentioned) that specifically grants you the ability to interrupt an attack. Unfortunately, both examples given for generic readied actions rely on responding to movement ('the cultist steps', 'the goblin steps'), and not responding to attacks, spells, etc. Since these are examples, not a complete list of possibilities, we simply do not know whether a generic readied action may be used to do what you want - move in response to being targeted by a spell attack.
Since the rules are silent on this, it is up to the DM to decide whether to allow a readied action to move away from an attack before the attack is completed. But at least we can say that allowing this does not contradict RAW, because there are multiple examples of this being permitted under RAW in certain circumstances.
For your second question, you ask what happens when the target is out of range by the time the attack action completes. The ranged attack rules (emphases mine) say:
When you make a ranged attack, you fire a bow or a crossbow, hurl a handaxe, or otherwise send projectiles to strike a foe at a distance. A monster might shoot spines from its tail. Many spells also involve making a ranged attack.
Range. You can make ranged attacks only against targets within a specified range.
If a ranged attack, such as one made with a spell, has a single range, you can't attack a target beyond this range.
Some ranged attacks, such as those made with a longbow or a shortbow, have two ranges. The smaller number is the normal range, and the larger number is the long range. Your attack roll has disadvantage when your target is beyond normal range, and you can't attack a target beyond the long range.
In this case, the archer or caster selects you as the target of the spell - but has not yet made the attack roll. If you are permitted to react to targeting, following the considerations as above, you can then move out of range before the attack is made. In this case the attack simply fails; it cannot be made, whether it is a weapon or spell, because you have made yourself an invalid target.
My perspective as a DM
In my experience as a DM, readied actions are a risky gamble. As often as not they end up being wasted when their trigger doesn't materialize. What if the attacker in question ends up attacking another member of your party? What if they attack you and you move away, but their attack has enough range to still reach you? Either of these outcomes would mean your spent both your action and reaction and got no benefit, which is a poor return on investment in the action economy. If allowing the readied action you suggest helps a player feel more agency but results in little overall benefit to their character, that's an easy yes at my table.
1 Cf.: "Attacks resolve instantaneously, so there is no such thing as 'before the attack completes'." "The rules describe attacks as simultaneously enacted and resolved"
2 The Mounted Combatant feat also allows you to change the target of an attack after Step 1 (targeting) but before Step 3 (attack roll). "You can force an attack targeted at your mount to target you instead." However, unlike every other example above, this neither requires your reaction nor requires you to see the attack. To me, that feels more like a meta ability the player is choosing to use, rather than an in-game decision the character is making.