sync_metadata.py

scripts/sync_metadata.py

THE LANGUAGE CLAUSE IS GENERATED, NOT MAINTAINED.

clause

str sync_metadata.clause()

The generated clause: what is harvested, and what is only named.

scripts/sync_metadata.py:59

Source · 14 lines
def clause() -> str:
    """ONE SENTENCE FOR BOTH SETS, because a reader who sees only the first concludes clew parses
    nothing else, and a reader who sees them merged concludes it parses everything listed.

    @brief Build the derived language clause.
    @return The clause.
    @version 1
    """
    return (
        f"Harvests symbols from {harvested_phrase()}; "
        f"registers {registered_phrase()} files by name without parsing them"
    )

Activity · 1 steps
lines 67–70return ( f"Harvests symbols from {harvested_phrase()}; " f"registers {registered_phrase()} files by name without parsing them" )

Flows

main

int sync_metadata.main(bool check)

Regenerate every derived metadata span.

scripts/sync_metadata.py:175

Source · 57 lines
def main(check: bool) -> int:
    """@brief Sync or check the derived metadata.
    @return 0 when in sync, 1 on drift under --check, 2 when a target could not be handled.
    @version 2
    """
    body = clause()
    table = "\n" + support_table() + "\n"
    changed: list[str] = []
    problems: list[str] = []

    ## THE README CARRIES THE TABLE, NOT THE CLAUSE. It is the page a stranger reads first and the
    ## PyPI description, and "which languages are FULLY supported" was the question it failed to
    ## answer: the flat clause put shell beside C as if "who calls this" worked for both. The other
    ## three targets cannot render a table, so they keep the clause.
    tools = tools_clause()
    spans = [
        (REPO / "pyproject.toml", f"# <{MARK}>", f"# </{MARK}>", body),
        (REPO / "README.md", f"<!-- {SUPPORT_MARK} -->", f"<!-- /{SUPPORT_MARK} -->", table),
        (REPO / "README.md", f"<!-- {TOOLS_MARK} -->", f"<!-- /{TOOLS_MARK} -->", tools),
        (REPO / "SKILLS.md", f"<!-- {TOOLS_MARK} -->", f"<!-- /{TOOLS_MARK} -->", tools),
        (REPO / "SKILLS.md", f"<!-- {MARK} -->", f"<!-- /{MARK} -->", body),
    ]
    for path, o, c, text in spans:
        did, why = replace_span(path, o, c, text, write=not check)
        ## `did` IS THE CONDITION, not `why`. The first version appended on an empty `why`, so
        ## every marker file read as drifted on every run — a check that always fails teaches the
        ## reader to ignore it, which is worse than no check.
        if why:
            problems.append(why)
        elif did:
            changed.append(str(path.relative_to(REPO)))
    for path in (REPO / ".claude-plugin/plugin.json", REPO / ".claude-plugin/marketplace.json"):
        did, why = replace_json_description(path, body, write=not check)
        if why:
            problems.append(why)
        elif did:
            changed.append(str(path.relative_to(REPO)))

    print(f"derived clause: {body}")
    ## NAMED, NEVER COUNTED AS DONE. A target this script could not handle is a target whose
    ## metadata is unmanaged, which is indistinguishable from in-sync unless it is printed.
    for why in problems:
        print(f"  UNHANDLED  {why}")
    for name in changed:
        print(f"  {'DRIFTED' if check else 'wrote'}    {name}")
    if problems:
        return 2
    if check and changed:
        print(
            "\nThese files' language clause had fallen behind the routers. It is GENERATED: run\n"
            "  .venv/bin/python scripts/sync_metadata.py\n"
            "and commit the result. Do not hand-edit the clause — that is what went stale."
        )
        return 1
    return 0

Activity · 29 steps
lines 180–180body = clause()lines 181–181table = "\n" + support_table() + "\n"lines 182–182changed: list[str] = []lines 183–183problems: list[str] = []lines 189–189tools = tools_clause()lines 190–196spans = [ (REPO / "pyproject.toml", f"# <{MARK}>", f"# </{MARK}>", body), (REPO / "README.md", f"<!-- {SUPPORT_MARK} -->", f"<!-- /{SUPPORT_MARK} -->", table), (REPO / "README.md", f"<!-- {TOOLS_MARK} -->", f"<!-- /{TOOLS_MARK} -->", tools), (REPO / "SKILLS.md", f"<!-- {TOOLS_MARK} -->", f"<!-- /{TOOLS_MARK} -->", tools), (REPO / "SKILLS.md", f"<!-- {MARK} -->", f"<!-- /{MARK} -->", body), ]lines 197–197◇ for path, o, c, text in spanslines 198–198did, why = replace_span(path, o, c, text, write=not check)lines 202–202◇ if whylines 203–203problems.append(why)lines 204–204◇ elif didlines 205–205changed.append(str(path.relative_to(REPO)))lines 206–206◇ for path in (REPO / ".claude-plugin/plugin.json", REPO / ".claude-plugin/marketplace.json")lines 207–207did, why = replace_json_description(path, body, write=not check)lines 208–208◇ if whylines 209–209problems.append(why)lines 210–210◇ elif didlines 211–211changed.append(str(path.relative_to(REPO)))lines 213–213print(f"derived clause: {body}")lines 216–216◇ for why in problemslines 217–217print(f" UNHANDLED {why}")lines 218–218◇ for name in changedlines 219–219print(f" {'DRIFTED' if check else 'wrote'} {name}")lines 220–220◇ if problemslines 221–221return 2lines 222–222◇ if check and changedlines 223–227print( "\nThese files' language clause had fallen behind the routers. It is GENERATED: run\n" " .venv/bin/python scripts/sync_metadata.py\n" "and commit the result. Do not hand-edit the clause — that is what went stale." )lines 228–228return 1lines 229–229return 0

Flows

replace_json_description

tuple[bool, str] sync_metadata.replace_json_description(Path path, str body, *bool write=True)

Rewrite the description of a JSON manifest's own plugin entry.

scripts/sync_metadata.py:116

Show on the map →

Called by main

Calls none

Source · 32 lines
def replace_json_description(path: Path, body: str, *, write: bool = True) -> tuple[bool, str]:
    """JSON CARRIES NO COMMENTS, so there is nowhere to put a marker. The sentence is identified
    by its own shape instead: the clause is the LAST sentence of `description`, and it is replaced
    wholesale. Identified by the generated prefix rather than by position, so a description a
    human reorders still has its clause found.

    @brief Rewrite a manifest description's trailing generated clause.
    @return Whether the file changed, and why it could not be handled.
    @version 1
    """
    if not path.is_file():
        return False, f"{path}: not found"
    data = json.loads(path.read_text("utf-8"))
    entries = data.get("plugins") if "plugins" in data else [data]
    changed = False
    for entry in entries:
        desc = entry.get("description")
        if not isinstance(desc, str):
            return False, f"{path}: entry has no description"
        stripped = re.sub(r"\s*Harvests symbols from .*?without parsing them\.", "", desc)
        want = f"{stripped.rstrip()} {body}."
        if want != desc:
            entry["description"] = want
            changed = True
    if changed and write:
        path.write_text(json.dumps(data, indent=2) + "\n", "utf-8")
    return changed, ""


# @brief The served surface as one clause: the tools, then the prompts, then the wiki resources.
# @return The clause, no trailing full stop.
# @version 1
Activity · 17 steps
lines 126–126◇ if not path.is_file()lines 127–127return False, f"{path}: not found"lines 128–128data = json.loads(path.read_text("utf-8"))lines 129–129entries = data.get("plugins") if "plugins" in data else [data]lines 130–130changed = Falselines 131–131◇ for entry in entrieslines 132–132desc = entry.get("description")lines 133–133◇ if not isinstance(desc, str)lines 134–134return False, f"{path}: entry has no description"lines 135–135stripped = re.sub(r"\s*Harvests symbols from .*?without parsing them\.", "", desc)lines 136–136want = f"{stripped.rstrip()} {body}."lines 137–137◇ if want != desclines 138–138entry["description"] = wantlines 139–139changed = Truelines 140–140◇ if changed and writelines 141–141path.write_text(json.dumps(data, indent=2) + "\n", "utf-8")lines 142–142return changed, ""

Flows

replace_span

tuple[bool, str] sync_metadata.replace_span(Path path, str open_mark, str close_mark, str body, *bool write=True)

Replace a marker-delimited span in one file.

scripts/sync_metadata.py:82

Show on the map →

Called by main

Calls none

Source · 27 lines
def replace_span(
    path: Path, open_mark: str, close_mark: str, body: str, *, write: bool = True
) -> tuple[bool, str]:
    """REPORTS A MISSING MARKER RATHER THAN SKIPPING IT. A generator that quietly covers three of
    five files is the failure this whole script exists to remove, one level up.

    @brief Rewrite one marker span.
    @return Whether the file changed, and why it could not be handled.
    @version 1
    """
    if not path.is_file():
        return False, f"{path}: not found"
    text = path.read_text("utf-8")
    pattern = re.compile(re.escape(open_mark) + r".*?" + re.escape(close_mark), re.S)
    if not pattern.search(text):
        return False, f"{path}: no {MARK} marker"
    new = pattern.sub(f"{open_mark}{body}{close_mark}", text)
    if new == text:
        return False, ""
    ## `--check` MUST NOT MUTATE THE TREE. A gate that fixes what it is checking reports success
    ## on the second run and leaves an uncommitted edit behind — which is how a "green" check
    ## comes to mean "I changed it for you".
    if write:
        path.write_text(new, "utf-8")
    return True, ""

Activity · 12 steps
lines 92–92◇ if not path.is_file()lines 93–93return False, f"{path}: not found"lines 94–94text = path.read_text("utf-8")lines 95–95pattern = re.compile(re.escape(open_mark) + r".*?" + re.escape(close_mark), re.S)lines 96–96◇ if not pattern.search(text)lines 97–97return False, f"{path}: no {MARK} marker"lines 98–98new = pattern.sub(f"{open_mark}{body}{close_mark}", text)lines 99–99◇ if new == textlines 100–100return False, ""lines 104–104◇ if writelines 105–105path.write_text(new, "utf-8")lines 106–106return True, ""

Flows

tools_clause

str sync_metadata.tools_clause()

Build the tool clause.

scripts/sync_metadata.py:148

Source · 22 lines
def tools_clause() -> str:
    """FROM THE DESCRIPTION FILES THE SERVER LOADS (tier 1, then tier 0, then the tier-2 prompts),
    in the order the server registers them — never a list a person keeps.

    @brief Build the tool clause.
    @return The clause.
    @version 1
    """
    from clew.mcp_server.descriptions import load_descriptions

    tools = [*load_descriptions(tier=1), *load_descriptions(tier=0)]
    prompts = list(load_descriptions(tier=2))
    named = ", ".join(f"`{t}`" for t in tools[:-1]) + f" and `{tools[-1]}`"
    words = _NUMBER.get(len(tools), str(len(tools)))
    asked = " and ".join(f"`{p}`" for p in prompts)
    return (
        f"{words} tools — {named} — plus the {asked} prompt{'s' if len(prompts) > 1 else ''} "
        "and the wiki as MCP resources (`clew://wiki/{slug}/llms.txt`, "
        "`clew://wiki/{slug}/flow/{name}`)"
    )

Activity · 7 steps
lines 156–156from clew.mcp_server.descriptions import load_descriptionslines 158–158tools = [*load_descriptions(tier=1), *load_descriptions(tier=0)]lines 159–159prompts = list(load_descriptions(tier=2))lines 160–160named = ", ".join(f"`{t}`" for t in tools[:-1]) + f" and `{tools[-1]}`"lines 161–161words = _NUMBER.get(len(tools), str(len(tools)))lines 162–162asked = " and ".join(f"`{p}`" for p in prompts)lines 163–167return ( f"{words} tools — {named} — plus the {asked} prompt{'s' if len(prompts) > 1 else ''} " "and the wiki as MCP resources (`clew://wiki/{slug}/llms.txt`, " "`clew://wiki/{slug}/flow/{name}`)" )

Flows