Repository navigation
memory.proposals names no contradicting memory and counts no graded run outside the caller's read scope - #2188
Merged
Merged
Conversation
…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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of: #1144
Changelog category (leave one):
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 inconflict_withonly 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.proposalsis 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_withwas 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_countscounted every graded run of the namespace that names the key inmemories_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'sgraded_passes,graded_failsandlast_graded_at, and with themreadyand the rowsonly="ready"keeps.The fix:
handle_memory_proposalsreads which of the returned page'sconflict_withids have a row inside the scope and leaves the others out. The read isids_in_scopeofserver/memory/store/conflicts.py, the oneconflicts.listmakes 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_scopebinds its ids a slice at a time (query_all_sliced), each slice within what one ClickHouse parameter carries. A row'sconflict_withhas 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 onloom.agent_runs, whose rows carryuser_idandsession_id. The handler passes the request's scope.apply_policy, whichrun.gradecalls, passesWHOLE_NAMESPACE: the owner's policy judges a proposal on every run of the namespace, whoever sends the grade.conflicts.list, and nodegradedcode 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 tograded_countsand filters the page'sconflict_with.server/memory/app/proposals.py:graded_countsand its call inapply_policy.decideis not touched.server/memory/store/conflicts.py:_ids_in_scopeis namedids_in_scopeand 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 intests/unit/test_conflicts_repo.pyand one case intests/integration/test_read_scope_isolation.py.Left alone: the listing statement, the
count_onlypath,_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:
mainand no other.loom.memory, byorg,namespaceand the distinctconflict_withids of that page. The page holds at mostlimitproposals (500), but a row'sconflict_withhas 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.listmakes 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.readyfor 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.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 nosession_idto complete that scope. The store read and the predicate take either scope, and the unit tests run both.Compatibility, rollout and rollback
conflict_with,graded_passes,graded_fails,last_graded_atandreadyofmemory.proposalschange as described; the same holds over MCP, the SDKs andloom call memory.proposals, which all reach this handler.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.pyruns the handler's real statements on clickhouse-local against theloom.memoryandloom.agent_runsdefinitions ofschemas/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 underown_userand the first underown_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 andready="accept"; underown_session1 pass; the whole namespace 2 and 2, and noready.test_the_policy_counts_the_whole_namespace: two users' runs each failed a proposal once; underreject_after_fails=2apply_policyrejects it from a request bound to one of the two users. Withrequest_scope()passed in place ofWHOLE_NAMESPACEthis test fails.tests/unit/test_conflicts_repo.pyrunsids_in_scopeon 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.setjudges 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: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 aclickhousebinary on the path: 124 passed.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.pyandtest_clickhouse_mutation_inventory.py: 427 passed.pytest tests/analyzerwith no ClickHouse: 10 passed, 2 skipped. The two skipped cases send every statement shape to a live ClickHouse;tests/analyzer/shapes.txthas no diff.Field value too long), andids_in_scopeover the same ids, under anown_userscope that holds half of their rows, answers those 2,500.ruff format --checkon the changed files,ruff check,make typecheckand 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/analyzeragainst 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.test_read_scope_isolation.py(the whole module),test_proposal_triage.py,test_graded_runs_proposals.pyandtest_memory_conflicts.py: 33 passed.test_cross_principal_leakage.py: 37 passed, 17 skipped, and 2 teardown errors intest_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.sdk_py_gen,sdk_py_client_gen,sdk_py_admin_gen,sdk_ts_gen,sdk_rb_gen,docs_referenceandop_reach_censuspass.rustfmt,gofmtandshfmtare not installed on this machine, so the Rust and Go SDK generator checks andmake formatas 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:
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
make test-unitand applicable integration results are reported by CI; a full local run is not required before pushing.docs/reference/configuration.mdand thedocs/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).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.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 sharedids_in_scope, also refactored to batch large id lists withquery_all_sliced).graded_countstakes the request’sReadFiltersand adds the same scope predicate to itsloom.agent_runsquery, sograded_passes,graded_fails,last_graded_at, andreadyreflect only in-scope runs.apply_policy(auto accept/reject on grade) still usesWHOLE_NAMESPACEso namespace policy judges every run.Docs in
writing.mdnote the scoped behavior. Unit and integration tests cover contradictions, graded counts, and policy acrossown_user/own_sessionvs whole-namespace readers.Reviewed by Cursor Bugbot for commit 10d433b. Bugbot is set up for automated code reviews on this repo. Configure here.