Skip to content

Same-key and value-update supersession use the write's user - #752

Merged
zlareb1 merged 4 commits into
ClickHouse:mainfrom
ningyixi:fix/723-writer-scope-supersession
Nov 19, 2026
Merged

zlareb1 merged 4 commits into
ClickHouse:mainfrom
ningyixi:fix/723-writer-scope-supersession

Conversation

@ningyixi

Copy link
Copy Markdown
Contributor

Closes: #723
Related: #701

Changelog category (leave one):

  • Critical Bug Fix (data loss, cross-tenant read, auth bypass)

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

A write that names its user, under a namespace-wide token, no longer retires another user's memory through same-key supersession or the value-update pass.

Why

#701 made the write-time conflict verdict read only the write's user's rows (_writer_scope in server/memory/app/conflicts.py). Same-key supersession (_live_same_key_rows) and the value-update peer reads (_peers_many, _peers_joined, _peers_codes_first) still used request_scope(). Under a namespace-wide token, a write of key seat for Bob could retire Alice's live seat row, and a bulk or set_from_messages write for Bob could mark Alice's dated fact superseded. A write that names no user keeps today's namespace-wide behaviour.

Scope and risk

Touches only the two post-insert retirement paths named in #723, plus tests. Conflict checks, pinned-peer refusal, and a token already pinned to one user are unchanged: _writer_scope is the same helper #701 introduced. If the write's scope cannot be resolved, those paths retire nothing (fail-open, as they already do on a lookup error). A mixed-user batch is read per write user.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: a revert restores the previous namespace-wide retirement behaviour; no migrations or stored rows are added.

Tests

Reproduced on main at 32afe0a with a fake ClickHouse client and a namespace-wide service token: a write that names user_id="bob" issued an unscoped same-key / value-update peer read (the new tests failed before the change). After the fix:

  • LOOM_ENV_FILE= .venv/bin/python -m pytest -q -p no:randomly tests/unit/test_write_scope.py tests/unit/test_key_supersession.py tests/unit/test_value_updates.py tests/unit/test_definition_write_rules.py tests/unit/test_auto_resolution.py tests/unit/test_write_peer_reads.py tests/unit/test_write_candidates_batched.py — 223 passed, 1 skipped.
  • ruff format and ruff check on the changed files: clean.
  • scripts/gates/function_length_gate.py: passed (baseline unchanged).
  • mypy --strict on server/memory/app/key_supersession.py and server/memory/app/value_updates.py: clean.

Did not run the full make test-unit integration suite or a live ClickHouse / Postgres server in this environment (make is not installed here; the local venv is missing some extras such as numpy and tree_sitter). The existing token-pinned write-scope tests still pass, and a namespace-wide write that names no user still reads the whole namespace.

AI assistance in the code (leave one; writing this description with AI is fine):

Checklist

  • Related unit tests and ruff / function-length / mypy on the changed modules pass; the full make format-check, make lint, make typecheck, make test-unit and the integration suite were not run in this environment (see Tests).
  • No generated files needed updating.
  • No new migration.
  • No plugin files changed.
  • No frontend change.
  • No docs change; the behaviour now matches the conflict-verdict rule already described for _writer_scope.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

A write that names its user, under a namespace-wide token, now reads
that user's rows only — the same scope the conflict verdict already
uses — so it no longer retires another user's memory.

Closes ClickHouse#723

Signed-off-by: ningyixi <[email protected]>
@zlareb1 zlareb1 self-assigned this Nov 17, 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 same-key fix does not reach the point-write and update-by-id callers, so cross-user retirement remains on those paths. The new peer-query tests supply a user_id that production point-write batches omit; the inline finding uses the actual production helpers.

223 related unit tests pass locally. One ClickHouse-local test fails identically on this branch and main because the installed binary lacks enable_quantized_codec. Live ClickHouse and Postgres write behavior was not exercised locally.

Comment thread server/memory/app/key_supersession.py Resolved
Same-key supersession reads the write's user from the batch. The
production point-write and update-by-id helpers omitted that field,
so a namespace-wide token writing Bob could still retire Alice's row.

Signed-off-by: ningyixi <[email protected]>

@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 point-write and update-by-id batches carry the write's user, fixing the earlier finding. All 255 other focused scope, conflict-resolver and definition tests pass. The remaining ClickHouse-local test cannot run with the installed binary because it lacks enable_quantized_codec; live ClickHouse and Postgres behavior remains for CI.

@zlareb1

zlareb1 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Three required CI jobs fail on this head; neither is a retry candidate.

  • Unit part 4: test_memory_update_conflict.py::test_clear_verdict_writes_an_active_version_and_resets_the_old_mark expects the old supersession batch without user_id. _revision_supersession correctly includes that field after this fix. Update the assertion to check the carried user. Reproduce with pytest tests/unit/test_memory_update_conflict.py::test_clear_verdict_writes_an_active_version_and_resets_the_old_mark -q.
  • Integration part 3 and integration isolation gates: test_read_scope_isolation.py::test_a_deferred_extraction_replays_inside_the_writers_scope still expects the namespace-wide token's write for user-x to supersede user-v. The intended behavior keeps those users separate. Assert that both named users retain active rows, then make the positive control revise the same user from another session, so it still proves permitted supersession.

The unit failure reproduces locally on this head and passes on main. The source fix for the earlier review finding remains correct. Update the tests for the intended scope and let CI validate the full write paths before merge. The integration failure was checked against its source and CI output; it was not run locally.

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

P2 — The test updates in the existing CI explanation are required before merge. tests/unit/test_memory_update_conflict.py::test_clear_verdict_writes_an_active_version_and_resets_the_old_mark fails on this head and passes on current main: its expected batch omits the carried user_id. The integration control also needs to exercise same-user supersession while preserving the cross-user isolation assertion. The source correction for named-user isolation remains sound; the required test suite must validate that intended behavior.

The clear-verdict update assertion now checks the carried user_id.
The deferred isolation test keeps both named users' rows active, then
revises the same user from another session so permitted supersession
is still covered.

Signed-off-by: ningyixi <[email protected]>

@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 unit expectation is fixed, and 425 focused unit tests pass. The revised deferred-write integration control still fails against a fresh private ClickHouse with fake providers and authentication enabled: user-x has no superseded row after the final replay. The inline finding describes the failing assertion and fixture issue. One additional unit test needs a newer local ClickHouse binary.

@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 deferred-write control passes against a fresh private ClickHouse with authentication enabled and fake providers. It verifies both cross-user isolation and same-user supersession across sessions. All 425 other focused unit tests pass; one ClickHouse-local test needs a binary with enable_quantized_codec. No blocking findings remain.

@zlareb1

zlareb1 commented Nov 19, 2026

Copy link
Copy Markdown
Member

@ningyixi could u check test failure?

@zlareb1
zlareb1 merged commit 551f0b3 into ClickHouse:main Nov 19, 2026
48 of 50 checks passed
@ningyixi

Copy link
Copy Markdown
Contributor Author

pytest unit part 6 passes on the merge commit 551f0b3: merge-run job. It also passes on main at 2e28b3b: main-run job.

The failing check is on PR head e1ef8c5: failed job. Its raw log and coverage-unit-6 artifact download are blocked in my environment, so the failing test and root cause remain unverified. The passing main runs alone do not establish that the failure is flaky.

Please attach the failure entry from junit-unit-6.xml, or the failed test name and traceback, if this failure still needs investigation. That gives the concrete case to reproduce without guessing at a code change.

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.

Same-key supersession and the value-update pass can retire another user's memory under a namespace-wide token

2 participants