Skip to content

A token mint, namespace.list_mine, memory.for_file and four code passes derive the configured repositories once - #2194

Open
zlareb1 wants to merge 8 commits into
mainfrom
fix/derive-once-remaining
Open

zlareb1 wants to merge 8 commits into
mainfrom
fix/derive-once-remaining

Conversation

@zlareb1

@zlareb1 zlareb1 commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance 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):

With many configured repositories, minting a token, listing a token's namespaces, the pre-edit memory.for_file lookup, the memory-link recheck after code.refresh, the risk refit, the nightly backport-gap pass and decision extraction held the server's event loop for a time that grew with the repositories times the items they handled. Each now derives the configured repositories once per call or pass.

Why

configured_repos() derives the configured entries on every call: a parse of the configuration, then a RepoConfig, a realpath and a stat per entry. A lookup of one namespace (repo_for_namespace, trunk_namespace, is_restricted, the tracker bound to it, the gate) derives them all. #2135, #2139 and #2170 put the loops that walk every entry, memory, hit or directory row inside repos.derived_once(). #2170 named the loops of the same shape it did not reach. This change measures each of them and fixes those over a fixed bar: at 400 entries, 100 ms of event-loop time on a path a request reaches, or 1 s on a worker pass.

Failing cases, with the derivations one call makes over the test's nine entries:

Site What reaches it Asked per item Derivations
token mint (service_accounts.create, personal_tokens.create) request each namespace the token names: whether it is restricted, its gate, whether it is a repository; each restricted one: the tracker bound to it 37 for 4 namespaces
admin token mint (admin/token.mint) request each namespace the token names: whether it is restricted, whether it is a repository; the namespace named before *: its kind 8 for 4 namespaces
namespace.list_mine request: the dashboard on load, a client asking what it reads the org's configured kinds, once; each grant a token inherits from its creator: whether its entry is restricted 7 for 6 grants
memory.for_file request: the pre-edit hook of the coding plugins each extra namespace for its gate and its trunk, the repo: tag for its trunk, each link for its memory's trunk and each trunk for its gate, for the request's namespaces and again for the caller's other namespaces 23
link recheck of code.refresh (recheck_repository) request: inside the call when it waits, else a task on the serving loop on the loop, the repository's entry at each git read and each file read; in a worker thread, each link it moves for its memory's trunk 17 for 3 changed files: 5 on the loop, 12 in the thread
risk refit of the graph statistics pass worker each entry for the tracker bound to it, that tracker whether it is restricted 14 for 9 entries, 5 of them bound
backport-gap pass worker, nightly each trunk for its tracker, each fix for its trunk's release branches, their refs and that tracker 23 for 2 trunks with 2 fixes each
decision extraction of one document worker: the extraction leg of the knowledge source sync, up to 40 documents a pass; loom decisions extract the document's namespace and the repository for their trunk and their gate, each code name the document mentions 9

The mint and memory.for_file counts are with the org's namespace kinds already read (namespace_kinds.kind_map keeps them 30 s per org). A call that finds them unread derives the entries once more to read them: the mint tests clear the kinds, so without the blocks they report 38 and 9.

Pattern: a call or a pass that asks a helper taking one namespace once per item (a namespace a token names, a grant, a changed file, an entry, a fix, a code name), outside a derived_once block.

Fix: each of them runs those lookups inside repos.derived_once(). No lookup changes and none is skipped; the entries are derived once per block instead of once per lookup.

Event-loop time of the lookups each loop makes at its bound, bare and inside one block (the script is under Tests; one laptop, so read the ratios). The lookups per item are the counts above:

Loop at its bound Lookups at 400 entries 100 entries 400 entries 1,000 entries 400 entries, in a block
token mint, 5 namespaces 46 108 ms 431 ms 1.1 s 9 ms
admin token mint, 20 namespaces 40 95 ms 370 ms 0.9 s 9 ms
namespace.list_mine, 50 inherited grants 51 118 ms 480 ms 1.2 s 10 ms
memory.for_file, one call 23 55 ms 216 ms 0.5 s 9 ms
refresh link recheck, 20 changed files 22 50 ms 202 ms 0.5 s 9 ms
risk refit, every entry bound 800 0.5 s 7.7 s 59 s 14 ms
backport-gap pass, every trunk and 20 fixes 201 0.3 s 1.9 s 9.2 s 12 ms
decision extraction, 40 documents 120 0.3 s 1.2 s 3.1 s 10 ms
SCIP delta apply, 10 batches 20 46 ms 185 ms 0.5 s 9 ms
knowledge source sync, 40 written files 40 93 ms 368 ms 1.0 s 10 ms

The recheck's row holds only its lookups on the loop: two a pass and one per changed file. The four per moved link (80 for 20 files) run under asyncio.to_thread and hold no event-loop time, so they are not in the row. The block covers them as well: the thread runs in a copy of the caller's context, which carries what the block holds.

The first eight are over the bar and are fixed. The last two are under it and are left alone. Decision extraction is the closest call. The 9 in the first table is a run that names a repository, as loom decisions extract --repo does. The sync worker names none: a document with one decision makes two lookups in a memory namespace (0.8 s for 40 documents at 400 entries, under the bar) and three in a branch's code namespace (1.2 s, over it), and each further decision adds a lookup for each code name it mentions. Its block is per document, so a pass of 40 documents derives the entries 40 times, about 0.4 s at 400 entries, instead of 80 times or more.

Scope and risk

Nine with derived_once(): blocks and the list of them in docs/architecture/code-index-architecture.md.

  • Authorization is unchanged. Each namespace is still asked for its gate, its classification and its grant, and a block holds only the list those lookups scan. The tests run the real configuration parse and the real gate: a mint still writes the token's own capped grant on the restricted repository it names and on nothing else, a token's list still leaves out its creator's restricted namespaces, and a file lookup still reads links only for the namespaces the caller may read.
  • A block ends with the request or pass that opened it. The recheck's block is inside recheck_repository, not around the code.refresh handler: a refresh that does not wait runs the recheck as a task that outlives the handler. The recheck awaits its worker thread inside the block; a thread that a cancelled recheck leaves running finds the block ended at its next lookup and derives the entries itself. memory.for_file starts and awaits its link read and its bounded read of the caller's other namespaces inside the block. The admin mint's block ends after its last configuration read, the kind of the namespace a * token names before *, and its audit row is written inside it. A write that a document's extraction leaves running when it ends derives the entries itself, since no task reads what a block held once the block has ended.
  • A configuration change in the middle of a block:
    • A registry snapshot that replaces the one the block read (a branch added from the Repositories page, a registry row's restriction) is read at the next lookup inside the block. The tests of A worker pass or a read derives the configured repositories once for the rows it asks about them #2170 replace the snapshot in the middle of a pass.
    • Directory rows (a namespace's classification, grants, join policy) are not held by a block. namespace_classification and the gate read them as before.
    • The operator's configuration is read once per block. code_repos_json is not a live setting, so it changes only at a restart. The stat of each clone directory is as of the block's first lookup; it feeds only the check that two entries do not hold one clone under two spellings, and the next block reads it again.
  • The longest blocks are the backport-gap pass (up to code_backport_gaps_budget_s), which already lists its trunks once at the start, and one document's extraction (its model calls).

Left alone, with what each costs at 400 entries:

  • The SCIP delta apply pass makes two lookups per uploaded batch and takes at most three batches per namespace with a complete upload in a pass. Ten batches cost 185 ms, under the bar for a worker pass. A backlog of 300 batches would cost about 6 s, spread over 300 awaits.
  • The knowledge source sync makes one lookup per file it writes for a source served from a code namespace, and a pass writes at most knowledge_source_sync_documents_per_pass documents (40): 368 ms, under the bar.
  • The code.backport_gap op makes about six lookups a call, about 60 ms, under the bar for a request.
  • The gate derives the entries two or three times for each call it is asked with a principal: linear in the entries.
  • Inside a block a lookup still copies and scans the list. A lookup keyed by namespace would remove this and the four items above, and is a different change.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: a revert leaves nothing behind.

Tests

tests/unit/test_namespace_loops_derive_once.py runs the real configuration parse and the real gate over the entries of test_repo_listings_derive_once.py: two orgs, repositories of several branches, a branch added from the page, entries the caller may only ask to join or may not see. Each site has a test of its answer and a test of its derivation count. The mint's count runs through service_accounts.create and through personal_tokens.create, and the admin mint's for a list of namespaces and for a namespace before *. The tests of the eight sites were committed before the change: the eight answer tests passed and the eight counts were the numbers in the first table. The count for an admin mint of ["notes", "*"] was committed while the block ended before the read of that namespace's kind, and reported 2. The count through personal_tokens.create was added after its block; with that block replaced by contextlib.nullcontext it reports 38. With the change all 19 pass, and each count is 1.

Three counts are lower bounds. The recheck's test replaces the mirror, the link store and the policy step. The backport-gap test's read of a fix is the test's own: it makes the three configuration lookups the composed read makes. The extraction test uses a fake model, and code.symbol itself over an empty index.

LOOM_ENV_FILE= .venv/bin/python -m pytest tests/unit/test_namespace_loops_derive_once.py tests/unit/test_code_passes_derive_once.py tests/unit/test_repo_listings_derive_once.py -q   # 56 passed
LOOM_ENV_FILE= make format lint typecheck   # clean

The unit files that name admin_routes, for_file, me_service_accounts or personal_tokens were run with test_package_contracts.py, test_repo_snapshot_contract.py, test_public_copy.py and test_doc_links.py: 2,416 tests pass. Eleven tests of test_store_names_refused.py and test_mirror_names_refused.py run the migrations through a local clickhouse binary and fail on that binary's Unknown setting 'enable_quantized_codec'; they call nothing this change edits. CI runs the full suites.

The timing script
import json, os, tempfile, time
from server.code.domain.index import repos
from server.settings import settings

settings.code_self_service_enabled = False
BRANCHES = ["main", "26.1", "25.8", "25.3"]  # four branches a repository
# loop -> the lookups it makes at its bound, n entries configured: the per-item counts
# are those the count tests report without the change
LOOPS = {
    "token mint, 5 namespaces (1 + 9 each)": lambda n: 46,
    "admin token mint, 20 namespaces (2 each)": lambda n: 40,
    "namespace.list_mine, 50 inherited grants (1 + 1 each)": lambda n: 51,
    "memory.for_file, one call": lambda n: 23,
    "refresh link recheck, 20 changed files (2 + 1 on loop)": lambda n: 22,
    "risk refit, every entry bound (2 each)": lambda n: 2 * n,
    "backport-gap pass, 20 fixes (1 + 1 a trunk + 5 a fix)": lambda n: 1 + n // 4 + 100,
    "decision extraction, 40 documents (3 each)": lambda n: 120,
    "SCIP apply, 10 batches (2 each)": lambda n: 20,
    "source sync, 40 written files (1 each)": lambda n: 40,
}


def entry(i, root):
    repo, branch = divmod(i, 4)
    os.makedirs(f"{root}/r{repo}.git", exist_ok=True)
    return {
        "org": "o",
        "namespace": f"code-r{repo}-{branch}",
        "repo": f"acme/r{repo}",
        "git_dir": f"{root}/r{repo}.git",
        "git_remote": f"https://github.com/acme/r{repo}.git",
        "git_branch": BRANCHES[branch],
    }


def ms(lookups, name, block):
    start = time.perf_counter()
    if block:
        with repos.derived_once():
            for _ in range(lookups):
                repos.repo_for_namespace("o", name)
    else:
        for _ in range(lookups):
            repos.repo_for_namespace("o", name)
    return (time.perf_counter() - start) * 1000


with tempfile.TemporaryDirectory() as root:
    for n in (100, 400, 1000):
        settings.code_repos_json = json.dumps([entry(i, root) for i in range(n)])
        name = f"code-r{n // 8}-0"
        for loop, lookups in LOOPS.items():
            k = lookups(n)
            bare, held = ms(k, name, False), ms(k, name, True)
            print(f"N={n:<5} {loop:54} {k:5} lookups  bare {bare:6.0f} ms  in a block {held:5.1f} ms")

Run it with LOOM_ENV_FILE= .venv/bin/python -P <file> from the repository root.

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. Checked by hand: each block's extent against what runs inside it and what outlives it, the derivation counts before and after, and that the answer tests pass on the unchanged code.

  • 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: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump), the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py) and the standalone skill bundles (python3 scripts/codegen/standalone_skills.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • 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.

🤖 Generated with Claude Code

zlareb1 and others added 4 commits December 5, 2026 06:38
… at a time

A token mint, a token's namespace list, a pre-edit file lookup, the link recheck of a refresh, the risk refit, the backport-gap pass and a document's decision extraction each ask a helper about one namespace per item, and each ask derives every configured entry. Each call's answer is asserted beside the count; the eight count tests fail until the call derives the entries once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…positories once

A token mint, a token's namespace list, a pre-edit file lookup, the link recheck of a refresh, the risk refit, the backport-gap pass and a document's decision extraction each asked a one-namespace helper per item outside a derived_once block, so each ask parsed the operator's configuration and took a realpath and a stat of every clone directory. Each now runs its lookups inside one block. No lookup's answer and no gate changes; a block ends with the call or pass that opened it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
The architecture page names every call and pass that runs its per-item lookups inside repos.derived_once(): the token mints, a token's namespace list, the file lookup, a refresh's link recheck, the risk refit, the backport-gap pass and a document's decision extraction join the list.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…mbol itself

The count of a document's decision extraction covers the lookup of each code name it mentions: code.symbol runs over an empty index, in place of the fixture's stand-in.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@vercel

vercel Bot commented Dec 5, 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 5, 2026 2:34am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 2:34am UTC

Request Review

zlareb1 and others added 4 commits December 5, 2026 07:17
…mint that names a home

`personal_tokens.create` opens its own block, which no test entered.
`admin/token.mint` for a namespace before `*` reads that namespace's kind after
its block has ended: the count test reports 2 derivations for it.

The mint fixture clears the namespace kinds, which are cached per org, so a
count no longer depends on which test read them first. `HELD` is the list of
names its one reader walks.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…s block

The mint's last configuration read, the kind of the namespace a `*` token saves
to, ran after its `derived_once` block: a second derivation of the entries in
one call. The block now ends after that read.

`memory.for_file` imports `derived_once` beside its other code imports, and the
block's comment stands apart from the comment on the read inside it. The
architecture page names a fix and a code name among what the loops run over,
and says which mints ask for a bound tracker.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…t_mine notes say what is asked

The `owner` fixture cleared the namespace kinds before a mint and not after, so a
later test in the same process read this file's configured kinds for 30 s.

The token-mint line of the architecture page and the mint test's docstring said the
tracker is asked of each namespace: only `service_accounts.create` and
`personal_tokens.create` ask it, and only of a restricted one. The comment on the
`namespace.list_mine` block named a loop that is in `_inherited_rows`: it now says the
block also holds the configured kinds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>

This branch has not been deployed

No deployments
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.

1 participant