routing.py

clew/mcp_server/routing.py

Which index of a repository answers, and what to say when none can (gh#48).

built

tuple[Target, ...] clew.mcp_server.routing.RepoIndexes.built(self)

The sub-indexes whose database exists right now.

clew/mcp_server/routing.py:63

Show on the map →

Called by __call__, not_searched

Calls none

Source · 4 lines
    def built(self) -> tuple[Target, ...]:
        """@brief Built sub-indexes only. @version 1"""
        return tuple(t for t in self.sub_indexes if Path(t.db_path).is_file())
Activity · 1 steps
lines 65–65return tuple(t for t in self.sub_indexes if Path(t.db_path).is_file())

Requirements

REQ-DDB-MCP-001

listed_names

str clew.mcp_server.routing.listed_names(tuple[str,...]|list[str] names)

Render a bounded list of sub-index names for a sentence.

clew/mcp_server/routing.py:190

Source · 7 lines
def listed_names(names: tuple[str, ...] | list[str]) -> str:
    """@brief Bounded name list. @return The rendered list. @version 1"""
    shown = ", ".join(repr(n) for n in names[:MAX_LISTED_SIBLINGS])
    extra = len(names) - MAX_LISTED_SIBLINGS
    return shown + (f" (+{extra} more — index(action='targets') lists them)" if extra > 0 else "")

Activity · 3 steps
lines 192–192shown = ", ".join(repr(n) for n in names[:MAX_LISTED_SIBLINGS])lines 193–193extra = len(names) - MAX_LISTED_SIBLINGSlines 194–194return shown + (f" (+{extra} more — index(action='targets') lists them)" if extra > 0 else "")

Flows

no_index_message

str clew.mcp_server.routing.no_index_message(str what, str remedy)

The sentence every unbuilt-index refusal opens with.

clew/mcp_server/routing.py:203

Source · 16 lines
def no_index_message(what: str, remedy: str) -> str:
    """ONE WORDING FOR EVERY SHAPE THAT CAN REFUSE. There were three; gh#48 folded the two server
    paths into `unbuilt_refusal`, and this is the third — `QueryTools` bound to a single database,
    which has no registry to ask about sub-indexes and so cannot use the full refusal. It can
    still say the same sentence.

    @brief The shared "no index yet" sentence.
    @return The sentence.
    @version 1
    """
    return (
        f"No index has been built for {what} yet — call {remedy} first. Nothing is wrong with "
        f"this repository; it has simply not been indexed."
    )

Activity · 1 steps
lines 213–216return ( f"No index has been built for {what} yet — call {remedy} first. Nothing is wrong with " f"this repository; it has simply not been indexed." )

Requirements

REQ-DDB-MCP-003

Flows

not_searched

tuple[str, ...] clew.mcp_server.routing.RepoIndexes.not_searched(self, Target answering)

Names of the other parts of this repository a reply from answering did not read.

clew/mcp_server/routing.py:125

Show on the map →

Called by none

Calls built, list

Source · 22 lines
    def not_searched(self, answering: Target) -> tuple[str, ...]:
        """A WHOLE-REPOSITORY ANSWER SEARCHED EVERYTHING, so it lists nothing even when sub-indexes
        also exist beside it. Only a sub-index answer is partial.

        UNBUILT PARTS COUNT. Measured end to end: a background first build of a split repository
        builds first-party only, so no vendored part is built — and a miss for a vendored function
        was worded "a definitive negative" because nothing was listed. The vendored tree is known
        (the build recorded its derived name); that it has no index yet makes the answer MORE
        partial, not less. Built names come first because those are queryable now.

        @brief Other parts left unread by a sub-index answer.
        @return Names in listing order.
        @version 1
        """
        if answering.name is None:
            return ()
        built = [str(t.name) for t in self.built() if t.name is not None]
        known = [str(t.name) for t in self.sub_indexes if t.name is not None] + list(self.derived)
        ordered = built + sorted(set(known) - set(built))
        return tuple(dict.fromkeys(n for n in ordered if n != answering.name))

Activity · 6 steps
lines 139–139◇ if answering.name is Nonelines 140–140return ()lines 141–141built = [str(t.name) for t in self.built() if t.name is not None]lines 142–142known = [str(t.name) for t in self.sub_indexes if t.name is not None] + list(self.derived)lines 143–143ordered = built + sorted(set(known) - set(built))lines 144–144return tuple(dict.fromkeys(n for n in ordered if n != answering.name))

Requirements

REQ-DDB-MCP-001

preferred

Target | None clew.mcp_server.routing.RepoIndexes.preferred(self)

The Target a call naming no sub_index answers from.

clew/mcp_server/routing.py:100

Show on the map →

Called by none

Calls whole_built

Source · 20 lines
    def preferred(self) -> Target | None:
        """BUILT BEATS REGISTERED, then WHOLE BEATS FIRST-PARTY. The old rule preferred a
        REGISTERED whole-repository record whether or not it had a database — and following the
        refusal's own advice is what registers one: `index(action='refresh', target=repo)`
        registers the whole target before building it, so a build killed at 900 s left a record
        that outranked a perfectly good first-party index for every later call.

        @brief Choose the default index for a bare call.
        @return Whole if built, else first-party if built, else whichever is registered, else None.
        @version 1
        """
        first_party = next((t for t in self.sub_indexes if t.name == FIRST_PARTY_INDEX), None)
        ranked = (
            (self.whole_built(), self.whole),
            (first_party is not None and Path(first_party.db_path).is_file(), first_party),
            (self.whole_registered, self.whole),
            (first_party is not None, first_party),
        )
        return next((target for eligible, target in ranked if eligible), None)
Activity · 3 steps
lines 111–111first_party = next((t for t in self.sub_indexes if t.name == FIRST_PARTY_INDEX), None)lines 112–117ranked = ( (self.whole_built(), self.whole), (first_party is not None and Path(first_party.db_path).is_file(), first_party), (self.whole_registered, self.whole), (first_party is not None, first_party), )lines 118–118return next((target for eligible, target in ranked if eligible), None)

Requirements

REQ-DDB-MCP-001

repo_indexes

RepoIndexes clew.mcp_server.routing.repo_indexes(TargetRegistry registry, str repo_path)

Collect every known index of one repository without walking its tree.

clew/mcp_server/routing.py:153

Source · 32 lines
def repo_indexes(registry: TargetRegistry, repo_path: str) -> RepoIndexes:
    """REGISTERED NAMES WIN OVER DIRECTORY SUFFIXES. `_safe_index_name` truncates at 48
    characters, so a deep vendored name's directory is not always its spelling — and the record a
    build wrote holds the name it was given. A database with no record is named by its suffix,
    which `target_for` maps back to the same directory, so it is still addressable.

    @brief Registered and on-disk indexes of one repository.
    @return The collected RepoIndexes.
    @version 1
    """
    whole = target_for(repo_path, registry.home)
    records = [t for t in registry.targets() if t.repo_path == whole.repo_path]
    by_slug = {t.slug: t for t in records if t.name is not None}
    prefix = f"{whole.slug}."
    targets_dir = registry.home / "targets"
    if targets_dir.is_dir():
        for child in sorted(targets_dir.iterdir()):
            if child.name.startswith(prefix) and child.name not in by_slug:
                if (child / DB_NAME).is_file():
                    by_slug[child.name] = target_for(
                        whole.repo_path, registry.home, child.name[len(prefix) :]
                    )
    ordered = sorted(by_slug.values(), key=lambda t: (t.name != FIRST_PARTY_INDEX, str(t.name)))
    return RepoIndexes(
        repo_path=whole.repo_path,
        whole=whole,
        whole_registered=any(t.name is None for t in records),
        sub_indexes=tuple(ordered),
        derived=registry.derived_names(whole.repo_path),
    )

Activity · 12 steps
lines 163–163whole = target_for(repo_path, registry.home)lines 164–164records = [t for t in registry.targets() if t.repo_path == whole.repo_path]lines 165–165by_slug = {t.slug: t for t in records if t.name is not None}lines 166–166prefix = f"{whole.slug}."lines 167–167targets_dir = registry.home / "targets"lines 168–168◇ if targets_dir.is_dir()lines 169–169◇ for child in sorted(targets_dir.iterdir())lines 170–170◇ if child.name.startswith(prefix) and child.name not in by_sluglines 171–171◇ if (child / DB_NAME).is_file()lines 172–174by_slug[child.name] = target_for( whole.repo_path, registry.home, child.name[len(prefix) :] )lines 175–175ordered = sorted(by_slug.values(), key=lambda t: (t.name != FIRST_PARTY_INDEX, str(t.name)))lines 176–182return RepoIndexes( repo_path=whole.repo_path, whole=whole, whole_registered=any(t.name is None for t in records), sub_indexes=tuple(ordered), derived=registry.derived_names(whole.repo_path), )

Requirements

REQ-DDB-MCP-001

Flows

split_evidence

bool clew.mcp_server.routing.RepoIndexes.split_evidence(self)

Whether anything on record says this repository is split.

clew/mcp_server/routing.py:79

Show on the map →

Called by none

Calls none

Source · 17 lines
    def split_evidence(self) -> bool:
        """`.gitmodules` COUNTS FOR ADVICE, NOT FOR ROUTING. A never-built repository has no
        sub-index on record, and B12 was exactly that before its first split build — so the
        refusal that recommends a build has to know, without walking, whether an unscoped one would
        descend into vendored trees. A root `.gitmodules` is the cheap, strong signal. It never
        changes which database answers: only a database on disk does that.

        @brief Split evidence from records, recorded derivations and the root `.gitmodules`.
        @return True when the repository is known or declared to be split.
        @version 1
        """
        return (
            bool(self.sub_indexes)
            or bool(self.derived)
            or (Path(self.repo_path) / ".gitmodules").is_file()
        )
Activity · 1 steps
lines 90–94return ( bool(self.sub_indexes) or bool(self.derived) or (Path(self.repo_path) / ".gitmodules").is_file() )

Requirements

REQ-DDB-MCP-001

unbuilt_refusal

str clew.mcp_server.routing.unbuilt_refusal(RepoIndexes indexes, Target asked, str background="")

The one message for a query whose index does not exist.

clew/mcp_server/routing.py:226

Source · 48 lines
def unbuilt_refusal(indexes: RepoIndexes, asked: Target, background: str = "") -> str:
    """WRITTEN FOR A MODEL CHOOSING ITS NEXT CALL, because gh#48 recorded what the old wording
    produced in real sessions: retry the same call, run the suggested build, stall, or report the
    tool as broken. So it says, in order: what is missing, what IS queryable now with the exact
    argument, the single build that fixes it, and — on a split repository — the build NOT to run.

    `index(action='refresh', target=...)` WITHOUT `sub_index` IS NEVER ADVISED ON A SPLIT
    REPOSITORY. That is the 87,911-file build. First-party is the index a bare call defaults to, so
    it is the build that makes the refused call work.

    @brief Compose the unbuilt-index refusal.
    @return The message.
    @version 2
    """
    repo = indexes.repo_path
    built = [str(t.name) for t in indexes.built()]
    split = indexes.split_evidence()
    if asked.name is not None:
        what = f"sub-index {asked.name!r} of {repo}"
    elif split:
        what = f"{repo} (neither its first-party nor a whole-repository index)"
    else:
        what = repo
    wanted = asked.name if asked.name is not None else (FIRST_PARTY_INDEX if split else None)
    remedy = f"index(action='refresh', target={repo!r}" + (
        f", sub_index={wanted!r})" if wanted is not None else ")"
    )
    parts = [
        f"{background} Nothing is wrong with this repository; ask again once it finishes, or call "
        f"{remedy}, which waits for that same build rather than starting another."
        if background
        else no_index_message(what, remedy)
    ]
    if built:
        parts.append(f"Queryable now with the same target: sub_index= {listed_names(built)}.")
    unbuilt = [str(t.name) for t in indexes.sub_indexes if t.name not in built]
    if unbuilt:
        parts.append(f"Known but not built yet: {listed_names(unbuilt)}.")
    if split:
        parts.append(
            "This repository is split into sub-indexes (it vendors nested git trees): do NOT "
            "refresh it without sub_index, which indexes every vendored tree and can run past "
            "doxygen's time limit."
        )
    declaration = describe_declaration(Path(repo))
    if declaration:
        parts.append(declaration)
    return " ".join(parts)

1 more lines, not shown

Activity · 23 steps
lines 240–240repo = indexes.repo_pathlines 241–241built = [str(t.name) for t in indexes.built()]lines 242–242split = indexes.split_evidence()lines 243–243◇ if asked.name is not Nonelines 244–244what = f"sub-index {asked.name!r} of {repo}"lines 245–245◇ elif splitlines 246–246what = f"{repo} (neither its first-party nor a whole-repository index)"lines 247–247elselines 248–248what = repolines 249–249wanted = asked.name if asked.name is not None else (FIRST_PARTY_INDEX if split else None)lines 250–252remedy = f"index(action='refresh', target={repo!r}" + ( f", sub_index={wanted!r})" if wanted is not None else ")" )lines 253–258parts = [ f"{background} Nothing is wrong with this repository; ask again once it finishes, or call " f"{remedy}, which waits for that same build rather than starting another." if background else no_index_message(what, remedy) ]lines 259–259◇ if builtlines 260–260parts.append(f"Queryable now with the same target: sub_index= {listed_names(built)}.")lines 261–261unbuilt = [str(t.name) for t in indexes.sub_indexes if t.name not in built]lines 262–262◇ if unbuiltlines 263–263parts.append(f"Known but not built yet: {listed_names(unbuilt)}.")lines 264–264◇ if splitlines 265–269parts.append( "This repository is split into sub-indexes (it vendors nested git trees): do NOT " "refresh it without sub_index, which indexes every vendored tree and can run past " "doxygen's time limit." )lines 270–270declaration = describe_declaration(Path(repo))lines 271–271◇ if declarationlines 272–272parts.append(declaration)lines 273–273return " ".join(parts)

Requirements

REQ-DDB-MCP-001

Flows

whole_built

bool clew.mcp_server.routing.RepoIndexes.whole_built(self)

Whether the whole-repository database exists.

clew/mcp_server/routing.py:71

Show on the map →

Called by preferred

Calls none

Source · 4 lines
    def whole_built(self) -> bool:
        """@brief Whole-repository index presence. @version 1"""
        return Path(self.whole.db_path).is_file()
Activity · 1 steps
lines 73–73return Path(self.whole.db_path).is_file()

Requirements

REQ-DDB-MCP-001