Repository navigation
fix(backend): stop deleting the knowledge graph before the rebuild that replaces it - #11820
Merged
Merged
Conversation
…at replaces it POST /v1/knowledge-graph/rebuild deleted the legacy graph inline and only then scheduled `_rebuild_graph_task`, so any task that never ran to completion — process restart, pod eviction — left the user with no graph at all behind a 200 that said "rebuilding". The task also re-checks the mutation gate after the response is returned; an account that became assertion-backed (or whose canonical head read timed out) in that window no-ops the rebuild, which meant the delete stayed permanent with nothing left to own the graph. `rebuild_knowledge_graph` already clears the graph itself as its first step (utils/llm/knowledge_graph.py), so the route's delete was redundant: dropping it leaves the happy path byte-identical and shrinks the window where the user has no graph and nothing is rebuilding one. Tested: pytest tests/unit/test_knowledge_graph_canonical_mutation_routes.py — 20 passed; the three new/updated assertions fail against the unfixed route (deleted == ['uid-arbitrary-account']). Wider KG lane green: 130 passed, 1 skipped across the 7 knowledge-graph/rate-limit unit files. make preflight: 11 checks passed. Failure-Class: FC-split-mutation-authority fixes #11796
1 task done
This was referenced Aug 20, 2026
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.
What
POST /v1/knowledge-graph/rebuildno longer deletes the legacy knowledge graph beforescheduling the rebuild that replaces it. The delete now happens where the rebuild happens.
Why
From #11796. The route did:
Two ways a user ended up with no graph at all behind a 200 that said "rebuilding":
already committed; nothing is rebuilding anything.
_rebuild_graph_taskre-evaluates_legacy_graph_mutation_decisionafter the delete. An account that becameassertion-backed in that window — or, since the mutation-gate hardening, one whose
canonical head read times out and returns
UNVERIFIED— hits the early return, so thetask no-ops and the legacy graph stays deleted permanently.
The eager delete was also redundant:
rebuild_knowledge_graph(
backend/utils/llm/knowledge_graph.py:341) clears the graph itself as its first step.So the happy path is byte-identical, and both loss windows above close. Dropping the
duplicate owner is the point — the graph's delete transition now has exactly one owner,
the rebuild that immediately repopulates it.
Not addressed here (unchanged, pre-existing): a rebuild that dies after its own delete
still leaves the graph empty. Closing that needs the shadow-collection-and-swap the issue
describes, which is a much larger change than this one and does not belong in a fix that
can be verified hermetically.
What I tested
The three new/updated assertions are real regression coverage — against the unfixed route
they fail with
AssertionError: assert ['uid-arbitrary-account'] == []:test_rebuild_does_not_delete_the_graph_before_the_rebuild_runs— a scheduled task thatnever executes leaves the graph intact.
test_a_regate_that_flips_after_the_response_leaves_the_graph_intact— the accountbecomes assertion-backed between the route's gate check and the task's; the rebuild
no-ops and the graph is still there.
test_the_legacy_rebuild_still_clears_the_graph_itself— locks the invariant the routenow relies on, by calling the real
rebuild_knowledge_graph.test_legacy_principal_can_rebuildupdated: the route itself no longer deletes.Wider knowledge-graph lane, unchanged:
130 passed, 1 skippedacrosstest_knowledge_graph_canonical_mutation_routes.py,test_knowledge_graph_legacy_fallback.py,test_kg_prompt_node_cap.py,test_kg_prune_memory_citations.py,test_knowledge_graph_extract_helper.py,test_memory_graph_assertion_read.py,test_rate_limiting.py.make preflight: 11 checks passed.black --line-length 120 --skip-string-normalizationreports both files unchanged.
Not exercised against live Firestore — the loss window is a process death between two
writes, which is not reproducible hermetically. The route path itself is exercised through
the real FastAPI app via
TestClient, including the inline background task.Product invariants affected
none
Failure class (fixes)
Failure-Class: FC-split-mutation-authority
The legacy graph's delete transition had two owners — the route and
rebuild_knowledge_graph— and only the second one is the authority that also repopulatesit. This converges the transition on that single owner.
fixes #11796
Auto-generated from issue feedback by the mini issues-improver — tested and merged.