Short answer for this two parter, it is from a fictional tale of brother upon brother hatred defeated by the quick wit of the poet Cao Zhi. It is not a poem Cao Zhi wrote so a pity Elon Musk didn't tweet one of his genuine ones.
Cao Zhi
The story of the poem is meant to reflect a particular situation in time and then fictionalize it. So I want to first talk about the life of the most famous poet of the 3kingdoms era.
Cao Zhi was born in 192, two years into a civil war, the fourth son of Cao Cao (of over twenty sons and a few stepsons) of a minor warlord who would rise to be the strongest power in his day. Zhi was the third son of Lady Bian, an entertainer and a figure of restraint on her not always calm family, who had four sons. Both parents loved Cao Zhi dearly.
Cao Zhi grew up, when not travelling with the army, well educated and notably bright by the age of just over ten sui (elven in western terms), able to recite treaties and poems, said to be a skilled writer to point Cao Cao asked if someone else was writing on his behalf. He would spend time in Ye which became Cao Cao's stronghold with the Han court in Xuchang. There Cao Cao, a scholar and a poet in his own right, sought to build prestige via the building up of the old city and via scholarship. There were literary circles and that included two of his sons, Cao Pi (eldest son of Bian as first son of Cao Cao, via Lady Lu, Cao Ang got killed via lust of father) and Cao Zhi.
There were drinking parties, scholarly discussions, the performance art, music and the poetry challenges of creating one to a theme of the day. The Cao family used their intellectual qualities and that of their court to strengthen their authority though it would also play a part in their overthrow to the Simas. Cao Zhi thrived in this environment, he was able to impress scholars of his day while bringing lustre to the warlord court with poems and rhapsodies of his own.
However, there would be a problem that would help drive a wedge between Zhi and his elder brother. Who would be Cao Cao's heir? The heir problem was one a lot of warlords struggled with, helping bring down regimes like the Yuan clan, splitting families, requiring some brutal steps by warlords to secure the succession to ensure a smooth future (and not always very successfully). With the early death of prodigy Cao Chong (half-brother by Lady Huan), it came down to two real candidates: Cao Pi and Cao Zhi. Both were part of the literary circles and even shared excursions and banquets, both were very able scholars and poets, each given a household of leading scholars to surround them, both were given chances to look after Ye when Cao Cao was on campaign.
Cao Pi, born in 187, had the advantage of being the eldest which helped others push his candidature as the natural choice. While Cao Pi lacked the poetic brilliance of his younger sibling, he was able to portray himself as the sensible, restrained figure of the two, the planner who could keep himself under control, contrasting with his brother.
Cao Zhi on the other hand, perhaps due to his youth and the opportunities of wealth, hampered his chances. He got drunk often, his lack of restraint did not please all in his household, he was impulsive. He did have the problem of being the younger candidate with many in the court, including Zhi's father-in-law Cui Yan, warning against disrupting the natural order (and remembering the fate of other regimes) but his own behaviour did not help convince people. Cao Zhi really lost favour, possibly after losing the battle for succession, when he raced through the imperial highway in Ye (which Cao Cao had only just been awarded permission to use) and got the major of the gate to let him in without properly dismounting. The major was executed, Cao Cao was livid with Cao Zhi.
The accusations between the two camps and the contest for succession did not seem to help brotherly bonds while Cao Zhi would pay a heavy price over time. Cao Pi would be made heir in 217 but Zhi's situation got worse with Cao Cao keeping a firm eye on Cao Zhi after the gate incident.
His wife Lady Cui would be forced to commit suicide for embroidered clothing that disobeyed the sumptuary laws in 218. His close adviser Yang Xiu would be executed in 219 partly for revealing council secrets to Cao Zhi to help his friend and to ensure he could not help Zhi in future. That year Cao Zhi would get one final opportunity, he was to be sent against the Shu-Han general Guan Yu who was attacking Cao positions in Jing but Cao Zhi was too drunk to accept the order.
Cao Cao died in 220 and his eldest son moved quickly, taking the father's rank of King and moving his brothers away to their fiefs outside the capital with Zhi going to Linzi. Warrior brother Cao Zhang's questioning in the aftermath of the death about the seal of their father, linked with support for Cao Zhi, was perhaps not overly reassuring. Soon Cao Zhi's old friends the Ding brothers were executed and problems emerged.
The seven steps
Since we are nearing panto season, feel free to boo Cao Pi's name during this section for practice.
Around this time there are tales of Cao Pi and Cao Zhi... not getting on so well well. In 221 (with Cao Pi now Emperor) one of the Linzi officials, Guan Jun seems to have understood what was hoped for by the new Emperor. He accused Cao Zhi of getting drunk, behaving rudely towards the throne and even threatening to lay hands on Cao Pi's envoy. There were calls for punishment and Zhi's life may well have been under threat but the Dowager Bian intervened, not for the only time, and Cao Zhi survived. However, his rank as a marquis was demoted to Anxiang and the lowest of the brothers and he lost a lot of income. There would be a second round of accusations from some officials that Cao Zhi mentions in "Memorial Presenting the Poems ‘Blaming Myself ’ and ‘Responding to an Edict" in 225 CE but from which little detail is known.
The seven-step poem is a moment when Cao Zhi is summoned to Cao Pi and facing severe punishment. Possibly meant to be in one of the two situations above with the 221 event is more famous, use of titles figures in the texts hadn't got yet don't help to place the tales times. The only way for Zhi to save himself is to come up with a poem in the time it would take for seven steps (translation by Robert Joe Cutter)
Boil beans by burning beanstalks, Strain fermented beans to make a juice. Beanstalks burn beneath the pot, Beans in the pot weep, We are born from the same root, Why such hurry us to fry?
Cao Zhi thus brilliantly completes the task and foils Cao Pi's scheme with the Emperor rather embarrassed as this rebuke over the treatment of his sibling.
The novel Romance of the Three Kingdoms would include the episode in chapter 79 with the opening segment covering a lot of Cao Pi brutality, fictional and otherwise, on becoming King. In this case where Cao Zhi doesn't attend the funeral of his father, is very drunk while Ding family insult envoys and then has them beaten. Cao Pi plans to kill Zhi but since their mother is being annoyingly against it, agrees on a test of literary ability as an excuse. Only Cao Zhi manages two poems, moving everyone and Bian uses it to rebuke Pi.
So what source do we get this poem from? None of the primary sources of the three kingdoms nor Cao Zhi himself. The tale and the poem itself first appears in the 5th-century work A New Account of Tales of the World by Liu Yiqing, a collection of tales of wit and court affairs. While it is useful for history and perceptions people had, the prime concern was not accuracy in these tales.
It could also be described as a somewhat hostile source. Also included in these tales, accusations of Cao Pi taking the concubines of his father and his mother refusing to speak to him after finding out on Pi's deathbed, she refused to come to the funeral. Or Cao Pi poisoning his warrior brother Zhang in the chambers of their mother, all tales that first turn up in this collection.
Who doesn't like a bit of family infighting and scandal? Cao Zhi was a tragic figure, a great writer with emotional poems people loved to read whose talents would be denied and who died in a depressed state, he was a figure who appealed to many in the literary class. Cao Pi's literary talents were overshadowed by his brother and his treatment of his siblings do not seem to have gone down well, he overthrew the Han dynasty and he gained a reputation for being a bit cruel.
Unfortunately, the attribution to Cao Zhi, or the Goddess of the Luo River being thought of as Zhi writing about Pi's first chief wife Lady Zhen (who was forced to commit suicide by her husband when she lost favour), is one of those ideas that never seems to die. Furthering a damaging image of Cao Pi and the romanticism of Cao Zhi who gets to shame his brother and get attached to the tragic figure of Lady Zhen (and in one tale, sleeps with her ghost).