aws-samples / aws-samples/bedrock-chat

[BUG] Shared KB: file-only modifications clear knowledge_base_id and skip ingestion

Open
#1,091 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
1.3k
Forks
535
Avg merge
1d 12h
Merged PRs (30d)
10

Description

## 🐞 Describe the bug

**Shared KB bots lose their `knowledge_base_id` after file-only modifications (v3 with shared KB feature)**

When a shared KB bot is modified by adding/removing files without changing the KB configuration (embeddings model, chunking strategy, etc.), two bugs occur:

1. `modify_owned_bot` calls `model_copy(update=modify_input.bedrock_knowledge_base.model_dump())` which overwrites `knowledge_base_id` with `None` (frontend sends `knowledgeBaseId: null`)
2. `is_sync_shared_knowledge_bases_required` returns `False` (hash unchanged) → `SyncSharedKnowledgeBasesRequired=False` → `FinalizeSharedKnowledgeBasesBuild` never runs → `knowledge_base_id` is never restored

The bot permanently loses its KB association in DynamoDB after any file-only edit.

## 🔄 To Reproduce

1. Create a shared KB bot with a file — KB syncs successfully, `knowledge_base_id` is set
2. Edit the bot to add another file (no KB config changes)
3. Observe `knowledge_base_id = null` in DynamoDB after the edit
4. Bot can no longer query its knowledge base

## 🔎 Root Cause

In `backend/app/usecases/bot.py`:
```python
updated_kb = current_bot_kb.model_copy(
update=modify_input.bedrock_knowledge_base.model_dump() # overwrites knowledge_base_id with None
)
```

In `backend/app/routes/schemas/bot.py`, `is_sync_shared_knowledge_bases_required` only returns `True` when the KB config hash changes — file-only changes return `False`, skipping `FinalizeSharedKnowledgeBasesBuild` which is the only step that restores `knowledge_base_id`.

## ✅ Suggested Fix

1. In `modify_owned_bot`, preserve `knowledge_base_id` and `data_source_ids` when merging KB config:
```python
updated_kb = current_bot_kb.model_copy(
update={
k: v for k, v in modify_input.bedrock_knowledge_base.model_dump().items()
if k not in ("knowledge_base_id", "data_source_ids") or v is not None
}
)
```

2. In `is_sync_shared_knowledge_bases_required`, always return `True` for shared KB bots so `FinalizeSharedKnowledgeBasesBuild` always runs:
```python
if self.bedrock_knowledge_base.type == "shared" or current_bot_model.bedrock_knowledge_base.type == "shared":
return True
```

## Environment

- Version: v3 (shared KB feature)
- Affected: `backend/app/usecases/bot.py`, `backend/app/routes/schemas/bot.py`

Contributor guide

Open the contributing guide

Research direction

Start in backend/app/usecases/bot.py at modify_owned_bot and then inspect is_sync_shared_knowledge_bases_required in backend/app/routes/schemas/bot.py. Trace the shared knowledge-base edit flow and its finalization step; done means file-only modifications preserve knowledge_base_id and still restore or retain the association so the bot can query its knowledge base.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
backend, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.