Skip to content

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

Merged
zlareb1 merged 10 commits into
mainfrom
fix/derive-once-remaining
Dec 5, 2026
Merged

zlareb1 merged 10 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 29 for 2 links in 2 trunks
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 tests clear the kinds, so without the blocks the mints report 38 and 9 and the file lookup 30.

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 the size given, bare and inside one block (the script is under Tests; one laptop, so read the ratios). The lookups per item are the counts above. Decision extraction's block is per document, so its last cell is 40 blocks:

Loop and the size timed 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 with 2 links 29 66 ms 260 ms 0.7 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 0.4 s, in 40 blocks
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

Three of the sizes are caps: the 40 documents of decision extraction and of the knowledge source sync are a sync pass's knowledge_source_sync_documents_per_pass, and the risk refit's is every configured entry with a tracker bound to it. The others are what one call or pass of a large deployment handles, not limits the code sets, so their bare times are not worst cases. A mint's namespace list, the grants a token inherits, the links of a file and the files a push changes have no cap. The backport-gap pass takes up to code_backport_gaps_prs_per_pass fixes a trunk (300). The SCIP apply pass takes three batches for each namespace with a complete upload. Bare, a larger size costs in proportion to its lookups.

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 and serves no memory that only a link in a trunk the caller may not read reaches.
  • 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 its link read and its bounded read of the caller's other namespaces inside the block, and a call that answers has awaited both there. 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. Two tasks can also outlive their block. One is a write that a document's extraction leaves running when it ends. The other is the read of the caller's other namespaces when the request's own read of memory.for_file raises: asyncio.gather raises at once and leaves that read running to its bound (_OWN_ROWS_SECONDS, 0.5 s), as it does without this change. Each 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 (until the pass ends, at most the supervisor's code_backport_gaps_max_pass_seconds), 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, except the file lookup's. Its test then served one link, with the kinds read by the test before it, and reported 23. It now serves a second link, in a trunk the caller may not read, and asserts the hits: with the gate on each link's trunk bypassed it fails with both memories served, and with the block replaced by contextlib.nullcontext it reports 30. 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: its three helper calls make five lookups, one fewer than the composed read. 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,379 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 contextlib, 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 the size named, 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: 29,
    "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,
}
# the blocks a loop's lookups are spread over: one, except a block per document
BLOCKS = {"decision extraction, 40 documents (3 each)": 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, blocks):
    """`lookups` spread evenly over `blocks` blocks, or bare when `blocks` is 0."""
    start = time.perf_counter()
    for _ in range(blocks or 1):
        with repos.derived_once() if blocks else contextlib.nullcontext():
            for _ in range(lookups // (blocks or 1)):
                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, blocks = lookups(n), BLOCKS.get(loop, 1)
            bare, held = ms(k, name, 0), ms(k, name, blocks)
            print(f"N={n:<5} {loop:54} {k:5} lookups  bare {bare:6.0f} ms  in {blocks:2} block(s) {held:6.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 3:21am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 3:21am UTC

Request Review

zlareb1 and others added 6 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>
…aller may not read; every test of the file drops the namespace kinds it read

The memory.for_file answer test asserted only what the link store was asked
for. It now serves two of the caller's memories linked to the file, one in a
trunk the caller may read and one in a trunk they may not, routes their rows,
and asserts the hits and their code state: the memory reached only by the
link in the unreadable trunk is not a hit. With the per-link gate bypassed
the test fails with both memories served.

A file lookup with the `all_mine` default scope reads the org's namespace
kinds, which are kept per org for 30 s. The reset the mint fixture made is
now a fixture every test of the file runs under, so no kind is left for a
later test on the same worker.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
The handler runs the read for the request's namespaces and the read for the
caller's other namespaces side by side, so which one reaches the store first
is the event loop's order, not part of the answer.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1
zlareb1 merged commit 518474b into main Dec 5, 2026
85 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.

1 participant