Skip to content

fix(backend): stop deleting the knowledge graph before the rebuild that replaces it - #11820

Merged
kodjima33 merged 1 commit into
mainfrom
issues-improver/11796-rebuild-eager-delete
Aug 19, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
issues-improver/11796-rebuild-eager-delete

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

What

POST /v1/knowledge-graph/rebuild no longer deletes the legacy knowledge graph before
scheduling the rebuild that replaces it. The delete now happens where the rebuild happens.

Why

From #11796. The route did:

_require_legacy_graph_mutation(uid)
user_name = get_user_name(uid) or ""
kg_db.delete_knowledge_graph(uid)          # committed before any rebuild work
background_tasks.add_task(_rebuild_graph_task, uid, user_name)
return RebuildResponse(status="rebuilding", ...)

Two ways a user ended up with no graph at all behind a 200 that said "rebuilding":

  1. The task never runs to completion — process restart, pod eviction. The delete is
    already committed; nothing is rebuilding anything.
  2. The re-checked gate says no. _rebuild_graph_task re-evaluates
    _legacy_graph_mutation_decision after the delete. An account that became
    assertion-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 the
    task 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

$ pytest tests/unit/test_knowledge_graph_canonical_mutation_routes.py
20 passed in 2.20s

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 that
    never executes leaves the graph intact.
  • test_a_regate_that_flips_after_the_response_leaves_the_graph_intact — the account
    becomes 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 route
    now relies on, by calling the real rebuild_knowledge_graph.
  • test_legacy_principal_can_rebuild updated: the route itself no longer deletes.

Wider knowledge-graph lane, unchanged: 130 passed, 1 skipped across
test_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-normalization
reports 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 repopulates
it. This converges the transition on that single owner.

fixes #11796

Auto-generated from issue feedback by the mini issues-improver — tested and merged.

Review in cubic

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

POST /v1/knowledge-graph/rebuild deletes the legacy graph before the rebuild worker runs, with no recovery if the worker dies

1 participant