Skip to content

A revision lookup by id or by identity sends only the branch it has rows for - #2187

Merged
zlareb1 merged 1 commit into
mainfrom
fix/live-revisions-one-branch
Dec 5, 2026
Merged

zlareb1 merged 1 commit into
mainfrom
fix/live-revisions-one-branch

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

On ClickHouse 26.4, the read that resolves the rows a namespace.audit, namespace.validate, namespace.tune or memory.inspect report cites does not read the whole namespace when the report names its rows by id only or by identity only: 256 rows, not 200,896, for two identities in a namespace of 200,000 rows.

Why

reads.live_revisions resolves the rows a report cites: by memory_id, by full identity (agent, user_id, session_id, memory_key), or by both. It sent one statement for every call,

AND (memory_id IN {ids:Array(UUID)}
     OR (agent, user_id, session_id, memory_key)
        IN {identities:Array(Tuple(String, String, String, String))})

with an empty array bound for a list that names no row. On ClickHouse 26.4, the oldest release Loom runs on, that statement reads the whole namespace. EXPLAIN indexes = 1 on the identity-only call shows the primary key condition as org and namespace alone and 1,563 of 1,563 granules selected; with the identity branch alone the condition holds the identity set and 2 granules are selected. An id-only call reads the namespace the same way, where the memory_id branch alone reads 9,728 rows. On the latest release (26.11.10.7) the joined statement with the id list bound empty reads 512 rows for the same identity-only call.

The statement is built from the lists that name a row: ids alone, identities alone, or both joined by OR. A call with both lists empty returns no row and sends no statement, as before. The select list, the org, namespace and tombstone predicates, the read-scope clause and the LIMIT are the same on each shape, and so is the answer.

Alternative and its cost: one statement per list would let a call that names rows both ways prune too, at a second round trip for that call. This change keeps one statement per call.

Scope and risk

  • Touches live_revisions in server/memory/store/reads.py and nothing else in the server. Its callers (finding_evidence.revisions_for, and through it the four ops above) are unchanged, and no op answer changes.
  • Left alone: a call that names rows both ways sends the joined statement, which reads the namespace on 26.4 (200,896 rows in the run below) and on the latest release (179,776 rows).
  • Tenant isolation: every shape keeps org = {org:String} AND namespace = {ns:String}, tombstoned_at IS NULL and scope_and(scope, params). The statement test asserts all three on each shape, and the clickhouse-local test reads each shape over the whole namespace and inside one user's scope, with another namespace's row and another user's row in the table.
  • No op, wire field, table, column, migration, setting, worker or doc is added or changed. tests/analyzer/shapes.txt gains the two new statement shapes; the joined shape keeps its digest.
  • Three existing test files change. The stubs of tests/unit/test_namespace_audit.py and tests/unit/test_namespace_validate.py recognised the citation read by the text of its identity branch, which an id-only call does not carry; they recognise it by its select list, which every shape carries. One assertion of the audit test, that an id-only read binds identities to an empty list, becomes that it binds no identities parameter. The analyzer case memory_repo.live_revisions in tests/analyzer/cases/legs.py drives the read by id, by identity and by both, which renders the two new shapes.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: a revert; nothing is stored.

Tests

New, in tests/unit/test_live_revisions.py:

  • test_an_empty_list_sends_no_branch_for_it (three cases: ids, identities, both): fails when the statement holds an IN over a list bound empty, when a branch for a named list is missing, or when the tenant predicates, the scope clause or the LIMIT leave a shape.
  • test_both_lists_empty_runs_no_statement: fails when a statement is sent.
  • test_each_shape_returns_the_rows_of_the_joined_form (three cases, on clickhouse-local against the loom.memory definition of schemas/schema.sql): fails when a shape drops or adds a row against the joined statement with the unnamed list bound empty. The table holds a row at two versions, a tombstoned row, a second user's row, an unnamed row and another namespace's row named by id and by identity; each case reads over the whole namespace and inside one user's scope and compares with the expected rows too.

Against live_revisions as main has it: 2 failed (the id-only and identity-only cases of the first test), 5 passed. With the change: 7 passed with the clickhouse binary of 26.4.5.143 and of 26.11.10.7; 4 passed, 3 skipped without the binary.

Measurement, on a ClickHouse started with dev/ci_start_clickhouse.sh and migrated with make migrate, 200,000 rows in one namespace in one part (1,563 granules of 128 rows), a call for two rows:

Call 26.4.5.143, joined statement 26.4.5.143, this change 26.11.10.7, this change
identities only 200,896 read_rows 256 512
ids only 199,872 9,728 4,736
both 200,896 200,896 179,776

To reproduce: seed with

INSERT INTO loom.memory (org, namespace, agent, user_id, session_id, memory_key, memory_type, value, version)
SELECT 'measure-org', 'measure-ns', 'a', '-', '-', concat('k/', leftPad(toString(number), 7, '0')),
       'semantic', concat('value ', toString(number)), 1
FROM numbers(200000)

then call reads.live_revisions('measure-org', 'measure-ns', scope=WHOLE_NAMESPACE, hash_max_bytes=0, ...) under query_tag(...) for keys k/0000042 and k/0150000 (by their two identities, by their two ids, and by the id of one with the identity of the other), run SYSTEM FLUSH LOGS, and read

SELECT log_comment, read_rows FROM system.query_log
WHERE type = 'QueryFinish' AND startsWith(log_comment, '<the tag>')

The first column of the table is the same calls at main's commit, before the edit.

Run locally with pytest -n 2:

  • At this head, with the clickhouse binary of 26.11.10.7: tests/unit/test_live_revisions.py with the unit tests of the callers (test_namespace_audit.py, test_namespace_validate.py, test_memory_inspect.py, test_namespace_tune.py, test_pg_authority.py), in random order: 558 passed.
  • At this head, tests/analyzer against the migrated ClickHouse (26.11.10.7): 12 passed. python -m tests.analyzer.corpus --write wrote the two new lines of tests/analyzer/shapes.txt.
  • At this head, with no ClickHouse and no clickhouse binary: the repository-wide scans (package contracts, op transports, tunable constants, store names, degradation notes, public copy, doc links, agent instructions) with tests/analyzer: 601 passed, 12 skipped (10 need the binary, 2 need a migrated ClickHouse).
  • ruff format --check ., ruff check (the make lint paths), make typecheck, the function-length and secret gates pass at this head. So does every generator check of make lint except the Rust and Go SDK generators: rustfmt and gofmt are not installed on this machine, no op or wire field changes, and CI runs them. The Biome lint of frontend/src was not run: no frontend file changes. make format was not run as a whole: shfmt is not installed, and the diff holds Python files and one generated list.
  • The runs below are from this change's commit before it was rebased onto the current main. The rebase brought in one commit, which touches none of this change's files except other lines of tests/analyzer/shapes.txt.
    • The same scans with the clickhouse binary of 26.11.10.7: 601 passed.
    • The existing integration tests of the citation read, against a server run from source on that ClickHouse: tests/integration/test_namespace_findings_contract.py, 5 passed. test_the_citation_read_resolves_ids_and_identities calls live_revisions on the live store by id only, by identity only and under another user's scope, so both new shapes ran there. With test_memory_inspect.py and test_findings_labeled_cases.py (-m "not slow and not destructive"): 7 passed, 1 skipped; test_namespace_tune.py: 1 passed, 1 skipped. Both skips need a server that enforces auth.
  • No new integration test: the two new shapes are covered by the existing one, and no op answer changes.

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 wrote the diff, the tests and this description, ran the checks listed under Tests and took the measurement.

  • 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

…ows for

Beside an `IN` over an empty list, ClickHouse 26.4 cannot prune the other
branch by the sorting key or the `memory_id` index, and reads the whole
namespace. `reads.live_revisions` builds its statement from the lists that
name a row: ids alone, identities alone, or both joined by `OR`. The answer,
the tenant and read-scope clauses and the `LIMIT` are the same on each.

On 26.4 with 200,000 rows in one namespace, an identity-only call for two
rows reads 256 rows where the joined statement reads 200,896, and an id-only
call reads 9,728. A call that names rows both ways sends the joined
statement and reads the namespace.

The stubs of the namespace.audit and namespace.validate tests recognise the
citation read by its select list, which every shape carries.

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.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 12:22am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 12:22am UTC

Request Review

@zlareb1
zlareb1 merged commit 5daf4aa into main Dec 5, 2026
82 of 83 checks passed
@zlareb1
zlareb1 deleted the fix/live-revisions-one-branch branch December 5, 2026 02:10
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