Skip to content

fix(kb): born-managed must not provision over an established legacy agent - #1109

Merged
DerrickF merged 1 commit into
developfrom
fix/born-managed-skip-legacy-agents
Sep 14, 2026
Merged

DerrickF merged 1 commit into
developfrom
fix/born-managed-skip-legacy-agents

Conversation

@DerrickF

Copy link
Copy Markdown
Contributor

Problem

Born-managed (MANAGED_KB_NEW_DEFAULT) is meant to provision a managed KB on a brand-new agent's first document. It decided "is this new?" by checking for a KB_Record row and treating its absence as new.

But legacy KBs are not first-class: they share one S3-Vectors index (assistants-index) and never write a KB_Record. So an established legacy agent looks identical to a brand-new one. Its next upload was mistaken for a first upload → born-managed stamped retrievalEngine=managed, flipping retrieval to the empty, still-provisioning managed KB and stranding the agent's existing corpus on the legacy index (only the new upload gets ingested into managed).

Reproduced in dev on ast-43c0869ddd07: an agent with an existing legacy doc kicked off "Provisioning knowledge base…" on the next upload.

Fix

Guard the record is None branch of begin_born_managed on an existing-documents check. Only provision when the agent has zero documents; an agent that has already ingested anything is an established legacy KB and stays legacy.

  • New assistant_has_documents() in document_service.py: cheap Select='COUNT', Limit=1 probe. No ownership check (the caller already authorised the upload — it answers a question about data, not access).
  • Fails toward legacy (returns True) on any error / missing table config: wrongly provisioning over a legacy agent loses its corpus from retrieval, whereas wrongly staying legacy is benign (Upgrade still works).
  • The _join path (record exists) is unchanged and was already correct.

Only MANAGED_KB_NEW_DEFAULT-on environments were affected (dev; prod flag is off).

Tests

  • Mutation guard: established legacy agent with documents but no record → no provisioning, no record written.
  • Fail-safe: an indeterminate probe keeps the agent on legacy.
  • Helper unit tests: true/false/fail-toward-legacy.
  • tests/lambdas/test_kb_born_managed.py 31 pass, tests/routes/test_documents.py 7 pass, ruff clean.

…gent

Born-managed treated the absence of a KB_Record as 'brand-new agent' and
provisioned a managed KB on the first upload. But legacy KBs are not
first-class: they share one S3-Vectors index and never write a KB_Record,
so an established legacy agent looks identical to a new one. Its NEXT
upload was therefore mistaken for a first upload, flipping retrieval to an
empty managed KB and stranding the existing corpus on the legacy index.

Guard the record-is-None branch on an existing-documents check
(assistant_has_documents: cheap COUNT, Limit=1, no ownership check). Only
provision when the agent has zero documents; fail toward legacy on any
probe error. Adds mutation-guard + helper unit tests (31 pass).
@DerrickF
DerrickF merged commit f5b4e61 into develop Sep 14, 2026
6 checks passed
@DerrickF
DerrickF deleted the fix/born-managed-skip-legacy-agents branch September 14, 2026 21:33
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.

1 participant