>Would it actually be legal to alternate between Andorra and France or Spain?

Yes, on condition that when entering Andorra you have a Schengen Area **exit** stamp and when leaving Andorra have a Schengen Area **entry** stamp.

As far as the Schengen Border Code, is concerned, it is the proper entry/exit stamps that counts (assuming you don't require a visa) for short term stays.

Without the entry/exit stamps you are assumed to have overstayed (Article 12 (1,4)), unless you can prove otherwise (Article 12 (2)).

---
>[Schengen Border Code](https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX%3A02016R0399-20190611#tocId18)   
>**Article 12**   
>**Presumption as regards fulfilment of conditions of duration of stay**   
>(1) If the travel document of a third-country national **does not bear an entry stamp**, the competent national authorities may presume that the holder **does not fulfil, or no longer fulfils**, the conditions of duration of stay applicable within the Member State concerned.   
>(2) The presumption referred to in paragraph 1 may be rebutted where the third-country national provides, by any means, credible evidence, such as transport tickets or proof of his or her presence outside the territory of the Member States, that he or she has respected the conditions relating to the duration of a short stay.    
>...   
>(4) The relevant provisions of paragraphs 1 and 2 shall apply mutatis mutandis **in the absence of an exit stamp**.  

---
**Sources**:
- [How to Get a Residence Permit in Andorra | Andorra Guides](https://andorraguides.com/residency/how-to-get-residence-permit-in-andorra/)
   - [Décret n° 2003-740 du 30 juillet 2003 portant publication de la convention entre la République française, le Royaume d'Espagne et la Principauté d'Andorre relative à la circulation et au séjour en Principauté d'Andorre des ressortissants des Etats tiers, signée à Bruxelles le 4 décembre 2000 (1) - Légifrance](https://www-legifrance-gouv-fr.translate.goog/jorf/id/JORFTEXT000000229407?_x_tr_sl=fr&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=sc)
   >**Section 4**   
   >...   
   >The regime for entry and movement in France and Spain for foreigners holding an **Andorran residence permit** is identical to that which applies to foreigners holding a French or Spanish residence permit.
   >>Google Translate Original text
- [Andorra Schengen Visa Explained | Andorra Guides](https://andorraguides.com/visa/schengen/)