Skip to content

A code read can name a branch as repo@branch - #1977

Merged
zlareb1 merged 12 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/1680-03-repo-at-branch
Dec 4, 2026
Merged

zlareb1 merged 12 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/1680-03-repo-at-branch

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

A code call can now name a branch as <repository or namespace>@<branch> in namespace (ClickHouse/ClickHouse@26.4), over HTTP, MCP and the SDKs alike, and it runs on the namespace that indexes that branch. A branch the repository does not index answers UNKNOWN_BRANCH with the branches the caller can read. The documents namespace and the release set now read a repository's entries from one helper, repo_members, which groups them as before.

Related: #1680

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A code call can name a branch as repository@branch in namespace on every surface; a branch that is not indexed, or that the caller cannot read, answers UNKNOWN_BRANCH and lists the branches the caller can read.

Why

A repository with several indexed branches is addressed by the namespace of each branch, and a caller has to know those names. Over MCP, server/transport/namespace_resolve.py already resolves a repository's name to its default branch's namespace, but only there: mcp_auth and mcp_outcome import it, and HTTP and the SDKs take namespace as given. A caller of any other surface, or one that wants a release line, had no way to say "this repository, that branch".

This change gives the name a spelling, <repository or namespace>@<branch>, and resolves it where both transports share their admission sequence, before validation and before the gate:

  1. namespace_resolve.resolve_branch finds the repository the left part names (a repository, its name, or any namespace of it), lists its members, and returns the namespace whose branch is the right part. It reads only what the caller can read: the branches of the repository the caller can see are the ones it matches against and the ones an error names.
  2. gates.with_branch_name runs it for a code.* op's namespace and rewrites the argument. HTTP calls it before _validate_payload; MCP calls it from _named_input (every tool, a call the screen did not resolve) and from the pre-dispatch screen (for a loom_code_* tool, ahead of the read check that would otherwise refuse the literal name).
  3. The resolved name replaces the written one in what the rest of the call reads: HTTP's raw body, as _resolve_personal writes @me's namespace, and MCP's arguments, as the screen writes its own resolution. The request identity, the ledger row and the gates after validation name the namespace the call runs on, so the call counts in that namespace's activity and usage, and an erasure of the namespace finds its rows.
  4. The gate is unchanged. It authorizes the namespace the name resolved to, as it authorizes a namespace named directly. No identity field is added.
  5. A branch the repository does not index, or that the caller cannot read, raises UnknownBranchError (UNKNOWN_BRANCH, 404) naming the readable branches. The two cases answer alike, so the error confirms no branch the caller cannot read. When the caller reads no branch of the repository, nothing resolves and the gate refuses the name as it would any ungranted namespace.
  6. A read check that could not run (server.auth.fail_closed) leaves the branch unknown, so it is never answered as one that is not there. The resolver reads each check's unverified flag, as the screen's repository resolver does. When the branch asked for was not admitted and any check could not run, the call is refused as the gate refuses its own failed check on that transport, and the row is the server's failure.

@ is not a namespace character: validate_namespace_name accepts [a-z0-9][a-z0-9-]* (_NAMESPACE_RE in server/core/naming.py), so a name made through it never collides with the form. An operator entry (LOOM_CODE_REPOS_JSON) is not checked against that rule, so the resolver also leaves any configured namespace alone before it reads a name as a branch.

Two places listed a repository's entries each their own way: docs_namespaces (server/code/domain/index/repos.py) for the documents namespace, and release_namespaces (server/code/app/backport.py) for the release set, whose grouping backport.branch_refs repeated. They are replaced by repo_members(org, repo), which lists the entries of one org whose repo spells the repository, in the Repositories page's branch order. The first member is the trunk. Their callers read it: repo_scope, memory_links, source_sync, code.repos and backport_gap. It groups as they did, by repo as written, so an entry that fetches the repository from another remote (a fork) stays one of its branches. The history reads (blame_range, first_build) keep their own grouping, the entries on one clone (repo_siblings), because they resolve refs in that clone.

docs/architecture/code-index-architecture.md now states the trunk's role as it is: its namespace holds the repository's documents.

Scope and risk

Touches the transport admission sequence, the name resolver, the list of a repository's entries and the places that read it. It adds no op, field, setting, table or migration.

Every configuration groups as before: the documents namespace and the release set by the spelling of repo, the history reads by clone. The release set's order for two branches of the same version follows the page's branch order instead of the order of the configuration.

Authorization: unchanged. The resolver reads check_authorized with VIEWER, the check the MCP screen already uses for the same purpose, and the gate runs again on the resolved namespace. A registry that has not been read resolves nothing, so the gate refuses the literal name. A check that could not run is the server's failure on both transports, never an unknown branch.

Compatibility, rollout and rollback

  • Wire: one error code, UNKNOWN_BRANCH (HTTP 404, with repository, branch and branches beside message). It is in docs/reference/errors.md and the Ruby SDK's generated errors; the SDKs map it to NotFound by status. No op or field changes.
  • Previous builds: on MCP the previous build's match finds no namespace for the form and answers UNKNOWN_NAMESPACE; on HTTP its gate refuses the ungranted literal. Neither runs the call on a wrong namespace.
  • Operator steps: none.
  • Rollback: reverting leaves nothing behind.

Tests

Red on main, then green with the change. The new files are tests/unit/test_repo_at_branch.py and tests/integration/test_repo_at_branch_ledger_live_ch.py. On main 33 of the unit file's 37 tests fail, and the integration test fails; on the change all pass. The four that pass on main hold what main already does: the documents namespace and the release set as the replaced derivations answered, the history reads' siblings, and a name that is no branch.

  • Resolver cases: a repository, its name or any namespace of it with a branch, in the page's order; padding; a name that is not the form (kj, @me, kj@, an unknown repository, a memory namespace); another org's entries; a configured namespace that holds @; a check that could not run, for the branch asked for or another; a registry not yet read.
  • Both transports through one probe op: HTTP and MCP authorize and run on the resolved namespace; the ledger rows both write name it, and an erasure export of the namespace finds them on live ClickHouse while one of the written name finds none; the gate still refuses a caller it does not admit, on the resolved namespace; an unreadable or unindexed branch is refused before the gate with the same body and the same ledger row on both, and the readable branches only; a check that could not run is refused as the gate's own failed check, its rows the server's; a name that is no branch reaches the gate as given.
  • The MCP screen rewrites the argument and notes namespace_resolved; answers UNKNOWN_BRANCH for an unreadable or an unindexed branch; refuses a check that could not run as the server's failure; leaves a tracker tool alone.
  • repo_members: order, org, the spelling of repo, and the snapshot argument; an entry on a fork remote stays in the repository's trunk, release set and branch names.
  • Callers unchanged: the documents namespace (repo_scope, memory_links) and the release set (backport) answer as the two replaced derivations did, over one configuration of several repositories, orgs, spellings, remotes and branch kinds; the previous derivations are in the test as the reference. code.blame_range and code.first_build hand their history reads the entries on their own clone, in the configuration's order.

The existing tests that patched repos.docs_namespaces or called release_namespaces read the new seams (repo_members, resolve_branches); their assertions are unchanged. The source-level order guard in test_mcp_session_runs.py looks for _named_input(, which now precedes the gate where _validated_input( did, and test_sdk_errors_from_response.py lists the new code among the server's.

tests/unit/test_repo_at_branch.py                       # main: 33 failed, 4 passed; change: 37 passed
tests/integration/test_repo_at_branch_ledger_live_ch.py # main: 1 failed; change: 1 passed

Also run: ruff format --check and ruff check, function_length_gate, check_no_secrets, mypy_gate, every generator's --check with the CI toolchains (sdk_py_gen, sdk_py_client_gen, sdk_py_admin_gen, sdk_ts_gen, sdk_rb_gen, sdk_rust_gen with and without --fixtures, sdk_go_gen, agent_plugin_sync, standalone_skills, served_skills, settings_reference, settings_schema, settings_example, docs_reference, op_reach_census), the unit tests of the transports, the resolver, blame, first build, bisect, backport, mirrors, the repository registry and its access rules, memory code links and doc links, and the integration tests that read those paths.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code; I read the diff and ran the tests above.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: docs/reference/errors.md and the Ruby SDK's errors.rb; no op, setting or plugin file changed.
  • A new migration is additive and numbered after the latest on main: not applicable, none added.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version: not applicable, none changed.
  • The frontend builds and its tests pass, if frontend/ changed: not applicable, unchanged.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature.

Note

Medium Risk
Changes shared transport admission and namespace resolution before authorization; auth rules are unchanged but mis-resolution could route calls to the wrong index without the new tests and fail-closed paths.

Overview
Code calls can target a specific indexed branch by setting namespace to <repository or namespace>@<branch> on HTTP, MCP, and the SDKs. Admission resolves that form to the branch’s code namespace before validation and authorization (resolve_branch, gates.with_branch_name), so ledger activity and erasure use the resolved namespace—not the literal string.

Unknown or unreadable branches return UNKNOWN_BRANCH (404) with the branches the caller may read; failed read checks stay fail-closed (gate refusal, not a branch list).

Repository grouping is consolidated: repo_members replaces separate docs_namespaces / release_namespaces logic for trunk docs, release sets, listings, and backport—behavior is preserved with tests against the old derivations.

Reviewed by Cursor Bugbot for commit 86eb526. Bugbot is set up for automated code reviews on this repo. Configure here.

KaloyanNaumov and others added 6 commits December 1, 2026 23:02
A code call's `namespace` can be `<repository or namespace>@<branch>` on HTTP, MCP and
the SDKs alike. The name resolves in the shared admission sequence, before validation
and the gate, to the namespace that indexes that branch, and the gate authorizes that
namespace exactly as it authorizes one named directly. A branch the repository does not
index, or the caller cannot read, answers UNKNOWN_BRANCH (404) naming the readable
branches. `@` is not a namespace character (`server/core/naming.py`), so the form never
collides with a name.

`repo_members(org, repo_key)` replaces `docs_namespaces`, `repo_siblings` and
`release_namespaces`: one list of a repository's entries in the Repositories page's
branch order, the trunk first. The architecture page states the trunk's role.

Related: ClickHouse#1680

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Both transports resolved the branch name into a copy of the arguments, so
the request identity, the ledger rows and the gates that read the raw body
named the text the caller wrote instead of the namespace that served the
call. Those rows fell outside the namespace's activity, usage and erasure.

HTTP now writes the resolved name into the raw body, as `@me` is written,
and MCP writes it into the call's arguments, as the screen does. A unit
test reads the rows both transports hand the event writer, and an
integration test finds them with an erasure export of the namespace.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ilure

The resolver kept only whether each branch's read check allowed it, so a
check refused because a read it needed failed made a branch the caller
may read look absent: the call answered UNKNOWN_BRANCH, the caller's
error, and dropped that branch from the list it named.

The resolver now reads the check's `unverified` flag, as the screen's
repository resolver does. When the branch asked for was not admitted and
any check could not run, it raises the gate's refusal marked unverified
and notes the request as the server's; each transport answers it as its
gate answers a failed check.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`repo_members` grouped entries by `remote.repo_key`, which lets a URL
remote decide the key. An entry that names the repository as the others
do but fetches it from a fork became a repository of its own: its own
trunk, documents and decisions namespace, and a release set without it.
The documents namespace and the release set grouped by the spelling of
`repo`, so a configuration with such an entry changed meaning.

`repo_members(org, repo)` now groups by `repo` as written, the grouping
those derivations had, and a test holds a fork-remote entry to the
repository's trunk, release set and branch names.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`code.blame_range` and `code.first_build` took their siblings from
`repo_members`, which lists every entry of the repository. An entry an
operator gave its own clone then became a sibling of the others: the
indexed view tested its head against this clone, and a release ref
resolved to its head was read in a clone that may not hold it.

The history reads resolve refs in one clone, so their siblings are the
entries on that clone again (`repo_siblings`), and bisect reads its entry
as before. A test reads the siblings both ops hand their history reads
for a configuration with a branch on a clone of its own.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The entries these tests configure gained a `git_remote` field when the
repository's members were keyed on the remote. They are grouped by
`repo` alone now, which the stubs always had, so the field goes.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 3, 2026 7:43pm UTC
loom-telemetry Ready Ready Preview Dec 3, 2026 7:43pm UTC

Request Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/transport/mcp_auth.py Resolved
@zlareb1 zlareb1 self-assigned this Dec 2, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An omitted org can resolve repo@branch over HTTP and fail over MCP. The existing finding follows from the MCP screen using the token org and the HTTP resolver preferring the code-plane org. Use the same effective org for branch resolution and the subsequent read check. Cover an omitted org with a configured code-plane org on both transports.

A code call that names no org acts in the code plane's org on HTTP, but the MCP
screen resolved `repo@branch` and read the namespace in the token's org. With a
token org that is not the code plane (dev mode, federated login) the same call
answered on HTTP and was refused on MCP.

`gates.org_for_op` is the one derivation of the org a call acts in, and
`resolve_org` is now its op form. The MCP screen resolves the branch in that org
and makes the read check on the resolved namespace in it.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Round 2

Both transports now pick one effective org for branch resolution and the read check that follows, so an omitted org with a configured code-plane org resolves the same namespace on HTTP and MCP (a02e246).

zlareb1
zlareb1 previously approved these changes Dec 2, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch resolver and the MCP namespace screen now use the same effective organization as HTTP. All 226 focused transport, branch, backport, and SDK error tests passed.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The analyzer job reports the same mentor evidence shape mismatch on this head's tested base. The focused test passes on current main with the snapshot update from #1957. The existing digest repair in #1990 addresses the SQL comment that made the literal affect the shape. Failed CI job · Digest repair

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub reports a merge conflict in sdk/ruby/lib/clickhouse/loom/generated/errors.rb. Please reconcile the schema changes with main and regenerate the Ruby error definitions so CI can test the combined tree.

The Ruby error definitions are regenerated from the combined tree with
scripts/codegen/sdk_rb_gen.py, so they carry main's codes and
UNKNOWN_BRANCH.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Round

Merged main in 2e688c9. sdk/ruby/lib/clickhouse/loom/generated/errors.rb is regenerated from the combined tree with scripts/codegen/sdk_rb_gen.py, not merged by hand, so it carries main's codes and UNKNOWN_BRANCH. Every generator's --check is clean on the merge, and test_repo_at_branch.py, test_sdk_errors_from_response.py, test_error_envelope.py and tests/integration/test_repo_at_branch_ledger_live_ch.py pass. Ready for another look.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 2e688c9. Configure here.

Comment thread server/transport/namespace_resolve.py Resolved
`resolve_branch` finds the repository by its normalized spelling, but
`repo_members` compares `repo` exactly, so a repository configured under
two spellings (`Acme/kj` and `acme/kj`) hid the other group's branches
and answered UNKNOWN_BRANCH. The resolver now lists the entries of every
spelling that normalizes to the repository's, its own spelling first.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mixed repository spelling finding blocks approval. The resolver matches the repository using a normalized name, then looks for branches only under the chosen entry’s exact spelling. Use the same repository identity for both steps and cover a readable release branch configured under a different spelling.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch lookup uses the same repository spelling rules as name lookup and still checks read access on each candidate. All 39 focused branch and transport tests passed.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch-resolution cases pass. CI found a type regression in the new member helper. Failed type gate

Comment thread server/transport/namespace_resolve.py Resolved

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seed 2 Undo failure also reproduced on clean main. Four fixture pairs share values, so a delete can make another key’s value ineligible for Undo. The repair seeds distinct values and passes the same test with its Undo assertion. Failed integration job · Repair #2035

`_members` returned `list[Any]`, so `hit.namespace` was `Any` and
`resolve_branch` returned it from a `str | None` signature. The mypy
ratchet caught it as `no-any-return`. Annotate the helper with the
`RepoConfig` that `repo_members` already returns, imported for typing
only so the registry stays a lazy import.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Fixed in one line: _members() returns list[RepoConfig] (typing-only import under TYPE_CHECKING), so hit.namespace stays str and resolve_branch's str | None contract holds.

Same command your CI runs — python scripts/gates/mypy_gate.py:

before: REGRESSION server/transport/namespace_resolve.py: 1 error(s) (baseline 0)
          line 171: Returning Any from function declared to return "str | None"  [no-any-return]
after:  mypy-gate: 0 error(s) in 0 file(s) (baseline 0 in 0 file(s))

The ratchet is the pin: the baseline is {} and --update only lowers it, so a list[Any] return fails again as no-any-return.

pytest tests/unit/test_namespace_resolve.py tests/unit/test_repo_at_branch.py → 81 passed; ruff check and format clean.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The member helper keeps the registry’s RepoConfig type through branch resolution, so the annotated return is a string or None. All 99 focused branch and transport tests passed.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact CI merge fails both new history cases because their mock targets a helper that the handlers no longer import. The existing blame and first-build tests pass on main. Failed unit job

Comment thread tests/unit/test_repo_at_branch.py Resolved

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pinned Biome check finds the same unformatted dashboard assertion in this run and on clean main. The separate repair formats that assertion. Failed job · Repair #2107

blame calls mirrors.history_dir and first_build calls the history_dir it
imported; the wrapper history_git_dir went out with the default-mirror
change, so both cases died at setattr before the handler ran and the
clone-isolation assertions never executed.

Patch the seam each handler reads: the mirrors module attribute for blame,
the imported name for first_build. The assertions are unchanged.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Steady point: 86eb526b6.

Both history cases now mock the seam each handler reads, on the merged tree.

main merged first (30440ee80, merge 54060cb0d, no conflicts — 9e82a7594 removed the wrapper):

  • blame — monkeypatch.setattr(blame.mirrors, "history_dir", git_dir), because handle_code_blame_range calls mirrors.history_dir.
  • first_build — monkeypatch.setattr(first_build, "history_dir", git_dir), because handle_code_first_build calls the name it imported.

The clone-isolation assertions are unchanged, and sibling selection still comes from the configuration entries (repo_siblings keys on c.git_dir).

Red, before the change: both params die at setattr with AttributeError: module ... has no attribute 'history_git_dir' — 2 failed. Control with the seam patch removed: 1 failed, so the mock is what carries the read rather than the test passing by accident.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The history tests patch the resolver used by blame and first-build and still check clone isolation. All 227 focused branch, transport, backport, and SDK tests passed.

@zlareb1
zlareb1 merged commit 9e70620 into ClickHouse:main Dec 4, 2026
81 of 82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants