GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Grid network and substations / sld / tests

Build Topology and 3 more files

A Python command-line tool in the tools folder of Spiders that fetches cables from GitHub API. It scans local (`scan_local`), lists repos API (`list_repos_api`) and scans API (`scan_api`).

681 lines of Python from sld, soil, spiders, in 4 files. Topic: Grid network and substations. Section 452 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Test Furnace Gate test · sld / tests/test_furnace_gate.py lines 1–67
  2. Test Sources test · sld / tests/test_sources.py lines 1–109
  3. Import Network storage · soil / import_network.py lines 1–33
  4. Build Topology render · spiders / spider_printer_v1/tools/build_topology.py lines 1–472

Test Furnace Gate

A test in the tests folder of Single-Line Diagram (SLD) that checks Single-Line Diagram (SLD) Verify Furnace.

testsld / tests/test_furnace_gate.py · lines 1–67 of 67 · Entry point

What it does
It tests its inputs. Defines 7 functions or classes, including FurnaceGateTests, setUp, write, test_valid_fixture, test_bound_file_mutation_rejected. Uses libraries copy, json, pathlib, sys, tempfile. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain stage field_execution (path word 'gate'): on-road and off-road field navigation to the farm gate (access gates, terrain slope, ground-bearing limits, electrical hazards), as Ventus Ltd describes its SatNav work.
Inputs & outputs
In: JSON file library.json; JSON file source-lock.json; JSON file photon-library.json; JSON file photon-evidence.json.
Downstream impact
Used directly by 1 file: Single-Line Diagram (SLD) Pages Workflow (sld).
What it achieves
It sets up (`setUp`) and is built from the furnace gate tests class (`FurnaceGateTests`). Defines `FurnaceGateTests`, `setUp`, `write` and 4 more. It writes `data/library.json`.
Why it matters
Run by Single-Line Diagram (SLD) Pages Workflow.
Application
sld · tests · test
Uses
Single-Line Diagram (SLD) Verify Furnace (sys.path import), copy, json, pathlib, sys, tempfile, unittest
Used by
run by Single-Line Diagram (SLD) Pages Workflow (workflow run)
Produces
FurnaceGateTests
Defines here
FurnaceGateTests, setUp, write, test_valid_fixture, test_bound_file_mutation_rejected, test_invalid_receipts, test_duplicate_domains_and_corrupt_counts
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
sld@1205aa0d1f80d8e05a2cbb19f363939174b50002 tests/test_furnace_gate.py:1-67 · blob 112a490def8aa5a3e7f44a04f2cc7ba0844ff362 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem · domain stage: field_execution · users: EPC_contractor, landowner_planner · canonical: unknown
import copy
import json
from pathlib import Path
import sys
import tempfile
import unittest

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from verify_furnace import digest, verify


class FurnaceGateTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory()
        self.addCleanup(self.temp.cleanup)
        self.root = Path(self.temp.name)
        (self.root / 'data/furnace').mkdir(parents=True)
        (self.root / 'scripts').mkdir()
        self.write('data/library.json', {'components': [{'id': 'a', 'size': {'width': 1, 'height': 1}}]})
        self.write('sources.yaml', {'sources': []})
        self.write('data/source-lock.json', {'files': []})
        (self.root / 'scripts/furnace.py').write_text('test fixture only')
        self.write('data/furnace/photon-library.json', {'library_sha256': digest(self.root / 'data/library.json'), 'production_enabled': False, 'components': [{'id': 'a', 'width': 1., 'height': 1.}]})
        self.receipt = dict(accepted=True, library_sha256=digest(self.root / 'data/library.json'), source_lock_sha256=digest(self.root / 'data/source-lock.json'), script_sha256=digest(self.root / 'scripts/furnace.py'), photon_library_sha256=digest(self.root / 'data/furnace/photon-library.json'), component_count=1, excluded_component_count=0, grid=2, pitch=.5, cpu_reference_cases=4, threshold_fixtu  … [line shortened here: 628 characters; full line on GitHub]
        self.write('data/furnace/photon-evidence.json', self.receipt)

    def write(self, name, value):
        (self.root / name).write_text(json.dumps(value), encoding='utf-8')

    def test_valid_fixture(self):
        self.assertEqual(verify(self.root)['unique_cases'], 4)

    def test_bound_file_mutation_rejected(self):
        for path in ['data/library.json', 'data/source-lock.json', 'scripts/furnace.py', 'data/furnace/photon-library.json']:
            with self.subTest(path=path):
                file = self.root / path
                original = file.read_bytes()
                file.write_bytes(original + b' ')
                with self.assertRaises(ValueError):
                    verify(self.root)
                file.write_bytes(original)

    def test_invalid_receipts(self):
        mutations = [dict(accepted=False), dict(accepted=1), dict(injected_error_detected=False), dict(threshold_fixtures=0), dict(unique_cases=8), dict(cpu_reference_cases=0), dict(component_count=2), dict(iterations=[])]
        for mutation in mutations:
            with self.subTest(mutation=mutation):
                self.write('data/furnace/photon-evidence.json', self.receipt | mutation)
                with self.assertRaises(ValueError):
                    verify(self.root)

    def test_duplicate_domains_and_corrupt_counts(self):
        for edit in ['duplicate', 'mismatch', 'counts']:
            receipt = copy.deepcopy(self.receipt)
            if edit == 'duplicate':
                receipt['iterations'] *= 2
                receipt['unique_cases'] *= 2
            elif edit == 'mismatch':
                receipt['iterations'][0]['mismatches'] = 1
            else:
                receipt['iterations'][0]['separated'] = 0
            self.write('data/furnace/photon-evidence.json', receipt)
            with self.assertRaises(ValueError):
                verify(self.root)


if __name__ == '__main__':
    unittest.main()

Test Sources

A test in the tests folder of Single-Line Diagram (SLD) that checks Single-Line Diagram (SLD) Fetch Sources and Single-Line Diagram (SLD) Build Catalogue, using SHA-256.

testsld / tests/test_sources.py · lines 1–109 of 109 · Entry point · pattern archive (shared by 3 files)

What it does
It tests its inputs. Defines 12 functions or classes, including archive, AcquisitionTests, test_decc_evidence_stable_despite_nonce_changes, test_offline_detects_local_tampering, test_zip_rejects_traversal_and_executables. Uses libraries io, json, pathlib, sys, tempfile. Side effects: reads or writes files; makes network requests.
Why it exists in the grid workflow
Domain stage sub33kv_design (repository name word 'sld'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary.
Inputs & outputs
In: JSON file source-lock.json; JSON file components.json. Out: JSON file source-lock.json.
Downstream impact
Used directly by 1 file: Single-Line Diagram (SLD) Pages Workflow (sld).
What it achieves
It is built from the acquisition tests class (`AcquisitionTests`) and the catalogue tests class (`CatalogueTests`). Defines `archive`, `AcquisitionTests`, `test_decc_evidence_stable_despite_nonce_changes` and 9 more.
Why it matters
Run by Single-Line Diagram (SLD) Pages Workflow.
Application
sld · tests · test
Uses
Single-Line Diagram (SLD) Fetch Sources (sys.path import), Single-Line Diagram (SLD) Build Catalogue (sys.path import), io, json, pathlib, sys, tempfile, unittest, unittest.mock, zipfile
Used by
run by Single-Line Diagram (SLD) Pages Workflow (workflow run)
Produces
archive, AcquisitionTests, CatalogueTests
Defines here
archive, AcquisitionTests, test_decc_evidence_stable_despite_nonce_changes, test_offline_detects_local_tampering, test_zip_rejects_traversal_and_executables, test_zip_rejects_binary_missing_and_oversize, test_zip_preserves_text_without_execution, test_lock_rejects_changed_hash_url_and_file_set, test_filename_rejects_paths, test_download_rejects_oversize_body, CatalogueTests, test_full_library_preserves_geometry_and_is_deterministic
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
sld@1205aa0d1f80d8e05a2cbb19f363939174b50002 tests/test_sources.py:1-109 · blob a0c5681d89039bd005d96e7f029232ff0dae089e · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem, network · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unknown
import io
import json
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
import zipfile

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
from fetch_sources import extract_text_zip, verify_lock, safe_name, download, check_offline, digest, decc_licence_evidence
from build_catalogue import build


def archive(files):
    stream = io.BytesIO()
    with zipfile.ZipFile(stream, "w") as value:
        for name, data in files:
            value.writestr(name, data)
    return stream.getvalue()


class AcquisitionTests(unittest.TestCase):
    def test_decc_evidence_stable_despite_nonce_changes(self):
        source = next(item for item in json.loads((ROOT / "sources.yaml").read_text())["sources"] if item["id"] == "decc")
        pdf = next(item["url"] for item in source["files"] if item["name"].endswith(".pdf"))
        html = f'<h1>{source["title"]}</h1><a href="{pdf}">PDF</a><footer>All content is available under the <a rel="license" href="{source["licence_url"]}">Open Government Licence v3.0</a>, except where otherwise stated <a>Crown copyright</a></footer>'
        expected = decc_licence_evidence(html.encode(), source)
        self.assertEqual(expected, decc_licence_evidence((html + '<script nonce="random">analytics=42</script>').encode(), source))
        self.assertNotIn(b"\r", expected)
        for changed in (html.replace("Licence v3.0", "Licence v2.0"), html.replace(source["licence_url"], "https://example.org/restricted"), html.replace(pdf, "https://example.org/wrong.pdf")):
            with self.assertRaises(ValueError):
                decc_licence_evidence(changed.encode(), source)

    def test_offline_detects_local_tampering(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            path = root / "upstream/s/file.txt"
            path.parent.mkdir(parents=True)
            path.write_bytes(b"original")
            entry = dict(path="upstream/s/file.txt", source_id="s", licence="CC-BY-4.0", url="https://example.org/file", bytes=8, sha256=digest(b"original"))
            (root / "source-lock.json").write_text(json.dumps({"files": [entry]}))
            config = {"sources": [{"id": "s", "licence": "CC-BY-4.0", "files": [{"name": "file.txt", "url": entry["url"]}]}]}
            check_offline(config, root)
            path.write_bytes(b"modified")
            with self.assertRaises(ValueError):
                check_offline(config, root)

    def test_zip_rejects_traversal_and_executables(self):
        for name in ("../escape.dss", "C:/escape.dss", "send_network_dsse/evil.exe"):
            with self.assertRaises(ValueError):
                extract_text_zip(archive([(name, b"hello")]), [name])

    def test_zip_rejects_binary_missing_and_oversize(self):
        name = "send_network_dsse/master.dss"
        for files, limit in (([(name, b"\x00")], 100), ([], 100), ([(name, b"abc")], 2)):
            with self.assertRaises(ValueError):
                extract_text_zip(archive(files), [name], limit)

    def test_zip_preserves_text_without_execution(self):
        name = "send_network_dsse/master.dss"
        data = b"compile an-untrusted-command\n"
        self.assertEqual(extract_text_zip(archive([(name, data)]), [name]), {name: data})

    def test_lock_rejects_changed_hash_url_and_file_set(self):
        item = dict(path="x", sha256="abc", url="https://example.org/x", licence="CC-BY-4.0", source_id="s")
        previous = {"files": [item]}
        for field in ("sha256", "url", "licence", "source_id"):
            changed = dict(item, **{field: "changed"})
            with self.assertRaises(ValueError):
                verify_lock([changed], previous)
        with self.assertRaises(ValueError):
            verify_lock([], previous)

    def test_filename_rejects_paths(self):
        for name in ("../x.svg", "a/b.svg", "a\\b.svg", ".."):
            with self.assertRaises(ValueError):
                safe_name(name)

    def test_download_rejects_oversize_body(self):
        response = unittest.mock.MagicMock()
        response.__enter__.return_value = response
        response.geturl.return_value = "https://example.org/file"
        response.headers = {}
        response.read.return_value = b"1234"
        with patch("urllib.request.urlopen", return_value=response), self.assertRaises(ValueError):
            download("https://example.org/file", 3)


@unittest.skipUnless((ROOT / "data/source-lock.json").exists(), "Acquire sources for catalogue integration check")
class CatalogueTests(unittest.TestCase):
    def test_full_library_preserves_geometry_and_is_deterministic(self):
        config = json.loads((ROOT / "sources.yaml").read_text())
        result = build(config, ROOT / "data")
        original = json.loads((ROOT / "data/upstream/powsybl/components.json").read_text())
        by_type = {item["type"]: item for item in result["components"]}
        self.assertEqual(result["counts"], {"components": 35, "svg_files": 37})
        for item in original:
            actual = by_type[item["type"]]
            for key in ("size", "anchorPoints", "transformations"):
                self.assertEqual(item.get(key), actual.get(key))
            self.assertFalse(actual["production_enabled"])
            self.assertEqual(actual["standards_status"], "unverified")
        self.assertEqual(result, build(config, ROOT / "data"))


if __name__ == "__main__":
    unittest.main()

Import Network

A Python module in Soil that fetches network sources, passages and summary, using SHA-256.

storagesoil / import_network.py · lines 1–33 of 33 · Not reached from any entry point (candidate for review)

What it does
It fetches network sources, passages and summary. Uses libraries hashlib, json, time, urllib.request, pathlib. Side effects: reads or writes files; makes network requests.
Why it exists in the grid workflow
Domain stage sub33kv_design (repository name word 'soil'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. README: "`import_network.py`: local study cache of publicly available network thermal-design documents."
Inputs & outputs
In: JSON file network_sources.json. Out: JSON file .passages.json; JSON file summary.json.
What it achieves
Bounded local cache of published network thermal-design evidence.
Why it matters
No other file in the estate was found to import it.
Application
soil · / · Python module
Uses
hashlib, json, time, urllib.request, pathlib, pymupdf
Used by
none found
Produces
none declared
Defines here
none
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
soil@d26288ed03df533507739b9f218358fc7aa36a01 import_network.py:1-33 · blob 01b63c5af931947379098a6da7389fd1f13453ad · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, network · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unreached_review
"""Bounded local cache of published network thermal-design evidence."""
import hashlib,json,time,urllib.request
from pathlib import Path
import pymupdf
ROOT=Path(__file__).parent
OUT=ROOT/'.local'/'network';OUT.mkdir(parents=True,exist_ok=True)
sources=json.loads((ROOT/'network_sources.json').read_text())['sources']
terms=['thermal resistivity','soil temperature','moisture','drying','dry out','backfill','burial','ground temperature','thermal conductivity']
results=[]
for src in sources:
    p=OUT/(src['id']+'.pdf')
    try:
        cached=p.exists()
        if not cached:
            request=urllib.request.Request(src['url'],headers={'User-Agent':'SoilResearch/1.0 bounded public-document study'})
            with urllib.request.urlopen(request,timeout=35) as r:body=r.read(15*1024*1024+1)
            if len(body)>15*1024*1024:raise ValueError('15MB cap exceeded')
            if not body.startswith(b'%PDF'):raise ValueError('Not a PDF; no challenge bypass')
            p.write_bytes(body)
        body=p.read_bytes();pages=[]
        with pymupdf.open(stream=body,filetype='pdf') as doc:
            if len(doc)>1000:raise ValueError('Page cap exceeded')
            for i,page in enumerate(doc):
                text=page.get_text();normal=' '.join(text.lower().split());hits=[t for t in terms if t in normal]
                if hits:pages.append({'page':i+1,'terms':hits,'text':text,'review_status':'candidate_not_interpreted'})
            total=len(doc)
        result={**src,'sha256':hashlib.sha256(body).hexdigest(),'cached':cached,'bytes':len(body),'pages':total,'candidate_pages':len(pages),'analysed_at':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime())}
        (OUT/(src['id']+'.passages.json')).write_text(json.dumps({'source':result,'pages':pages},indent=2),encoding='utf8')
        results.append(result)
    except Exception as e:results.append({'id':src['id'],'error':str(e)})
    time.sleep(.5)
(OUT/'summary.json').write_text(json.dumps(results,indent=2),encoding='utf8')
print(json.dumps([{k:v for k,v in r.items() if k in ['id','pages','candidate_pages','bytes','error']} for r in results]))

Build Topology

A Python command-line tool in the tools folder of Spiders that fetches cables from GitHub API. It scans local (`scan_local`), lists repos API (`list_repos_api`) and scans API (`scan_api`).

renderspiders / spider_printer_v1/tools/build_topology.py · lines 1–472 of 472 · Entry point · pattern classify (shared by 27 files)

What it does
It fetches cables. Defines 12 functions or classes, including classify, repo_reference_pattern, scan_local, api_json, list_repos_api. Uses libraries __future__, argparse, json, os, re. Side effects: changes the web page (DOM); reads or writes files; makes network requests; starts processes.
Why it exists in the grid workflow
Domain stage agent_infra (path word 'spider'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief). Domain terms in the code: cables.
Inputs & outputs
In: data source: GitHub API; JSON file topology.json; command-line arguments.
Downstream impact
Used directly by 2 files: Test Build Topology (spiders); Elements Topology Workflow (spiders).
What it achieves
Build `spider_printer_v1/data/topology.json` from the estate as it actually is. It accepts `--local`, `--api` and `--out`.
Why it matters
Run by Elements Topology Workflow. Checked by Test Build Topology.
Application
spiders · spider_printer_v1/tools · Python CLI
Uses
__future__, argparse, json, os, re, subprocess, sys, urllib.request
Used by
imported by Test Build Topology (import), run by Elements Topology Workflow (workflow run)
Produces
classify, repo_reference_pattern, scan_local, api_json, list_repos_api, scan_api, mapped, layout, outside_map, overlapping, build, main
Defines here
classify, repo_reference_pattern, scan_local, api_json, list_repos_api, scan_api, mapped, layout, outside_map, overlapping, build, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
spiders@78fc0f90bd5ca73f3d5d7c0e0b7b5712cfe781e5 spider_printer_v1/tools/build_topology.py:1-472 · blob b29fad975daadf82fe13e22e57e8b7b1df66d5d2 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, filesystem, network, process · domain stage: agent_infra · users: ai_agent · canonical: unknown
#!/usr/bin/env python3
"""Build spider_printer_v1/data/topology.json from the estate as it actually is.

WHY THIS EXISTS
---------------
The first topology cartridge was hand-drawn on 30 August 2026: 38 nodes with
hand-placed coordinates and conceptual names. It was a good drawing and it went
stale immediately. By 6 September the estate had gained ventus-grid-engine,
teleprinter, gpu-drivers-for-global-grid, unnamed-01, layout-tool,
cable-trench-or-drill and more, none of which appear on it, and a map that
silently omits a third of its subject is worse than no map because it is still
believed.

So the cartridge is generated, not drawn. Nodes are the repositories that exist.
Edges are references that were actually found in committed bytes. Positions are
computed deterministically from the layer assignment, so the same input always
produces the same file and a diff means the estate moved, not that the layout
engine wandered.

WHAT COUNTS AS AN EDGE
----------------------
A reference from repository A to repository B, found by scanning A's committed
text for B's GitHub or Pages URL. That is evidence: somebody wrote that link.
It is deliberately NOT an inferred dependency -- nothing here parses imports,
guesses at intent, or connects two repositories because they sound related. An
edge you can click through to is worth more than a clever one you cannot.

The count of references is carried on the edge, because one mention in a README
and forty in a build script are different relationships.

WHAT IT REFUSES
---------------
It does not certify that the federation is correct, complete or well designed.
It does not invent an edge to make the picture connected. A repository with no
found references is drawn unconnected, because that is what the evidence says.

Two modes:
  --local <dir>   scan sibling clones on disk (fast, offline, needs the clones)
  --api           list the org's public repositories and scan their default
                  branch through the GitHub API (what CI uses)

Run:
  python3 -B tools/build_topology.py --local ../..
  python3 -B tools/build_topology.py --api --out data/topology.json
"""

from __future__ import annotations

import argparse
import json
import os
import re
import subprocess
import sys
import urllib.request
from datetime import datetime, timezone
from pathlib import Path

ORG = "Ventusltd"
SCHEMA_VERSION = "0.2.0"

# Which column a repository is drawn in. A repository that matches no rule is
# placed in "application", which is stated in the output rather than hidden, so
# an unclassified repo is visible as unclassified instead of silently absorbed.
LAYER_RULES = [
    ("data", re.compile(r"^(data-|seed-data|registry)")),
    ("engines", re.compile(r"(engine|maths|geodesy|gpu-drivers|linux-for-the-power-grid)")),
    ("observation", re.compile(r"^(spiders|cvaa|chatgpt-audits|codex-chatgpt|gemini|claude)")),
    ("publication", re.compile(r"^(globalgrid2050|pipelinenews|ventusltd\.com|studies)")),
    ("tools", re.compile(r"(teleprinter|layout-tool|cable-trench|sandbox|testcode|companies)")),
]

LAYER_ORDER = ["data", "engines", "apps", "tools", "observation", "publication"]

# `kind` is the class the VIEWER already styles. The viewer's CSS vocabulary is
# data / engine / lib / appn / schema / infra / external / future, and reusing it
# is deliberate: inventing a seventh vocabulary here would mean every node
# rendered unstyled while the JSON looked perfectly correct.
LAYER_META = {
    "data":        {"label": "Data spines",           "colour": "#00ff88", "kind": "data",   "defaultOn": True},
    "engines":     {"label": "Engines",               "colour": "#bdb2ff", "kind": "engine", "defaultOn": True},
    "apps":        {"label": "Applications",          "colour": "#61d7ff", "kind": "appn",   "defaultOn": True},
    "tools":       {"label": "Tools",                 "colour": "#ffae00", "kind": "lib",    "defaultOn": True},
    "observation": {"label": "Observation and audit", "colour": "#ff8f8f", "kind": "schema", "defaultOn": True},
    "publication": {"label": "Publication",           "colour": "#4ad9c4", "kind": "infra",  "defaultOn": True},
}

TEXT_SUFFIXES = {
    ".md", ".json", ".js", ".mjs", ".py", ".yml", ".yaml", ".html", ".txt", ".css", ".sh", ".toml",
}

SKIP_DIRS = {".git", "node_modules", "__pycache__", ".venv", "dist", "releases", "archive"}

# A repository is not an edge to itself, and these names appear inside URLs for
# reasons that are not references.
MAX_FILE_BYTES = 400_000


def classify(name: str) -> str:
    for layer, pattern in LAYER_RULES:
        if pattern.search(name):
            return layer
    return "apps"


def repo_reference_pattern(names: list[str]) -> re.Pattern:
    """One pass over each file, matching any repository name in a GitHub or
    Pages URL. Sorted longest-first so `data-grid-gb` is not matched as
    `data-grid` when both exist."""
    alternation = "|".join(re.escape(n) for n in sorted(names, key=len, reverse=True))
    return re.compile(
        r"(?:github\.com/" + ORG + r"/|" + ORG.lower() + r"\.github\.io/)(" + alternation + r")\b",
        re.IGNORECASE,
    )


def scan_local(root: Path, names: list[str],
               clone_dirs: dict[str, str] | None = None) -> dict[str, dict[str, int]]:
    pattern = repo_reference_pattern(names)
    found: dict[str, dict[str, int]] = {n: {} for n in names}
    for name in names:
        repo = root / (clone_dirs or {}).get(name, name)
        if not (repo / ".git").exists():
            continue
        for dirpath, dirnames, filenames in os.walk(repo):
            dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
            for filename in filenames:
                path = Path(dirpath) / filename
                if path.suffix.lower() not in TEXT_SUFFIXES:
                    continue
                try:
                    if path.stat().st_size > MAX_FILE_BYTES:
                        continue
                    text = path.read_text(encoding="utf-8", errors="ignore")
                except OSError:
                    continue
                for match in pattern.finditer(text):
                    target = match.group(1).lower()
                    if target != name.lower():
                        found[name][target] = found[name].get(target, 0) + 1
    return found


def api_json(url: str, token: str | None):
    request = urllib.request.Request(url, headers={
        "Accept": "application/vnd.github+json",
        "User-Agent": "spider-printer",
        **({"Authorization": f"Bearer {token}"} if token else {}),
    })
    with urllib.request.urlopen(request, timeout=60) as response:
        return json.load(response)


def list_repos_api(token: str | None) -> list[dict]:
    repos, page = [], 1
    while True:
        batch = api_json(f"https://api.github.com/orgs/{ORG}/repos?per_page=100&page={page}", token)
        if not batch:
            break
        repos.extend(batch)
        if len(batch) < 100:
            break
        page += 1
    return [r for r in repos if not r.get("archived")]


def scan_api(repos: list[dict], names: list[str], token: str | None) -> dict[str, dict[str, int]]:
    """Scan each repository's default branch through the code-search-free route:
    fetch the tree, then the text blobs small enough to be worth reading."""
    pattern = repo_reference_pattern(names)
    found: dict[str, dict[str, int]] = {n: {} for n in names}
    for repo in repos:
        name = repo["name"]
        try:
            tree = api_json(
                f"https://api.github.com/repos/{ORG}/{name}/git/trees/"
                f"{repo['default_branch']}?recursive=1", token)
        except Exception as exc:                      # noqa: BLE001 - reported, not raised
            print(f"  ! {name}: tree unavailable ({exc})", file=sys.stderr)
            continue
        for entry in tree.get("tree", []):
            if entry.get("type") != "blob":
                continue
            path = entry.get("path", "")
            if Path(path).suffix.lower() not in TEXT_SUFFIXES:
                continue
            if entry.get("size", 0) > MAX_FILE_BYTES:
                continue
            if any(part in SKIP_DIRS for part in Path(path).parts):
                continue
            raw = (f"https://raw.githubusercontent.com/{ORG}/{name}/"
                   f"{repo['default_branch']}/{path}")
            try:
                request = urllib.request.Request(raw, headers={"User-Agent": "spider-printer"})
                with urllib.request.urlopen(request, timeout=30) as response:
                    text = response.read().decode("utf-8", errors="ignore")
            except Exception:                         # noqa: BLE001 - a missing blob is not fatal
                continue
            for match in pattern.finditer(text):
                target = match.group(1).lower()
                if target != name.lower():
                    found[name][target] = found[name].get(target, 0) + 1
    return found


# The viewer maps node coordinates through
#   mapPos(n) = { x: 360 + (n.x/3600)*1180,  y: 150 + (n.y/3000)*820 }
# so node coordinates live in a 3600x3000 VIRTUAL space, not in sheet pixels
# and not in the canvas the cartridge declares. The hand-drawn cartridge
# declared a 2400x1500 canvas that never matched those divisors, which is
# harmless when coordinates are hand-placed to look right and fatal the moment
# they are generated. Emit in the space the viewer actually divides by.
VIRTUAL_W, VIRTUAL_H = 3600, 3000
MAP_LEFT, MAP_TOP, MAP_W, MAP_H = 360, 150, 1240, 880
MAP_SCALE_W, MAP_SCALE_H = 1180, 820          # the divisors mapPos actually uses
SHEET_W, SHEET_H = 1920, 1080
NODE_W, NODE_H = 150, 64


def mapped(node: dict) -> tuple[float, float]:
    """The sheet position the viewer will draw this node at."""
    return (MAP_LEFT + (node["x"] / VIRTUAL_W) * MAP_SCALE_W,
            MAP_TOP + (node["y"] / VIRTUAL_H) * MAP_SCALE_H)


def layout(nodes: list[dict]) -> None:
    """Deterministic: columns are layers in a fixed order, rows are repositories
    sorted by name. No randomness, no force simulation, no hand placement — so a
    diff in this file means the estate changed.

    Bounds are chosen so that the MAPPED box, including the node's 150x64
    footprint, lands inside the map area with a gap between neighbours."""
    by_layer: dict[str, list[dict]] = {}
    for node in nodes:
        by_layer.setdefault(node["layer"], []).append(node)
    present = [layer for layer in LAYER_ORDER if layer in by_layer]

    # Work backwards from the sheet: the last column must leave room for the
    # node's width, and the last row for its height.
    max_mapped_x = MAP_LEFT + MAP_W - NODE_W - 16
    max_mapped_y = MAP_TOP + MAP_H - NODE_H - 16
    right = ((max_mapped_x - MAP_LEFT) / MAP_SCALE_W) * VIRTUAL_W
    bottom = ((max_mapped_y - MAP_TOP) / MAP_SCALE_H) * VIRTUAL_H
    left, top = 60, 60

    span_x = (right - left) / max(1, len(present) - 1) if len(present) > 1 else 0
    for column, layer in enumerate(present):
        members = sorted(by_layer[layer], key=lambda n: n["id"])
        span_y = (bottom - top) / max(1, len(members) - 1) if len(members) > 1 else 0
        for row, node in enumerate(members):
            node["x"] = round(left + column * span_x)
            node["y"] = round(top + row * span_y) if len(members) > 1 else round((top + bottom) / 2)


def outside_map(nodes: list[dict]) -> list[str]:
    """Which nodes the viewer would clip, checked on the MAPPED position rather
    than the raw one. Should always be empty; returned so the generator can
    refuse rather than publish a node nobody can see."""
    bad = []
    for node in nodes:
        x, y = mapped(node)
        if not (MAP_LEFT <= x and x + NODE_W <= MAP_LEFT + MAP_W
                and MAP_TOP <= y and y + NODE_H <= MAP_TOP + MAP_H):
            bad.append(node["id"])
    return bad


def overlapping(nodes: list[dict]) -> int:
    """How many pairs of node boxes intersect once mapped. Overlap is a
    readability defect, not a correctness one, so it is reported rather than
    refused — but it is reported, because 98 overlapping pairs is not a map."""
    boxes = []
    for node in nodes:
        x, y = mapped(node)
        boxes.append((x, y, x + NODE_W, y + NODE_H))
    pairs = 0
    for i in range(len(boxes)):
        for j in range(i + 1, len(boxes)):
            a, b = boxes[i], boxes[j]
            if a[0] < b[2] and b[0] < a[2] and a[1] < b[3] and b[1] < a[3]:
                pairs += 1
    return pairs


def build(found: dict[str, dict[str, int]], repo_meta: dict[str, dict], source: str) -> dict:
    names = sorted(found)
    canvas = {"width": VIRTUAL_W, "height": VIRTUAL_H,
              "sheet": {"width": SHEET_W, "height": SHEET_H},
              "map": {"left": MAP_LEFT, "top": MAP_TOP, "width": MAP_W, "height": MAP_H,
                      "scaleW": MAP_SCALE_W, "scaleH": MAP_SCALE_H},
              "node": {"width": NODE_W, "height": NODE_H},
              "print": {"size": "A1", "orientation": "landscape"}}

    nodes = []
    for name in names:
        meta = repo_meta.get(name, {})
        nodes.append({
            "id": name,
            "label": name,
            "subtitle": (meta.get("description") or "no description published")[:110],
            "kind": LAYER_META[classify(name)]["kind"],
            "layer": classify(name),
            "unclassified": not any(p.search(name) for _, p in LAYER_RULES),
            "x": 0, "y": 0,
        })
    layout(nodes)
    clipped = outside_map(nodes)
    if clipped:
        raise SystemExit(
            "refusing to write a cartridge with nodes outside the viewer's map area, "
            "where they would be silently invisible: " + ", ".join(clipped))

    edges = []
    for source_repo in names:
        for target, count in sorted(found[source_repo].items()):
            match = next((n for n in names if n.lower() == target), None)
            if match is None:
                continue
            edges.append({
                "from": source_repo, "to": match, "type": "reference",
                "references": count,
                "evidence": f"{count} URL reference(s) to {ORG}/{match} found in {source_repo}'s committed text",
            })

    referenced = {e["to"] for e in edges} | {e["from"] for e in edges}
    unconnected = [n for n in names if n not in referenced]

    return {
        "schemaVersion": SCHEMA_VERSION,
        "species": "spider_printer_v1",
        "title": "GlobalGrid2050 Elements",
        "methodState": "screening",
        "mapType": "topological_sld_not_geospatial",
        "generated": {
            "utc": datetime.now(timezone.utc).isoformat(timespec="seconds"),
            "by": "spider_printer_v1/tools/build_topology.py",
            "source": source,
            "edgeRule": (
                "An edge is a URL reference to another repository found in committed text. "
                "Nothing is inferred: no import parsing, no guessing, and no edge added to make "
                "the picture look connected."
            ),
        },
        "canvas": canvas,
        "principles": [
            "Nodes are the repositories that exist, not the ones that were drawn once.",
            "An edge is evidence somebody wrote a link, with the count of times they wrote it.",
            "Positions are computed from the layer assignment, so a diff means the estate moved.",
            "A repository with no found references is drawn unconnected, because that is the evidence.",
            "Every node is laid out inside the viewer's map area; the generator refuses to "
            "publish one the viewer would clip.",
            "Screening-grade topology. It does not certify that the federation is correct.",
        ],
        "layers": [
            {"id": layer, **LAYER_META[layer],
             "count": sum(1 for n in nodes if n["layer"] == layer)}
            for layer in LAYER_ORDER
        ] + [{"id": "reference", "label": "Reference edges", "colour": "#61d7ff",
              "kind": "edge", "defaultOn": True,
              "count": len(edges)}],
        "edgeTypes": {
            "reference": {
                "label": "URL reference in committed text",
                "colour": "#61d7ff",
                "layer": "reference",
                "dash": "",
            }
        },
        "totals": {
            "nodes": len(nodes), "edges": len(edges),
            "unconnected": len(unconnected), "unconnectedIds": unconnected,
            "totalReferences": sum(e["references"] for e in edges),
            "overlappingPairs": overlapping(nodes),
        },
        "nodes": nodes,
        "edges": edges,
    }


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--local", metavar="DIR", help="scan sibling clones under DIR")
    parser.add_argument("--api", action="store_true", help="scan through the GitHub API")
    parser.add_argument("--out", default="data/topology.json")
    parser.add_argument("--check", action="store_true",
                        help="build and compare against --out; exit 1 if it differs")
    args = parser.parse_args()

    if not args.local and not args.api:
        parser.error("choose --local <dir> or --api")

    repo_meta: dict[str, dict] = {}
    if args.api:
        token = os.environ.get("GITHUB_TOKEN")
        repos = list_repos_api(token)
        names = sorted(r["name"] for r in repos)
        repo_meta = {r["name"]: r for r in repos}
        print(f"{len(names)} repositories from the {ORG} organisation")
        found = scan_api(repos, names, token)
        source = f"GitHub API, {ORG} organisation, {len(names)} repositories"
    else:
        root = Path(args.local).resolve()
        clones = sorted(p.name for p in root.iterdir()
                        if p.is_dir() and (p / ".git").exists())
        # A directory is not a repository. Several clones on this machine are
        # extra working copies of the SAME remote -- gridatlas-main-<stamp>,
        # pipelinenews-codex-<stamp> and so on. Counting them as separate nodes
        # would draw an estate a third larger than the one that exists, so the
        # remote decides identity and the clone whose directory name matches it
        # wins.
        by_remote: dict[str, str] = {}
        skipped: list[str] = []
        for clone in clones:
            try:
                url = subprocess.check_output(
                    ["git", "-C", str(root / clone), "remote", "get-url", "origin"],
                    text=True, stderr=subprocess.DEVNULL).strip()
            except Exception:                         # noqa: BLE001
                skipped.append(clone)
                continue
            match = re.search(r"[/:]" + ORG + r"/([^/]+?)(?:\.git)?$", url, re.IGNORECASE)
            if not match:
                skipped.append(clone)
                continue
            canonical = match.group(1)
            if canonical not in by_remote or clone == canonical:
                by_remote[canonical] = clone
        names = sorted(by_remote)
        duplicates = [c for c in clones if c not in by_remote.values() and c not in skipped]
        print(f"{len(clones)} directories under {root}: {len(names)} distinct repositories, "
              f"{len(duplicates)} extra working copies, {len(skipped)} without a {ORG} remote")
        if duplicates:
            print("  extra working copies (not drawn): " + ", ".join(sorted(duplicates)))
        if skipped:
            print("  no " + ORG + " remote (not drawn): " + ", ".join(sorted(skipped)))
        for name in names:
            repo_meta[name] = {"description": "", "clone": by_remote[name]}
        found = scan_local(root, names, {n: by_remote[n] for n in names})
        source = (f"local clones under {root.name}: {len(names)} distinct repositories "
                  f"deduplicated from {len(clones)} directories by origin remote")

    topology = build(found, repo_meta, source)
    text = json.dumps(topology, indent=2, ensure_ascii=False) + "\n"

    out = Path(args.out)
    if args.check:
        if not out.exists():
            print(f"{out} does not exist", file=sys.stderr)
            return 1
        current = json.loads(out.read_text(encoding="utf-8"))
        fresh = json.loads(text)
        for document in (current, fresh):
            document.pop("generated", None)
        if current != fresh:
            print("topology.json is out of date with the estate", file=sys.stderr)
            return 1
        print("topology.json matches the estate")
        return 0

    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(text, encoding="utf-8")
    t = topology["totals"]
    print(f"wrote {out}: {t['nodes']} nodes, {t['edges']} edges, "
          f"{t['totalReferences']} references, {t['unconnected']} unconnected, "
          f"{t['overlappingPairs']} overlapping pair(s)")
    if t["unconnectedIds"]:
        print("  unconnected: " + ", ".join(t["unconnectedIds"]))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())