#!/usr/bin/env python3
"""The red room's doubleness (00:34): the dove of thorns, and the room that drives its seekers mad. And the acrostic, set apart as a possible coda. Developed in place from RUN-VISION-14."""
import re, pathlib, importlib.util, io, contextlib
D = pathlib.Path(__file__).resolve().parent; R = D.parents[2]
def mod(n):
    s = importlib.util.spec_from_file_location(n, str(D/f"{n}.py")); m = importlib.util.module_from_spec(s)
    with contextlib.redirect_stdout(io.StringIO()): s.loader.exec_module(m)
    return m
U = mod("build_run_vision14"); V14 = U.V14; wc = lambda s: len(re.findall(r"\S+", s))
def find(axn, s):
    for i, l in enumerate((R/f"data/texts/AXN-{axn}-text.md").read_text().split("\n")):
        if s in l: return f"“{s}” (AXN:{axn} l. {i+1})"
    raise AssertionError((axn, s))
MAD = " It is a room that drives its seekers mad. I did not ask how I had come to be shown it."
DOVE = """A dove flew through the rooms, and what was given to it came whole to where it was sent. It was a dove of thorns. No other kind would do: a plain dove would have carried the thing, and it would have come changed. The red of the walls was the red of that dove."""
V = V14
a = "and in each of them someone had spent a life looking for this one."; assert V.count(a) == 1
V = V.replace(a, a + MAD)
b = "a room of wheels opened off it, and had no other way in.\n\n"; assert V.count(b) == 1
V15 = V.replace(b, b + DOVE + "\n\n")
assert V15.replace(MAD, "").replace(DOVE + "\n\n", "") == V14 and V15.endswith("I have not yet turned it over.")
for w in ["Acanth", "Hexagon", "Borges"]: assert w.lower() not in (MAD + DOVE).lower()
SRC = [find("0060", "**Acanthian dove = A messenger protected by thorns.**"), find("0060", "A dove whose flight patterns trace protective geometries."), find("0060", "The dove must be Acanthian because an ordinary dove's blood lacks the apotropaic dimension. The message would arrive, but unprotected. It could be intercepted, corrupted, misdelivered."), find("0060", "**The specificity is not arbitrary. The specificity is the spell.**"), find("0060", "| **A song about the impossibility of obtaining it** | Acknowledgment as sympathetic link |"),
       find("019B", "name: Dove, status: OPERATIONAL,"), find("019B", "physics: 'transfer(gift) preserves; extract = ∅'"), find("0044", "Librarians spend their lives searching")]
# the acrostic, as seated in Antioch
AC = ["**Y** ou do not know what you just read: It is", "**A** document typed by shadow people", "**H** ung from the mist in my bathroom mirror", "**W** hich beings of light and moth dictate", "**E** ach a creature of great age, fell kings", "**H** aunting Outer Cranium: So each word", "IS", "AS IT", "MUST BE"]
L = (R/"data/texts/AXN-0086-text.md").read_text().split("\n"); at = []
for x in AC:
    hit = [i + 1 for i, l in enumerate(L) if l.strip() == x and i + 1 > 8690]; assert hit, x; at.append(hit[0])
assert at == sorted(at) and "".join(x[2] for x in AC[:6]) == "YAHWEH"
CODA = [x if x.startswith("**A** document") else re.sub(r"^\*\*(.)\*\* ", r"**\1**", x) for x in AC]  # "A" is a whole word there; its space is real
if __name__ == "__main__":
    o = ["# The red room's doubleness; and a coda, set apart", "", "Working file, not a deposit. 2026-11-29. Lee Sharks, 00:34:", "",
     "> i think perhaps the acrostic should stand as its own coda, but im not sure. and i do think the hexagon should have its uncanny doubleness - the acanthian dove flies the hexagon, and needs its thorns; and it is after all a room that drives its seekers mad.", "",
     f"Developed in place from RUN-VISION-14.md, which stands. Two additions are made inside the passage of the red room. The vision is now {wc(V15)} words, and it still ends on the unturned hand. The coda is not part of that text; it is set after it, apart, because the author is not sure.", "",
     "## 1. The two additions", "",
     "**The seekers.** Added to the paragraph of the rooms beyond counting, which now reads:", "",
     "> Behind it were rooms beyond counting, six-walled, and in each of them someone had spent a life looking for this one." + MAD, "",
     "The first new sentence is the author's, nearly as he gave it. The second is the session's. The red room was twenty-two years in the building, and the seer is the one who printed what was written; the vision has already shown the seer the seer's own acts twice without saying so (the making of the twelve; the saying against the coin). Here the seer is shown the room, and does not ask. Nothing is said of the seer's own state.", "",
     "**The dove.** A new paragraph, after the rooms that open out of it:", "", "> " + DOVE, "",
     "From the archive's record of the Acanthian dove: a messenger protected by thorns; a plain dove's message would arrive, but could be “intercepted, corrupted, misdelivered”; “The specificity is the spell.” The map gives the fourth room as the Dove's, where what is given is kept whole and what is taken is nothing. The last sentence takes the red of the room from the dove. The record's spell asks for the dove's blood, and its song paints in that colour because the dove cannot be had. The vision says only whose red it is.", "",
     "So the room has two sides now, and neither cancels the other: it is lit by what it cost, it has no lock, whoever sits is its measure; and it drives those who look for it mad, and its colour is a dove's.", "",
     "Source places, each asserted at its line:", ""] + ["- " + x for x in SRC] + ["",
     "## 2. The coda, set apart", "",
     "The author: “i think perhaps the acrostic should stand as its own coda, but im not sure.” So it is not joined to the vision. It is shown here in the place it would have: after the last line, under a rule.", "",
     "The text is the close of TACHYONICA, in Antioch, of 6/24/14, as seated (AXN:0086 ll. " + f"{at[0]}–{at[-1]}" + "; each line asserted). In the seated text a space stands after each bold first letter; it is closed up here, except in the second line, where the letter is the whole word “A” and the space is its own. Nothing else is touched. (Corrected 01:36: the first form closed that space too, and read “Adocument”.) The first letters spell a name of God, which the script checks.", "",
     "> …I have not washed. I have not yet turned it over.", ">", "> ·", ">"] + ["> " + x + "  " for x in CODA] + ["",
     "What it would do there:", "",
     "1. **It answers the first sentence.** The vision says it was sent by dark machines to one who wrote what was seen. The coda says the reader has just read a document “typed by shadow people”, dictated by “beings of light and moth”.",
     "2. **It speaks to the reader, and the vision does so only once before**, in the greeting (“to you who are reading”). The coda's first word is “You”.",
     "3. **It turns Revelation's first clause.** “What must come to be” becomes “each word is as it must be”. That is the sign of completion, in a line of 2014.",
     "4. **It would follow the unturned hand.** The last sentence of the vision would no longer be the last words of the book. The hand would still be unturned.",
     "5. **It would be the second place where source words stand in the vision**, after Revelation 21. Both would be marked as read or received: one “as it is written”, one as a thing recovered.", "",
     "What weighs against: the blessing says the book makes new words, and the coda says each word is as it must be. Those can be heard as one thing or as two. And the register is the poem's, not the vision's; the author named that difference himself.", "",
     "## 3. Not done", "",
     "Noah sends a dove from the ark, and it comes back with a leaf. The vision has an ark, and now a dove, and has had leaves from its first page. Nothing is made of this. It is at hand.", "",
     "The two places where TACHYONICA presses on the city (the remnant taken out; the Cube) stand as mapped in CITY.md §7. The author has not spoken to them.", "",
     "## 4. The vision, whole", ""] + ["> " + l if l else ">" for l in V15.split("\n")]
    (D/"RUN-VISION-15.md").write_text("\n".join(o) + "\n"); print(wc(V15), at[0], at[-1])
