Skip to content

code.symbol's lesson reads go through the sort key and finish within their bound - #1322

Merged
zlareb1 merged 7 commits into
ClickHouse:mainfrom
enricoconvento98:fix/644-code-symbol-lessons
Nov 25, 2026
Merged

zlareb1 merged 7 commits into
ClickHouse:mainfrom
enricoconvento98:fix/644-code-symbol-lessons

Conversation

@enricoconvento98

@enricoconvento98 enricoconvento98 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #644

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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

code.symbol returns a symbol's lessons on large tables again: their reads now go through each table's sort key and finish within code_enrich_timeout_s. When the lessons read still fails or times out, the first definition carries lessons_unread: true, so an empty list no longer looks the same as a symbol with no lessons.

Why

code.symbol reads a symbol's lessons as a side read bounded by code_enrich_timeout_s (0.5 s by default). A read that runs past the bound is cancelled and replaced by an empty list, by design, so the core answer never waits on it. On a large deployment every lessons read ran past the bound, and the answer said nothing about it.

The reads filtered on columns that are not on the table's sort key:

  • memory_code_links by anchored_path and former.path, which have only bloom-filter skip indexes. Under FINAL, use_skip_indexes_if_final_exact_mode widens the skip index's selection to every granule that may hold another version of the same key: in a test table, 27 of 984 granules became 282, and the read went from 27,648 rows without FINAL to 306,176 with it.
  • loom.memory by memory_id alone. Ids are random against the sort key (org, namespace, agent, user_id, session_id, memory_key), so 176 ids touched nearly every granule.

The fix reads by sort key instead:

  • Link reads by anchored_path and former.path first select the matching sort keys with a subquery that does not use FINAL, where the skip index narrows fully, and then read FINAL only on those keys. The outer read still judges each key by its newest version, so results do not change. namespaces_of_id in server/memory/store/reads.py already uses this pattern.
  • Each link row already stores its memory's identity. The memory-head read and the excerpt read now filter on (namespace, agent, user_id, session_id, memory_key, memory_id), the sort key plus the id, and keep a row only while that identity still holds the linked id.
  • The excerpt read orders and cuts in SQL (code that still matches first, then pinned, then most confident; LIMIT) instead of reading every matched memory and ranking in Python, so a file with many linked memories reads only the rows the answer serves.

Rejected alternatives:

  • Turning off use_skip_indexes_if_final_exact_mode: FINAL could return a superseded version.
  • A projection on anchored_path: not used under FINAL, and it needs a migration.
  • Raising code_enrich_timeout_s: the bound exists so enrichment never slows the core answer.
  • A key subquery for the memory reads as well: 1,009,984 to 960,027 rows, not worth the extra statement.

Scope and risk

Touches the code-plane lesson reads (server/memory/store/code_links.py, server/memory/store/code_plane_reads.py, server/memory/app/code_link_reads.py, server/memory/app/code_plane_reads.py) and code.symbol's enrichment (server/code/app/symbol/symbol.py, server/code/app/enrich.py, server/code/domain/units.py). Reads only: no write path, schema, migration or setting changes. Tenant scoping is unchanged: every read keeps its org and readable-namespace filters.

The memory-head read is shared. current_links (server/memory/app/code_link_reads.py) now reads heads by identity, and it also serves memory.for_file (its links and code_state) and the decisions read behind code.context_pack and the review brief. Those paths get the same narrower read, with the same results except for the case below. The off-key link read changes only code.symbol's lessons: it is the one caller that asks for former paths.

One behaviour change, on every path above: a memory is found only under the identity its link was made from. If a memory id now sits under another identity (for example a namespace restored under another agent), its link does not find it until the link worker relinks it. Before, the read by id alone found it anywhere in the org.

Compatibility, rollout and rollback

  • Wire: code.symbol definitions gain lessons_unread: bool, serialized only when true. No field removed, no default changed. The SDK op tables are regenerated.
  • Operator steps: none.
  • Rollback: reverting leaves nothing behind; no rows, migrations or settings.

Tests

  • New tests/unit/test_code_link_reads_by_key.py runs the real statements on clickhouse-local with merges stopped, so each version of a row stays in its own part. It checks that a link is found by its anchored or former path as its newest version has it, that the off-key link reads take their keys from the index first, that a memory is read by its identity only while it holds the linked id, and that the excerpt read keeps the best rows up to its limit. It skips without the clickhouse binary; the three read-by-key checks fail on main.

  • tests/unit/test_code_symbol_concurrent_reads.py now checks that lessons_unread is set when the lessons read fails or is cut off at its bound, and absent for a symbol with no lessons, for a narrowed token (which makes no lessons read) and for include_lessons: false.

  • tests/integration/test_code_result_lessons.py calls served_excerpts directly; the call now passes the memory's identity and a limit, as the new signature takes.

  • Rows read, measured on a test table of 1M links and 1M memories inserted in 8 batches with merges stopped, from system.query_log for the statements main and this branch send (same rows returned in each case):

    Read main this branch
    links by anchored_path 306,176 35,840
    links by path 8,192 8,192
    memory heads, 176 memories 1,009,984 70,784
    excerpts, 176 memories 1,009,984 70,784

    These are rows read, not end-to-end latency; no code.symbol call was timed against a large table.

  • Run locally: make lint and make typecheck pass; the four lesson-read unit files pass with the clickhouse binary on PATH in random order; the unit and integration suites pass except for tests that fail the same way on main (test_read_scope_admission, test_read_scope_isolation: code.read is not classified; test_mcp_coding_toolset, test_mcp_initialize_surface; test_sdk_surface_contract: the code.grep fields) and the signed-mirror cases that run only with auth on.

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: the cause with EXPLAIN indexes = 1 and system.query_log on a test table, the rows each statement returns on main and on this branch, and the test results 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: 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) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (No migration.)
  • 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. (No plugin file.)
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed. (Not 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.

@vercel

vercel Bot commented Nov 24, 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 Nov 25, 2026 1:46pm UTC
loom-telemetry Ready Ready Preview Nov 25, 2026 1:46pm UTC

Request Review

@enricoconvento98
enricoconvento98 force-pushed the fix/644-code-symbol-lessons branch from 1f47ad2 to 3c99a8a Compare November 24, 2026 15:19
@zlareb1

zlareb1 commented Nov 24, 2026

Copy link
Copy Markdown
Member

rebase

@cla-assistant

cla-assistant Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@enricoconvento98
enricoconvento98 force-pushed the fix/644-code-symbol-lessons branch from 5b6b355 to ad5b982 Compare November 25, 2026 07:47
@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@enricoconvento98 is attempting to deploy a commit to the ClickHouse Team on Vercel.

A member of the Team first needs to authorize it.

@zlareb1

zlareb1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Hi @enricoconvento98 , please resolve merge conflicts and mark Ready for Review

enricoconvento98 and others added 7 commits November 25, 2026 14:30
…hin their bound

code.symbol returned no lessons on a large deployment: its lesson read ran past
code_enrich_timeout_s and was answered with none. Three of its statements read
most of their table under FINAL.

- The links by anchored_path and former.path: neither column is in the sort
  key. Their bloom filters select few granules, but FINAL adds every granule
  of every part whose sort-key range overlaps them. Measured on 1M links in 8
  parts: 27 granules widened to 282, 306,176 rows read for 51.
- The memory heads current_links reads and the excerpts of the matched
  memories: both by memory_id, which is not in loom.memory's sort key either,
  so 176 memories read the whole table (1,009,984 rows each).

The links reads now take the keys of every matching version from the index
without FINAL, and FINAL reads only those keys and judges each by its newest
version: 35,840 rows. The memory reads go by the identity each link was made
from, the table's sort key, and keep a row only while that identity still
holds the linked memory_id: 70,784 rows each. Every read returns the same rows
as before on the same data, including moved, deleted and replaced rows.

A memory whose id now sits under another identity than the one its link was
made from (a namespace restored into itself under another agent) is no longer
found through that link until the link worker relinks it.

Related: ClickHouse#644

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claude <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
…rpts than it serves

A lessons read that failed or ran past code_enrich_timeout_s was replaced
by an empty list, so the answer read the same as a symbol with no linked
memories. The first definition now carries lessons_unread: true in that
case; a symbol with none, a narrowed token and include_lessons: false
still carry nothing.

The excerpt read ranked and cut its rows in Python after reading every
matched memory. It now orders them in SQL the same way (code that still
matches, then pinned, then the most confident) and keeps limit rows, so a
file with many linked memories reads only what the answer serves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claude <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
With no changed code, the excerpt read orders pinned rows by confidence,
so the pinned row at 0.2 comes before the pinned row at 0.1. The test
expected the reverse; it runs only where the clickhouse binary is
installed, which is how the wrong order went unseen.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
…ns test by what it checks

A narrowed token makes no lessons read and carries no lessons_unread
flag; no test ran code.symbol with a narrowed request. The test that
stubbed the lessons read to None was named for a read past its bound,
which it does not run; the bound has its own parametrized test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
…t off

A symbol that matches several members reads each member's lessons. When
one of those reads is cut off or fails, the lessons the other reads
returned are still served, and the first definition carries
lessons_unread: true, since lessons may be missing. The owning type's
lessons, which fill in only when a method has none, are dropped when
their read is cut off, like an empty read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
Regenerated with scripts/codegen/sdk_rust_gen.py; the generator also
renumbers its default helpers after the new field's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
With matched members, lessons_unread can come with the lessons of the
members that were read. The concept page and the field comment no longer
say the flag comes instead of lessons. Two comments in symbol.py now say
why an owner's cut-off read does not set the flag and what the unread
list is for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Enrico Convento <[email protected]>
@enricoconvento98
enricoconvento98 force-pushed the fix/644-code-symbol-lessons branch from ad5b982 to 4f40f08 Compare November 25, 2026 13:45
@enricoconvento98
enricoconvento98 marked this pull request as ready for review November 25, 2026 13:48
@enricoconvento98

Copy link
Copy Markdown
Contributor Author

Rebased on main. The conflict was with the per-member lessons read from #1332. For code.edit_context, when one member's lessons read is cut off, the answer now keeps the other members' lessons and sets lessons_unread, so a list that comes with the flag may be incomplete.

That makes this PR two changes: the lesson reads by sort key, which fix #644, and the lessons_unread flag. I can move the flag to its own PR if you prefer.

@zlareb1 zlareb1 self-assigned this Nov 25, 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 sort-key reads preserve newest-version selection and readable lesson namespaces. The response marks failed or timed-out reads and keeps lessons from matched members whose reads finish.

Checked 343 focused tests with ClickHouse 26.11, 589 scope and statement-corpus tests, and 275 documentation link checks. The Python and TypeScript SDK generators report no drift.

@zlareb1
zlareb1 merged commit 35ec44b into ClickHouse:main Nov 25, 2026
81 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.

code.symbol returns no lessons when the memory_code_links reads exceed code_enrich_timeout_s

2 participants