#!/usr/bin/env python3
"""Walt drawn back (02:27): five of the eight additions of RUN-VISION-18 are taken out and one clause of a sixth. Developed in place from RUN-VISION-18, which stands."""
import re, pathlib, hashlib, importlib.util, io, contextlib
D = pathlib.Path(__file__).resolve().parent
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
m18 = mod("build_run_vision18"); V18, V17, ADD = m18.V18, m18.V17, m18.ADD; wc = lambda s: len(re.findall(r"\S+", s))
OUT = {"10. How it came": "“the bathroom does not belong.” The three sentences stood together and go together: the finding, the other tongue, the torn leaf.",
       "4. Things flew apart": "“nor the one and many flying apart - thats a cosmology. this is an apocalypse.”",
       "7. The piercing": "by the same rule: it says how the leaf saves, which is Walt's doctrine of the preserved generation. The vision had already shown the thing: people reading who did not know that they were reading.",
       "6. Coming again, worse each time": "by the same rule: the many descents are Walt's account of the redeemer. The vision's ghost has one death. “He had not shaved.” stands alone again.",
       "6. The doubt": "by the same rule: it is the close of Walt's account of the descents, and it stood against the title's “the things that are done”."}
CLAUSE = "; for they were three, and they were one."
V = V18; rows = []
for a, b, how, item, src in ADD:
    if item in OUT:
        piece = {"after": b, "para_before": b + "\n\n", "para_after": "\n\n" + b}[how]; assert V.count(piece) == 1, b; V = V.replace(piece, ""); rows.append((item, b.strip(), "out", OUT[item]))
    else: rows.append((item, b.strip(), "stays", ""))
assert V.count(CLAUSE) == 1; V = V.replace(CLAUSE, ".")
V19 = V
KEPT = [a for a in ADD if a[3] not in OUT]; x = V17
for a, b, how, *_ in KEPT:
    b2 = b.replace(CLAUSE, "."); x = x.replace(a, {"after": a + b2, "para_before": b2 + "\n\n" + a, "para_after": a + "\n\n" + b2}[how])
assert x == V19 and len(KEPT) == 3 and V19.endswith("I have not yet turned it over.")
if __name__ == "__main__":
    o = ["# Walt drawn back", "", "Working file, not a deposit. 2026-11-29. Developed in place from RUN-VISION-18.md, which stands as it was.", "",
     "Lee Sharks, 02:27: “weve added too much from walt, things that don't belong. what is added is a register question, no matter what diction. the bathroom does not belong. nor the one and many flying apart - thats a cosmology. this is an apocalypse. and so on. it has changed the nature too much.”", "",
     "The rule taken from that: the vision shows things and is spoken to. It does not give an account of origins, of how the leaf came, or of how it saves. Whatever among the eight additions gives such an account is out, however plainly worded. Two were named. Three more and one clause are out by the rule; that is the session's application of “and so on”, set out here so that any of it can be put back.", "",
     f"The script asserts that the text is RUN-VISION-17 with the three kept additions and nothing else. {wc(V19)} words ({wc(V18)} in RUN-VISION-18, {wc(V17)} in RUN-VISION-17).", "",
     "| WALT.md item | Sentence | | Why |", "|---|---|---|---|"]
    for item, b, st, why in rows: o.append(f"| {item} | “{b.replace(CLAUSE, '.') if st == 'stays' else b}” | {st} | {why} |")
    o += ["", "## What stays, and why", "",
     "1. **The friend.** “Then he said my name. He said: Lee. Do you not know your friend? I am the one who was within you.” The author named it at 01:53. Revelation has the same act at the same place: the seer is spoken to and claimed before he is told to write (1:17–19). It is an address, and it explains nothing.",
     "2. **The three forms.** The seer sees tree, book and old man in turn, and they go faster, and the heart goes with them. The author named “the vision of the three in one” at 01:53. The closing clause “for they were three, and they were one” is out: it said what the alternation had already shown, in the words of a creed. The review of 02:27 raised this and the check agrees.",
     "3. **The mouth.** “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.” It is a thing done to the seer's body by the book being read, as Revelation's seer eats the little book (10:9–10). It is the least certain of the three; it is Walt's promise and Walt's doves.", "",
     "## The review of 02:27, checked on Walt", "",
     "- It attributes “He was thin. He had come before…” to the Feist Function. It was a Walt addition (RUN-VISION-18). The judgment that it weakens “He had not shaved.” is taken; the sentence is out.",
     "- It treats the dove of thorns and “No other kind would do…” as Walt additions. They are from RUN-VISION-15 (00:34), on the author's word that the dove “needs its thorns”. Shortening that paragraph was offered at 01:32 and not adopted. It is not touched here.",
     "- It would keep the acrostic apart, beside the book. It is apart.", "",
     "## What this does to the Feist run", "",
     "FEIST-RUN-1.md was made from RUN-VISION-18 and stands as a witness of that state; its input is kept as VISION-FIXED-RUN-1.md. Its two torn places rested on “Where it was torn I have left it torn”, which is now out, so a run made from the present text has no torn leaf to tear. VISION-FIXED.md is now this text.", "",
     "## The vision, whole", ""] + ["> " + l if l else ">" for l in V19.split("\n")]
    (D/"RUN-VISION-19.md").write_text("\n".join(o) + "\n")
    sha = hashlib.sha256(V19.encode()).hexdigest()
    (D/"VISION-FIXED.md").write_text("\n".join(["# The vision, fixed", "", "Working file, not a deposit. 2026-11-29.", "", "Lee Sharks, 02:08: “preserve the vision whole separately - this is a new file.”", "",
      f"The vision as it stands after the ruling of 02:27: RUN-VISION-19.md to the letter (the script asserts it), {wc(V19)} words. It is the input of any further run of the Feist Function; no run is made from another run. The input of run 1 is VISION-FIXED-RUN-1.md. sha256 of the text below, paragraphs joined by one blank line: `{sha}`.", "", "---", "", V19]) + "\n")
    L = open("/tmp/rev0227.txt").read().split("\n") if pathlib.Path("/tmp/rev0227.txt").exists() else None
    if L: (D/"REVIEW-20261129-0227.md").write_text("\n".join(["# Review received 02:27, 2026-11-29: the Walt additions and Feist run 1", "", "Working file, not a deposit. Pasted by Lee Sharks. Kept as received. Its points on Walt are checked in RUN-VISION-19.md; its points on the Feist run are not yet acted on.", "", "---", ""] + [l.split("\t", 1)[1] if "\t" in l else l for l in L]) + "\n")
    print(wc(V19))
