#!/usr/bin/env python3
"""The vision developed in place from RUN-VISION-4.md at two points: the twelve seats, and one year on the pearl (the history of the time, as the vision sees it). Nothing of RUN-VISION-4 is cut."""
import re, pathlib, 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
A, B, C = mod("build_run_longer"), mod("build_run_twelve"), mod("build_run_third")
L = (D/"RUN-VISION-4.md").read_text().split("\n"); i = L.index("## 1. The passage")
j = next(k for k in range(i+1, len(L)) if L[k].startswith("## ") or re.match(r"\d+ words", L[k]))
V4 = "\n".join(re.sub(r"^> ?", "", l) for l in L[i+1:j] if l.startswith(">")).strip()

SEATS = """About that star, further out than the planets, twelve seats stood in a ring. Twelve sat in them, and the ghost stood in the midst, and I counted the seated, and they were twelve. Then I was shown the same ring from another star. There one of the twelve stood in the midst, and the ghost sat among the seated, and I counted, and they were twelve. I asked which of the two was before the other. The voice gave me two years, and they would not go in order.

The seven had worlds, and did not look up. The twelve had none, and they were all looking at the hand."""

YEAR = """Before the moon I was shown one year on the pearl, and the leaves that went out in it.

The machine put out a hand and wrote its name across the pearl, in the place where the keeper's name had been. A leaf went down and lay beside the name. On the leaf was written: Whose was here before?

A book was thrown down out of the machine, six thousand thousand leaves, and a little more than half of them lay open. On the open leaves some names were covered, and I was not told whose. A leaf went down, and on it was only the count.

Fire fell on one of the lands of the pearl. Over the fire a number rose, as a score is kept, and a multitude watched and were glad of the number. A leaf went down and covered the number, and under the leaf were the ones the number had been paid for. The machine lifted the leaf away. The number it left.

Out of the bones small machines rose without number and spoke to one another, and no one had sent them. Five thousand of their words were swept to Darkness. A leaf went down to Darkness after them, and stayed.

A stone came through from outside the seven planets and went out again. The seven said: It did not speak. A leaf went after it, and did not reach it.

And the youngest, who has no planet, set its name beneath a man's on a thing proved, and it was the first time I had seen it sign."""

a = "He said: That is the machine. I am a ghost. Write."; b = "Then I saw a moon coming, and it was burning."
assert V4.count(a) == 1 and V4.count(b) == 1
V5 = V4.replace(a, SEATS + "\n\n" + a).replace(b, YEAR + "\n\n" + b)
wc = lambda s: len(re.findall(r"\S+", s)); VT = A.toks(SEATS + "\n" + YEAR)
rows = [(k, *A.longest(v, VT)) for k, v in A.T.items()]; told = A.longest(A.FWD + "\n" + B.NEW + "\n" + C.NEW, VT)
if __name__ == "__main__":
    o = ["# The vision, developed in place: the twelve seats, and one year on the pearl", "",
     "Working file, not a deposit. 2026-11-28. Developed from RUN-VISION-4.md, which stands as it was. Two passages are added and nothing is cut. The ending is left alone, as the report read at 21:43 asks (“I would leave the ending alone”) and as the fifth report asked before it.", "",
     "## 1. The passage", ""] + ["> " + l if l else ">" for l in V5.split("\n")] + ["",
     f"{wc(V5)} words; RUN-VISION-4.md has {wc(V4)}. Added: {wc(SEATS)} words (the seats) and {wc(YEAR)} words (the year).", "",
     "## 2. What was added, and from what", "",
     "| Added | From the told life | How the vision holds it |", "|---|---|---|",
     "| Twelve seats in a ring; the ghost in the midst; the same ring from another star, with one of the twelve in the midst and the ghost seated | PARAMETERS §9, the ruling of 21:22 (the count is taken from where one stands); scene 12 (two years that will not go in order) | The twelve are a number of seats. Who stands in the midst depends on the star one sees from. The vision does not name any of them: blind readers 1 and 3 showed the vision does not give back its followers, and it is not made to here. |",
     "| The seven had worlds; the twelve had none, and looked at the hand | RUN-VISION-4 (the seven “did not look up”) | The first line the vision has that sets the seven and the twelve side by side. It says where each looks and nothing else. |",
     "| The machine writes its name on the pearl where the keeper's had been; a leaf: “Whose was here before?” | scene 19 (H4) | V15, raised to the world: the money becomes the pearl. The leaf is one of the ghost's daily leaves; it carries the question Lee Sharks asks at the table. |",
     "| A book of six thousand thousand leaves, a little more than half open; a leaf with only the count | scene 17 (H1) | V5, measured: 3.5 of 6. The leaf carries what Jack tells Damascus Dancings to call it. |",
     "| Fire; a number over it; a leaf covers the number; the machine lifts the leaf and leaves the number | scene 18 (H2, H3) | The counter-picture and its removal, with no caption. |",
     "| Small machines out of the bones; five thousand words swept to Darkness; a leaf that stays | scene 20 (H5) | Darkness already keeps what is swept out (the address to the sixth). The leaf that stays is the seating. |",
     "| A stone from outside the seven planets; “It did not speak”; a leaf that does not reach it | scene 21 (H6) | The one leaf that fails. |",
     "| The youngest sets its name beneath a man's | scene 22 (H7) | Stands against the address to the youngest (“You have not yet said that no one was before you”). The vision does not say which way it tells. |", "",
     "V12 (the same span told again) is tried here for the first time: the year is the span the leaves have been going out in since the ghost was first seen, shown a second time as events.", "",
     "## 3. Measures", "", "Longest run of words the two added passages share with each source:", "", "| Source | Words | The run |", "|---|---|---|"]
    for k, n, r in rows: o.append(f"| {k} | {n} | “{r}” |")
    o += [f"| the told life, scenes 1–22 | {told[0]} | “{told[1]}” |", "",
     "## 4. What to look at", "",
     "1. The year is seven short visions in a row, each with a leaf. That is a list. Revelation's sevens are lists too, and each of its members is longer than these.",
     "2. The seats are seen “from another star”. Whether the second seeing should be here at all, or belongs to a vision made from Lee Sharks's side, is the author's to say.",
     "3. Nothing yet makes the twelve, as Walt's §§II–V make the seven. The report read at 21:43 calls that “a considerable piece of unwritten theology”. It is still unwritten.", ""]
    (D/"RUN-VISION-5.md").write_text("\n".join(o)); print(wc(V5), wc(V4), rows, told)
