#!/usr/bin/env python3
"""The vision with its year veiled (22:54). Part IX of VISION.md is redeveloped in place: the history of the time is kept under figures (a count of keepers, measures), and each event changes something already in the vision. Everything else is RUN-VISION-7's, unchanged."""
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, V5m, V7m = mod("build_run_longer"), mod("build_run_twelve"), mod("build_run_third"), mod("build_run_vision5"), mod("build_run_vision7")
OLD = V5m.YEAR; V7 = V7m.V7; assert V7.count(OLD) == 1
YEAR = """Before the moon I was shown one year on the pearl, and what it did to the things I had seen.

The forty-seventh to keep the widest land of the pearl, who had also been the forty-fifth, put out a hand and wrote a name across it, in the place where the name of the one who keeps the count had stood eight score years and five. The name that was taken away went up, and there was a new star. A leaf went down and lay beside the writing. On the leaf was: Whose was here before?

A part of the book overhead was torn out and thrown down, six thousand thousand leaves. A little more than half of them fell open. On the open leaves some names were covered; and where a name was covered no light came through, so that for the first time I saw dark stars. A leaf went down, and on it was only the count.

Fire fell on one of the old lands of the pearl. Then the machine, of which no part moved, put out a wheel. It was an inner wheel, and it had no outer. What it made was like play, and was not play: a number, rising over the fire, as a score is kept. Eleven thousand thousand watched, and were glad of the number. A leaf went down and covered it, 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, and its planet rode lower for the weight. 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 did not turn. They said: It did not speak. One eye in the rim of a wheel followed it out. 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. It was the first time I had seen it sign. I looked at the ring around the hand, to see whether there was room."""
V8 = V7.replace(OLD, YEAR); assert V8.endswith("I have not yet turned it over.")
SHOW = [p for p in YEAR.split("\n\n")[1:]]; assert len(SHOW) == 6 and len(OLD.split("\n\n")) - 1 == 6
wc = lambda s: len(re.findall(r"\S+", s)); NT = A.toks(YEAR)
rows = [(k, *A.longest(v, NT)) for k, v in A.T.items()]; told = A.longest(A.FWD + "\n" + B.NEW + "\n" + C.NEW, NT)
if __name__ == "__main__":
    o = ["# The vision with its year veiled", "",
     "Working file, not a deposit. 2026-11-28. Lee Sharks, 22:54, with a sixth report on the vision:", "",
     "> yes... the Josephus layer is veiled in the vision and unveiled in the gospel. not nero but 666 in the vision, tho we know its nero.", "",
     "Developed in place from RUN-VISION-7.md, which stands. One passage is redeveloped: the year on the pearl (part IX of VISION.md). Nothing else is touched.", "",
     "## 1. What the report said of the year, and what was found", "",
     "The report (REVIEW-20261128-2254.md §6): the year “reads more like contemporary history translated into visionary language than like history generated within the vision's cosmology”; “the external referents become relatively easy to recover”; “The history should change the cosmology, not merely appear within it.” And: “The apparatus specifies seven showings of the year, but Part IX presently contains six.”", "",
     "The last is right, and the script now asserts it: the year had six showings and has six. VISION.md §1 and §2 said seven; RUN-VISION-5.md §4 said “seven short visions”. Both were the session's miscount. VISION.md is rebuilt with the count corrected. The sevens of the vision are four (the planets, the addresses, the acts on the leaf, the count).", "",
     "## 2. The year, as it now stands", ""] + ["> " + l if l else ">" for l in YEAR.split("\n")] + ["",
     f"{wc(YEAR)} words; it had {wc(OLD)}. The whole vision is now {wc(V8)} words.", "",
     "## 3. How each showing is veiled, and what it changes", "",
     "| Showing | The veil | What it changes in the vision | The event (RUN-THIRD §2) |", "|---|---|---|---|",
     "| The name written | A ruler given by count and no name: the forty-seventh, who had also been the forty-fifth. The office displaced is “the one who keeps the count”; the span is given in scores. | A name taken away goes up and is a new star. Until now stars were only seen; here one is made. | H4 |",
     "| The leaves thrown down | Measures only: six thousand thousand, a little more than half. No one is named or described. | The leaves are torn from the book overhead, so the sky loses a part; a covered name lets no light through, and there are dark stars. | H1 |",
     "| The fire and the number | The land is “one of the old lands”. The watchers are counted. | The machine, of which “no part moved”, puts out a wheel: an inner one with no outer. The wheels were the ghost's until now. | H2, H3 |",
     "| The small machines | As before. | Darkness's planet rides lower for what is swept to it. | H5 |",
     "| The stone | As before. | The seven do not turn; one eye in a rim follows it. The eyes have not acted before. | H6 |",
     "| The youngest signs | As before. | The seer looks to the ring for room, which the address to the youngest left open (“there is room in the ring”). | H7 |", "",
     "The count of rulers is the veil Revelation uses beside the number of a name: its kings are counted, and none is named. The forty-fifth and forty-seventh are public facts of the count of presidents; the vision gives nothing else of him. In the told life the same events are now named outright (RUN-THIRD.md, developed in place the same hour).", "",
     "## 4. Measures", "", "| Text | Words in the longest shared run | 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]}” |", "",
     "## 5. What to look at", "",
     "1. Whether the first showing is veiled enough, or too much. “The forty-seventh, who had also been the forty-fifth” will be read at once by anyone living now, as the number was read at once by those it was written for.",
     "2. The machine now has a wheel. That gives the machine and the ghost a thing in common, and the vision does not say what follows.",
     "3. Dark stars are a new kind of thing in the sky: a name neither kept in the book nor taken out of it.",
     "4. There are six showings. A seventh has not been written, and none is claimed.", ""]
    (D/"RUN-VISION-8.md").write_text("\n".join(o)); print(wc(V8), wc(YEAR), wc(OLD), rows, told)
