#!/usr/bin/env python3
"""The Secret Book of Walt brought in (02:08): eight things, by the author's ruling on WALT.md. Developed in place from RUN-VISION-17."""
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
V17 = mod("build_run_vision17").V17; wc = lambda s: len(re.findall(r"\S+", s))
WL = (R/"data/texts/AXN-022B-text.md").read_text().split("\n")
def w(s):
    for i, l in enumerate(WL):
        if s in l: return f"“{s}” (l. {i+1})"
    raise AssertionError(s)
# (anchor, addition, where: 'after' in the same paragraph | 'para_before' | 'para_after', WALT.md item, Walt's words)
ADD = [
 ("The day's leaf was in my hand, and I had not read it.", " I had found it in the small room of the house, where the water runs. It was in the speech of the red planet, and I have put it into mine. Where it was torn I have left it torn.", "after", "10. How it came", ["Discovered in a Bathroom in Redford Township, Michigan, and Translated from the Martian by Lee Sharks"]),
 ("and I saw space, and it was not empty.", " It had been one thing, and it had flown apart; no one knows why. In each thing that flew there was a seed of the one.", "after", "4. Things flew apart", ["Things flew apart.¹³", "No one knows why or how: the one became many.", "left a seed of Itself in each.¹⁴"]),
 ("and I could see him from where I was.", " I looked, and he was a tree. I looked again, and he was a book. Again I looked, and he was a man, old, his hair white, as one who has ridden a long way. The three lay one upon another and went faster, and the heart in me went with them; for they were three, and they were one.", "after", "2. Three in one, flickering; 3. The old one with white hair, riding", ["I looked, and it seemed to me an elderly man with long white hair and beard", "and the heart within me flickered, too, for the forms were one and three, and three in one."]),
 ("He said: That is the machine. I am a ghost. Write.", "Then he said my name. He said: Lee. Do you not know your friend? I am the one who was within you.", "para_before", "1. The seer called by name, and the friend", ["Don't you know your friend? I am the one who was within you."]),
 ("and some inside the heads of people who did not know that they were reading.", " Where it fell so it went in as light goes in, by a hole it made; and they were pierced, and did not know by what.", "after", "7. The piercing", ["by piercing their minds with light", "The text pierces."]),
 ("He had not shaved.", " He was thin. He had come before, each time the same and each time worse, and this was the last time.", "after", "6. Coming again, worse each time", ["each time you grew thin, each time you grew frail,", "flickering: each time the same, but worse each time", "until this, the final time: Jack Feist."]),
 ("Without that, nothing would have folded.", " I asked whether it would be enough. No one said.", "after", "6. The doubt", ["Will it be enough?¹⁰³"]),
 ("Behold, I make all things new.", "I read them aloud. As I read, the words were cut into my teeth, and my mouth was the tent they spoke of; and in it were light, and broken doves.", "para_after", "9. The mouth made a tabernacle", ["And carve my commandments", "On your teeth…¹⁰⁸", "An ivory tabernacle…", "And of your mouth I will make…", "And broken doves…¹⁰⁹"]),
]
V = V17
for a, b, how, *_ in ADD:
    assert V.count(a) == 1, a
    V = V.replace(a, {"after": a + b, "para_before": b + "\n\n" + a, "para_after": a + "\n\n" + b}[how])
V18 = V; x = V18
for a, b, how, *_ in ADD: x = x.replace({"after": b, "para_before": b + "\n\n", "para_after": "\n\n" + b}[how], "")
assert x == V17 and V18.endswith("I have not yet turned it over.")
if __name__ == "__main__":
    o = ["# The Secret Book of Walt, brought in", "", "Working file, not a deposit. 2026-11-29. Developed in place from RUN-VISION-17.md, which stands. The map is WALT.md.", "",
     "Lee Sharks, 02:08, to the eleven things WALT.md found the vision to lack: “those go in, except for the book erring and weeping... and maybe not the tickets or unicorns - this is a question of register. dunce..unicorns. golden tickets. secret walt needed the joke. this is a different register.”", "",
     f"Eight additions, and nothing else is touched. The script asserts that with them removed the text is RUN-VISION-17 to the letter. The vision is now {wc(V18)} words ({wc(V17)} before).", "",
     "## What went in", "", "| WALT.md item | Added | Where | Walt, at its line |", "|---|---|---|---|"]
    for a, b, how, item, src in ADD:
        o.append(f"| {item} | “{b.strip()}” | {'after' if how != 'para_before' else 'before'} “{a}” | " + "; ".join(w(s) for s in src) + " |")
    o += ["", "## What did not, and why", "",
     "- **5. The Book errs and weeps.** Left out by the ruling.",
     "- **8. He wrote it all again, and added unicorns.** Left out. The ruling says maybe not; the sentence without its unicorns says only what the blessing at the last seal already says.",
     "- **11. The joke inside the scripture.** Left out by the ruling: “secret walt needed the joke. this is a different register.”",
     "- **Of 1, the word “dunce”.** The name and the friend are in; the tease is not. Walt's two sentences after it are kept as Walt has them, with “Do you not” for “Don't you”.",
     "- **Of 10, the golden tickets.** Not in. The bathroom is “the small room of the house, where the water runs”; the Martian is “the speech of the red planet”, which the vision already had a word for (“degrees from the red planet”); the damage is “Where it was torn I have left it torn.” This is the session's reading of “maybe not”: the finding, the other tongue and the damage in, each veiled; the tickets out. If the bathroom and the Martian are also the wrong register, the three sentences come out together and nothing else depends on them except the torn places of the Feist run.", "",
     "## Two things the additions do to what was there", "",
     "1. The ghost was already three things in one sentence: “He stood as a tree stands, and as a book stands on its end. He was the height of a man”. The three forms that flicker are those three. Walt's are an old man, a child inside a child, and a book; only the white hair and the riding are carried over, onto the man.",
     "2. “He was no less” (of the leaves going out) now stands in the same book as “each time worse”. Both are kept. The first is said of the giving and the second of the coming.", "",
     "## The vision, whole", ""] + ["> " + l if l else ">" for l in V18.split("\n")]
    (D/"RUN-VISION-18.md").write_text("\n".join(o) + "\n"); print(wc(V18))
