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.
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
Flows
main
int sync_metadata.main(bool check)
Regenerate every derived metadata span.
Called by none
Calls clause, replace_json_description, replace_span, support_table, tools_clause
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
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.
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 1Activity · 17 steps
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.
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
Flows
tools_clause
str sync_metadata.tools_clause()
Build the tool clause.
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}`)"
)