Skip to content

memory.proposals names no contradicting memory and counts no graded run outside the caller's read scope - #2188

Merged
zlareb1 merged 2 commits into
mainfrom
fix/proposals-read-scope
Dec 5, 2026
Merged

zlareb1 merged 2 commits into
mainfrom
fix/proposals-read-scope

Conversation

@zlareb1

@zlareb1 zlareb1 commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Part of: #1144

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

memory.proposals, called with a token narrowed below the namespace (read_scope), lists in conflict_with only the rows inside the token's read scope and counts only the graded runs inside it. It listed the id of every contradicting row and counted every graded run of the namespace.

Why

memory.proposals is one of the ops a token pinned to one user may call (SCOPED_OPS), and its listing read carries the request's read scope. Two reads beside it did not:

  • conflict_with was served as stored. A proposal that recorded a contradiction with a row outside the caller's scope named that row's id to the caller.
  • graded_counts counted every graded run of the namespace that names the key in memories_used. A key is held per user, so another user's run that relied on that user's own row under the same key moved the caller's graded_passes, graded_fails and last_graded_at, and with them ready and the rows only="ready" keeps.

The fix:

  • Under a narrowed scope handle_memory_proposals reads which of the returned page's conflict_with ids have a row inside the scope and leaves the others out. The read is ids_in_scope of server/memory/store/conflicts.py, the one conflicts.list makes for its own peers; it loses its leading underscore for this second caller. A whole-namespace reader makes no such read and sees every recorded contradiction.
  • ids_in_scope binds its ids a slice at a time (query_all_sliced), each slice within what one ClickHouse parameter carries. A row's conflict_with has no cap, and ClickHouse refuses a parameter longer than its field limit, so a page whose ids were bound whole could fail for a narrowed reader where a whole-namespace reader got its answer.
  • graded_counts(ch, org, namespace, keys, scope) takes the read scope and appends its predicate (scope_and) to its one statement on loom.agent_runs, whose rows carry user_id and session_id. The handler passes the request's scope.
  • apply_policy, which run.grade calls, passes WHOLE_NAMESPACE: the owner's policy judges a proposal on every run of the namespace, whoever sends the grade.
  • A failure of either read fails the listing, as it fails conflicts.list, and no degraded code is added. An answer with unfiltered ids is the defect, and an answer without counts reads as an ungraded proposal.

Scope and risk

Touches:

  • server/memory/app/proposals_ops.py: the handler passes its scope to graded_counts and filters the page's conflict_with.
  • server/memory/app/proposals.py: graded_counts and its call in apply_policy. decide is not touched.
  • server/memory/store/conflicts.py: _ids_in_scope is named ids_in_scope and sends its statement, whose text is the same, once per slice of ids.
  • docs/features/memory/writing.md: one sentence in the Proposals section.
  • tests/unit/test_proposals_read_scope.py (added), two cases in tests/unit/test_conflicts_repo.py and one case in tests/integration/test_read_scope_isolation.py.

Left alone: the listing statement, the count_only path, _usage_counts (it reads the usage totals of the listed rows' own ids), the wire models, the SDKs and the dashboard, which lists what the op answers.

What could break:

  • Tenant isolation: the change only removes ids and runs from a narrowed token's answer. For a whole-namespace reader the scope clause is empty, so the op issues the statements it issues on main and no other.
  • Cost: a narrowed reader whose page names a contradiction makes one more read of loom.memory, by org, namespace and the distinct conflict_with ids of that page. The page holds at most limit proposals (500), but a row's conflict_with has no cap: each judged write merges up to 10 peers into it. One statement binds at most 1,638 ids (ch_params.FIELD_BYTES, half of ClickHouse's default field limit), so the read is one statement up to that many ids and one more for each further slice.
  • conflicts.list makes the same read for a narrowed reader and gets the same bound. Bound whole, a page with more than 3,276 distinct peer ids was refused by ClickHouse (Field value too long); it is answered.
  • ready for a narrowed token comes from that user's runs alone, so it can differ from what a whole-namespace reader and the owner's automatic policy see for the same proposal. The docs sentence says so.
  • Reach: a token pinned to one user (read_user_id) gets the op with a narrowed scope. A token narrowed to one session is refused by the transport, since the op's input has no session_id to complete that scope. The store read and the predicate take either scope, and the unit tests run both.
  • Stored data and concurrency: the op writes nothing, and no stored format changes.

Compatibility, rollout and rollback

  • Wire: no op, field, table, column, migration or setting is added, changed or removed. For a narrowed token the values of conflict_with, graded_passes, graded_fails, last_graded_at and ready of memory.proposals change as described; the same holds over MCP, the SDKs and loom call memory.proposals, which all reach this handler.
  • Operator steps: none.
  • Rolling upgrade: no schema, stored row or wire format changes. A replica of the previous build keeps the defect for the calls it serves until it is replaced.
  • Rollback: a revert leaves nothing behind.

Tests

The unit tests were written before the code. Against main's code the two defect tests failed (a narrowed reader got all four contradicting ids, and counted two fails of runs outside its scope) and the two controls passed.

tests/unit/test_proposals_read_scope.py runs the handler's real statements on clickhouse-local against the loom.memory and loom.agent_runs definitions of schemas/schema.sql:

  • test_a_contradiction_outside_a_narrowed_scope_is_not_named: a proposal that recorded a contradiction with the caller's row, the caller's row in another session, another user's row and a row of no user names the first two under own_user and the first under own_session.
  • test_a_whole_namespace_reader_sees_every_recorded_contradiction: the same proposal names all four.
  • test_graded_runs_outside_a_narrowed_scope_are_not_counted: with two passes by the caller's runs and two fails by runs outside the scope, the caller reads 2 passes, 0 fails and ready="accept"; under own_session 1 pass; the whole namespace 2 and 2, and no ready.
  • test_the_policy_counts_the_whole_namespace: two users' runs each failed a proposal once; under reject_after_fails=2 apply_policy rejects it from a request bound to one of the two users. With request_scope() passed in place of WHOLE_NAMESPACE this test fails.

tests/unit/test_conflicts_repo.py runs ids_in_scope on a stub client:

  • test_ids_in_scope_reads_more_ids_than_one_parameter_carries: 2,000 ids, more than one parameter carries, given twice and out of order. Each id is bound once, across two statements of the same text and scope whose parameters each fit, and the answer is the ids inside the scope from both. With the ids bound whole the test fails: one statement.
  • test_ids_in_scope_of_no_id_sends_no_statement.

tests/integration/test_read_scope_isolation.py::test_a_proposal_names_no_contradiction_and_counts_no_graded_run_outside_the_scope, against a server run from this head on a ClickHouse started for the task (dev/ci_start_clickhouse.sh, make migrate): one user's proposal names a contradiction with that user's fact and with another user's, and one graded run of each user relied on its key. A namespace-wide token reads both ids and 1 pass, 1 fail; a token pinned to the first user reads its own fact's id alone and 1 pass, 0 fails. memory.set judges a write that names its user against that user's rows alone, so the test stores the contradiction with the other user's row itself, as the module's duplicate and successor cases store theirs. With the handler's two uses of the scope taken out, the case fails on both fields.

What ran at this head, with pytest -n 2:

  • The focused unit files (test_proposals_read_scope.py, test_conflicts_repo.py, test_conflict_rows_locate_then_fetch.py, test_proposals.py, test_proposal_triage.py, test_high_rows_sql.py, test_memory_small_defects.py, test_ch_params.py), with a clickhouse binary on the path: 124 passed.
  • The repository-wide scans (test_package_contracts.py, test_op_transport_contract.py, test_tunable_constants.py, test_store_names_refused.py, test_degraded_unnoted.py, test_public_copy.py, test_doc_links.py, test_agent_instructions.py): 601 passed. test_test_inventory.py, test_coverage_floors.py, test_integration_suite_targets.py and test_clickhouse_mutation_inventory.py: 427 passed.
  • pytest tests/analyzer with no ClickHouse: 10 passed, 2 skipped. The two skipped cases send every statement shape to a live ClickHouse; tests/analyzer/shapes.txt has no diff.
  • Integration, against a server run from this head on a ClickHouse started for the task: the case above, 1 passed.
  • On that ClickHouse, 5,000 ids bound as one parameter are refused (Field value too long), and ids_in_scope over the same ids, under an own_user scope that holds half of their rows, answers those 2,500.
  • ruff format --check on the changed files, ruff check, make typecheck and the function-length and secret gates pass.

What ran at the branch's first commit, which this head follows with the sliced read and its two unit cases:

  • pytest tests/analyzer against a ClickHouse started for the task: 12 passed. The corpus drives the op over an empty listing, which issues neither statement; the unit tests above run both.
  • Integration, against a server run from that commit: test_read_scope_isolation.py (the whole module), test_proposal_triage.py, test_graded_runs_proposals.py and test_memory_conflicts.py: 33 passed. test_cross_principal_leakage.py: 37 passed, 17 skipped, and 2 teardown errors in test_the_file_lookups_wider_read_serves_only_the_callers_own_rows (memory_search_default_namespace_scope has no override to reset): its cases set and reset one deployment-wide setting, and two of them ran at once under -n 2. The same cases run one at a time: 7 passed. This change touches neither that test nor that setting.
  • Prettier on the changed page, and the generator checks sdk_py_gen, sdk_py_client_gen, sdk_py_admin_gen, sdk_ts_gen, sdk_rb_gen, docs_reference and op_reach_census pass. rustfmt, gofmt and shfmt are not installed on this machine, so the Rust and Go SDK generator checks and make format as a whole did not run; this change touches no op, no field and no shell file. Biome did not run: no frontend file changes.

Not run:

  • Stress or load run: none. The change adds one predicate to one statement and one read bound by the page's ids.
  • Left to CI: the full unit suite, the full integration suite and the mixed-version gate.

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 and ran the checks listed under Tests.

  • 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.

Unticked: this change has no migration, no plugin file and no frontend file, so those three checks did not run; the CLA line is the committer's to confirm.

🤖 Generated with Claude Code


Note

Medium Risk
Changes API-visible fields for scoped tokens and proposal readiness semantics; the fix tightens tenant isolation rather than adding new surface area.

Overview
Fixes a read-scope leak in memory.proposals: tokens narrowed below the namespace (read_scope) no longer see contradicting memory ids or graded-run stats from outside their scope.

For conflict_with, the handler now keeps only peer ids whose rows fall inside the caller’s scope (via shared ids_in_scope, also refactored to batch large id lists with query_all_sliced). graded_counts takes the request’s ReadFilters and adds the same scope predicate to its loom.agent_runs query, so graded_passes, graded_fails, last_graded_at, and ready reflect only in-scope runs. apply_policy (auto accept/reject on grade) still uses WHOLE_NAMESPACE so namespace policy judges every run.

Docs in writing.md note the scoped behavior. Unit and integration tests cover contradictions, graded counts, and policy across own_user / own_session vs whole-namespace readers.

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

…un outside the caller's read scope

memory.proposals serves a token narrowed below the namespace, and two of its
reads ignored that token's read scope. conflict_with named every row the
proposal contradicts, whoever holds it, and the graded counts came from every
run of the namespace, so another user's run that named the same key moved
graded_passes, graded_fails, last_graded_at and ready.

Under a narrowed scope the handler keeps in the returned page's conflict_with
only the ids whose rows lie inside the scope. It reads them with ids_in_scope
of the conflicts store, the read conflicts.list makes for its own peers, which
is public for this second caller. graded_counts takes the read scope and adds
its predicate to its one statement. apply_policy passes the whole namespace:
the owner's policy judges a proposal on every run of the namespace. A failure
of either read fails the listing, and no degraded code is added.

A whole-namespace reader's answer does not change, and no field changes.

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 1:06am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 1:06am UTC

Request Review

…e contradicting ids than one ClickHouse parameter carries

ids_in_scope bound every id it was given as one parameter. A row's
conflict_with has no cap: each judged write merges up to PEER_LIMIT (10)
peers into it. ClickHouse refuses a form field longer than its
http_max_field_value_size (128 KiB by default, 3,276 ids) with "Field value
too long", so a page with more distinct contradicting ids failed for a reader
narrowed below the namespace. A whole-namespace reader makes no such read and
got its answer.

ids_in_scope sends its statement through query_all_sliced: one statement per
slice of ids that fits ch_params.FIELD_BYTES (1,638 ids), and the answer is
the ids every slice found inside the scope. No id sends no statement.
conflicts.list and memory.proposals both make this read.

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@zlareb1
zlareb1 merged commit 465c908 into main Dec 5, 2026
79 of 82 checks passed
@zlareb1
zlareb1 deleted the fix/proposals-read-scope branch December 5, 2026 01:11
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