Skip to content

Erasing a code namespace removes its cached code.ask answers - #1585

Open
KaloyanNaumov wants to merge 5 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/erasure-answer-cache
Open

KaloyanNaumov wants to merge 5 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/erasure-answer-cache

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #1579

TL;DR: Erasing a code namespace left the cached code.ask answers asked in it in loom.code_answers, because the erasure step and the residue census matched only the memory an answer quoted. Both now also match the namespace the answer was asked in.

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

An erasure of a code namespace now deletes the cached code.ask answers asked in it, and the residue census counts them. Before, they stayed in loom.code_answers with the question, the answer text and the citations of the erased repository, and a re-index of the same commit served them again.

Why

code_answers.delete, the saga step that owns loom.code_answers, deleted by memory_namespace = {ns:String}. That column names the memory namespace an answer quoted (migration 086), '' for an answer drawn from the code index alone. So an erasure removed the answers that quoted the erased namespace's memories, in whichever code namespace they were asked, and no answer asked in the erased code namespace. The census row for the table used the same predicate, so it counted what the step deleted and the saga passed with the rest still there. The code index purge skips the table (KEEP_TABLES), so nothing else took them.

What stays is the question, the answer text the LLM wrote from the repository's code, and the cited paths, line spans and symbols. After a re-index of the same commit the cited files hash as they did, so the cache serves the answer again: it invalidates only when a cited or shown file changes (_files_current).

The fix is one predicate, ANSWER_NS_EQ = (namespace = {ns:String} OR memory_namespace = {ns:String}), used by the step and by the census row's ns_pred:

  1. The step still reads sequentially (sequential_reads) and still runs a lightweight DELETE: the table carries a vector column a mutation would rewrite. Every version goes, tombstones included, as before.
  2. A namespace list rewrites both equalities to IN through the existing ns_list_predicate (it replaces namespace = {ns:String} inside memory_namespace too), so the step and the census read one predicate for a list.
  3. The namespace census counts a version that holds an answer, citations or a question (answer != '' OR citations != '' OR question != ''). A negative entry, which an abstaining ask caches, holds only its question, so it counts; a tombstone keeps the question too, and counts until the same step deletes every version. The user-scope census still reads answer text and citations alone.

This reverses a position the code states. The comment on KEEP_TABLES in server/code/app/index/retire.py reads: "Telemetry stays: what a caller searched and what was answered is the deployment's audit trail, keyed by namespace but not the repository's content." Our argument against it:

  1. Once a real provider is routed, an answer row holds the cited paths, line spans and symbols and the model's text written from the code, so it is the repository's content.
  2. The memory half of the same table is already deleted on erasure for exactly that reason (An erasure deletes the cached code.ask answers that quoted the erased memory #332).
  3. copy_residue (server/erasure/store/provider_copies.py:55-60) already counts a rebuild copy of this table by namespace IN ... OR memory_namespace IN ..., so main's own copy census treats an answer asked in the namespace as residue. The main-table census was the outlier.
  4. A kept row is served again after a re-index of the same commit (_files_current), which an audit trail should not do.

The evidence is thin on one side: the one row we found was a fake-LLM echo with no repository code, and the re-serve comes from reading the code, not from observation. If answers are meant to stay as an audit record, the smaller change keeps the rows but stops serving them once their namespace is erased, and states in the docs that the residue claim does not cover them; we will implement whichever you prefer. The comment on KEEP_TABLES now says only what is not purged there.

Alternatives: answers-purge --apply after an erasure is today's workaround. It is an operator step the erasure runbook does not name, it tombstones rather than deletes, and it runs after the saga has reported residue 0. Purging the table in code_index.purge as one more content table would split one table across two steps, and its code_answers.delete ownership in both scopes would still have to stay for the memory side.

Scope and risk

server/erasure/store/predicates.py (one constant), steps_graph.py (the step), census.py (the census row and its comment), the comment on KEEP_TABLES in server/code/app/index/retire.py, server/code/app/answers_cache.py and server/code/store/index/answers.py (the write guard reaches every answer, with a delete by entry key), two sentences in docs/architecture/memory-architecture.md and storage-layout.md, and the tests. No wire, setting, migration or SDK change. A user-scope erasure and memory.redact are untouched.

The delete takes more rows than before, all of them cache: the next ask in a re-created namespace synthesizes again.

An ask in flight while the saga runs can store an answer after the delete. The fence that handles this (_remember before the store, _check_stored after it, both reading the erasure ledger) now runs for every answer, not only one that quoted memory, and for the negative entry an abstain writes: a code-only answer is not cached while an erasure of the org had not completed when the ask began, and one that lands after the census is tombstoned and deleted by its own key (delete_answer_entry, one lightweight delete). The same check guards the copy a release branch makes of its trunk's entry. The cost is one ledger read per stored answer. The redaction stamp is still read only for an answer that quoted memory keys. During a mixed rollout an N-1 replica's asks stay unguarded for a code-only answer; the census still catches such a row, the saga fails at verify.residue, steps_to_reopen re-opens code_answers.delete, and the recovery worker deletes it on its next try.

Compatibility, rollout and rollback

  • Wire: none. No setting, no migration.
  • Operator steps: none for new erasures; after the upgrade, loom code answers-purge is no longer needed after one. An erasure completed on N-1, or before the upgrade, keeps its answers, and loom erasure-re-verify does not reach this table, so run loom code answers-purge --namespace <ns> --all --apply for each code namespace erased before then.
  • Mixed versions: the recovery worker resumes a saga on whichever replica picks it up. A saga that N-1 checkpointed past code_answers.delete and N resumes fails one census at verify.residue on the answers asked in the namespace; the next attempt re-opens the step and completes, within the worker's attempt limit. A saga that N started and N-1 resumes passes N-1's narrower census.
  • Rollback: reverting the commit restores the old predicates. Nothing is written that the old build cannot read.

How this was found

Found and reproduced on our deployment with AI assistance. Fixed on a fork, PR submitted.

Tests

  • tests/unit/test_erasure_code_index_purge.py, three CH-free cases on the step and the census row, with a recording ClickHouse stub. They assert the count and the DELETE FROM loom.code_answers filter on namespace itself, not only memory_namespace, for one namespace and for a list, and that the census predicate matches both columns. On main all three fail (3 failed, 6 passed). On this branch the file passes (9 passed).
  • tests/unit/test_erasure_census_tail.py: the namespace census predicate for the table includes question != '' (CH-free; fails without it).
  • tests/unit/test_erasure_code_answers.py, cases on clickhouse-local with the real table: an answer asked in the erased namespace is counted by the census, deleted by the step with the other versions, and an answer of another code namespace stays; a negative entry holding only its question is counted and deleted; a tombstone with no answer text counts in the namespace census through its question and not in the user census. These cases were not run here: the host has no clickhouse binary, so the module skips. It is written to the module's existing fixture shape and needs a run in CI or on a host with clickhouse.
  • tests/unit/test_code_ask_cache_v2.py: test_a_code_only_answer_is_not_cached_while_an_erasure_may_have_missed_it (an unfinished or unreadable erasure ledger keeps a code-only answer out of the cache) and test_a_code_only_answer_stored_before_an_erasure_marks_is_deleted_again (the ledger reads clean before the store and marked after it, standing for a write that lands after the census: the row is tombstoned and no version keeps the text). Both fail without the guard on every answer; test_an_answer_with_no_memory_reads_no_erasure_ledger is replaced by the first. The same pair covers an abstain: test_an_abstain_is_not_cached_while_an_erasure_may_have_missed_it (an unfinished or unreadable ledger keeps the negative entry out of the cache) and test_an_abstain_stored_before_an_erasure_marks_is_deleted_again (the row is tombstoned and deleted by its key); both fail without the guard on the negative write.
  • Measured here, on the final head: pytest tests/unit/test_erasure_code_index_purge.py tests/unit/test_erasure_code_answers.py tests/unit/test_erasure_step_order.py tests/unit/test_erasure_verify.py tests/unit/test_storage_layout_rows.py tests/unit/test_public_copy.py tests/unit/test_tunable_constants.py tests/unit/test_doc_links.py tests/unit/test_provider_copy_residue.py tests/unit/test_erasure_cache_key_axis.py tests/unit/test_erasure_repo.py tests/unit/test_cell_scan_tables.py tests/unit/test_erasure_census_tail.py tests/unit/test_code_ask_cache_v2.py -q -p no:randomly -n 2 gives 503 passed, 22 skipped (the skips are the clickhouse-local cases). ruff check, ruff format --check and mypy pass on the touched files, and prettier passes on the changed docs. The full unit suite was not run here; CI runs it.

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 the tests. Checked: I reviewed the whole diff and its tests; the runs listed under Tests are the agent's, against main at the same commit.

  • 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: no op, setting, table or column changed, so none needs regenerating.
  • A new migration is additive and numbered after the latest on main; no migration is added.
  • 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: no plugin file changed.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed: it did not.
  • 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.

Note

Medium Risk
Changes erasure deletion scope and answer-cache write paths for all orgs with in-flight erasures; behavior is intentional but touches compliance-sensitive data retention.

Overview
Fixes a gap where namespace erasure only removed cached code.ask rows matched by memory_namespace, leaving answers asked in the erased code namespace (code-only or not) in loom.code_answers to be served again after re-index.

Erasure and census now share ANSWER_NS_EQ (namespace OR memory_namespace), and the namespace census treats rows with only a question (negative/abstain entries) as residue until code_answers.delete runs.

Caching renames the guard to _answer_may_be_gone and applies the unfinished erasure ledger check to every memoized answer and negative entry—not only memory-quoting answers. Post-store cleanup can delete_answer_entry by question_id when there are no memory keys. Docs and KEEP_TABLES comments are aligned with answers being deletable cache, not retained audit telemetry.

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

The erasure step for cached code.ask answers deleted only those that quoted the
erased namespace's memories (`memory_namespace`). An answer asked in the erased code
namespace carries the repository's code in its text and was left in
`loom.code_answers`, uncounted by the residue census. After a re-index of the same
commit it was served again, since the cache invalidates only when a cited file changes.

The step and the census now match a row by `namespace` as well as `memory_namespace`.
The delete stays a lightweight delete read sequentially, and the census still skips a
tombstone that holds no text.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
The architecture doc still said an answer drawn from the code index alone stays;
a namespace erasure now deletes it with the code namespace it was asked in. The
module docstring of the answer-cache erasure tests, the `namespace_tables`
docstring and one test docstring described the old predicate, and the comment on
`KEEP_TABLES` now says only what is not purged there.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@vercel

vercel Bot commented Nov 26, 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 26, 2026 11:27pm UTC
loom-telemetry Ready Ready Preview Nov 26, 2026 11:27pm UTC

Request Review

@zlareb1 zlareb1 self-assigned this Nov 26, 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.

Namespace erasure reaches existing code-only answers. The cache writer also needs to prevent a concurrent ask from restoring erased text.

Comment thread server/erasure/store/steps_graph.py Resolved
@KaloyanNaumov
KaloyanNaumov marked this pull request as draft November 26, 2026 20:49
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Converting to draft while I address it; a new head and the replies will follow, so no need to look at this head again.

… ones

An ask can finish synthesizing after the namespace delete and the final
census. The answer cache skipped its erasure check when the answer quoted no
memory, so a code-only answer could land in the erased namespace. Run the
before-and-after write guard for every answer, and delete a code-only answer
that lands after the mark by its own key.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@KaloyanNaumov
KaloyanNaumov marked this pull request as ready for review November 26, 2026 21:05

@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 synthesis and branch-reuse paths now check erasure around cache writes. Negative cache entries still need the same protection.

Comment thread server/erasure/store/census.py Resolved
@KaloyanNaumov
KaloyanNaumov marked this pull request as draft November 26, 2026 22:12
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Thanks, I've seen the new comments. Back to draft while I address them; a new head and the replies will follow.

KaloyanNaumov and others added 2 commits November 26, 2026 22:20
…ounted by its census

An abstaining ask that overlapped a namespace delete could recreate its cached
question afterwards: the negative entry was written without the erasure check,
and the census skipped it because its answer and citations are empty. The
negative write now runs the same before-and-after guard as a synthesized
answer, and the namespace census counts a row that holds a question.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…nly guard

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov
KaloyanNaumov marked this pull request as ready for review November 26, 2026 23:26

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

Namespace erasure deletes positive and negative cache entries, including rows that hold only the question. The write guards cover a concurrent erasure. All 142 focused tests passed with ClickHouse 26.11.2.4.

This branch has not been deployed

No deployments
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.

Erasing a code namespace leaves its cached code.ask answers

2 participants