Skip to content

fix: call reindexAll() when embedding config changes to preserve vector embeddings#200

Open
gkd2323c wants to merge 2 commits into
TencentCloud:mainfrom
gkd2323c:fix/auto-reindex-on-config-change
Open

fix: call reindexAll() when embedding config changes to preserve vector embeddings#200
gkd2323c wants to merge 2 commits into
TencentCloud:mainfrom
gkd2323c:fix/auto-reindex-on-config-change

Conversation

@gkd2323c

Copy link
Copy Markdown

Summary

When the embedding provider/model/dimensions config changes, initSchema() in sqlite.ts correctly detects the change via the needsReindex flag and drops the vector tables. However, tdai-core.ts ignores this flag entirely — reindexAll() (which is already fully implemented in sqlite.ts) is never called.

This means all historical L1 records and L0 messages permanently lose their vector embeddings after a config change. Users must either manually re-embed data or wait for new conversations to accumulate vectors.

Root Cause

In tdai-core.ts, the initStores() method destructures vectorStore and embeddingService from the store initialization result, but silently drops needsReindex and reindexReason:

const stores = await initStores(this.cfg, this.dataDir, this.logger);
this.vectorStore = stores.vectorStore;
this.embeddingService = stores.embeddingService;
// stores.needsReindex is never checked!

The reindexAll() method in sqlite.ts (lines 1810–1880) is complete and handles both L1 and L0 re-embedding with progress callbacks — it just was never wired up.

Fix

After initStores() returns, check stores.needsReindex and call reindexAll() with the embedding service to automatically re-embed all existing records.

if (stores.needsReindex && this.vectorStore && this.embeddingService) {
  this.logger.info(
    `${TAG} Reindex triggered: ${stores.reindexReason ?? "config changed"}. Re-embedding all records...`,
  );
  const embedFn = (text: string) => this.embeddingService!.embed(text);
  const reindexResult = await this.vectorStore.reindexAll(embedFn, (done, total, layer) => {
    if (done % 20 === 0 || done === total) {
      this.logger.debug?.(`${TAG} Reindex progress: ${layer} ${done}/${total}`);
    }
  });
  this.logger.info(
    `${TAG} Reindex complete: L1=${reindexResult.l1Count}, L0=${reindexResult.l0Count}`,
  );
}

Related

  • Fixes [Bug] #164 (vector search permanently returns 0 after config change)
  • The reindexAll() method already exists in sqlite.ts and is well-tested; this PR simply connects the missing call site.

gkd2323c added 2 commits June 13, 2026 00:55
When the embedding provider/model/dimensions config changes, initSchema()
correctly detects the change via the needsReindex flag and drops the vector
tables. However, tdai-core.ts ignores this flag entirely, so reindexAll()
—which already exists and is fully implemented in sqlite.ts—is never called.

This means all historical L1 records and L0 messages permanently lose their
vector embeddings after a config change. The user must manually re-embed
data or wait for new conversations to accumulate vectors.

Fix: after initStores() returns, check stores.needsReindex and call
reindexAll() with the embedding service to automatically re-embed all
existing records.

Fixes TencentCloud#164
When the embedding provider/model/dimensions config changes, initSchema()
correctly detects the change via the needsReindex flag and drops the vector
tables. However, tdai-core.ts ignores this flag entirely, so reindexAll()
—which already exists and is fully implemented in sqlite.ts—is never called.

This means all historical L1 records and L0 messages permanently lose their
vector embeddings after a config change. The user must manually re-embed
data or wait for new conversations to accumulate vectors.

Fix: after initStores() returns, check stores.needsReindex and call
reindexAll() with the embedding service to automatically re-embed all
existing records.

Fixes TencentCloud#164
@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Good catch on the missing reindexAll() call! When embedding config changes, vectors should be regenerated instead of permanently lost. We'll review it soon.

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.

[Bug]

2 participants