Skip to content

fix: refresh Go chunk vectors when embedding input changes - #20552

Open
GoldArowana wants to merge 2 commits into
infiniflow:mainfrom
GoldArowana:fix/go-update-chunk-embeddings
Open

GoldArowana wants to merge 2 commits into
infiniflow:mainfrom
GoldArowana:fix/go-update-chunk-embeddings

Conversation

@GoldArowana

Copy link
Copy Markdown

Summary

UpdateChunk changed content or questions without regenerating the stored embedding vector, so retrieval could use a vector for the previous effective text.

This change compares the old and new effective embedding body after applying partial-update semantics. It preserves omitted content/questions, accepts stored questions as either []string or []interface{}, trims and newline-joins nonempty questions, and uses content when there are no nonempty questions. Explicit questions: [] clears questions. Content-only edits with retained nonempty questions, equivalent effective text, and unrelated field edits skip embedding.

When that body changes, the authorized knowledgebase supplies the tenant and EmbdID. The service follows AddChunk's existing contract: embed document title and body with Dimension 0, require two nonempty equal-dimension vectors, combine 0.1 * title + 0.9 * body, and include q_<dim>_vec in the index update. Model resolution, embedding, and vector validation finish before image storage and index writes. Original document authorization and {id, doc_id} update conditions are preserved.

Regression coverage

Offline deterministic stubs cover content edits without questions, changed questions, omitted fields, explicit empty arrays, both stored question-array types, whitespace normalization, unchanged effective input, unrelated updates, nil title, and the exact weighted vector [28, 38, 48]. Tests also verify the selected tenant/model, two exact embedding texts, Dimension 0, and original document scope.

Seven failure cases cover model lookup/provider errors, 0/1/3 vectors, empty dimensions, and unequal dimensions, asserting no index/image/Wiki writes. A mixed image/content request verifies embedding runs before image storage. Existing image-removal behavior still has its own independent side effects: this change does not provide transaction rollback across image storage and the index.

The new regression group failed before the fix (3 of 4 main tests and 16 of 27 subtests failed, exit 1). After the fix, all 4 main tests and 27 subtests pass. Changes do not extend to question_tks, model selection, or index schema.

Validation

  • Go 1.27.1, CGO enabled, cgo,static tags, verified local native dependencies and module checksums; all embeddings use deterministic stubs.
  • Unchanged project build.sh test entry point, main --test -p 2 -v ./internal/service/chunk/... ./internal/handler/...: 543 main tests and 171 subtests passed, exit 0, no compiler warnings.
  • The environment mounts the build script's default /home/agent native cache read-only. Validation sourced the unchanged build.sh, redirected its cache-prefix variables to verified workspace caches, and invoked its main --test entry point with the arguments above. No build script, module, or environment changes are included in this PR.
  • The appended native tier returned success. A verbose check confirmed the fixture-free crop test passes (1 main/3 subtests); the parent DeepDoc native test binary deliberately skips because external test fixtures were not fetched. Model-backed integration tests and the full service stack were not run.
  • Independent review completed, and the normal Lefthook pre-commit checks passed without bypasses.

Upstream CI and maintainer review remain merge gates; local package validation is not a full RAGFlow readiness claim.

Preserve omitted content and questions and compare the effective embedding body before resolving the dataset embedding model. Generate and validate the weighted title/body vector before image and index writes, with offline regressions for update semantics and failures.
@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4e125786-85da-4ce0-8228-52aed26d325c
Commits

Reviewing files that changed from the base of the PR and between 2ecc93e and 7900894.

Files selected for processing: 3
  • internal/service/chunk/chunk.go
  • internal/service/chunk/chunk_test.go
  • internal/service/chunk/chunk_update_embedding_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

UpdateChunk preserves stored content and questions when replacements are omitted. It derives embedding text from questions or content and refreshes the chunk vector when that text changes. Tests cover input handling, failures, operation order, and authorization.

Changes: Chunk update embedding

Layer / File(s) Summary
Select update text and refresh embeddings
internal/service/chunk/chunk.go, internal/service/chunk/chunk_test.go, internal/service/chunk/chunk_update_embedding_test.go
UpdateChunk preserves omitted content and questions, trims supplied questions, and uses content when no usable questions remain. When embedding text changes, it resolves the embedding model, embeds the document name and text, and updates the vector. Tests check the update values and vector.
Validate failures, ordering, and authorization
internal/service/chunk/chunk_test.go, internal/service/chunk/chunk_update_embedding_test.go
Tests verify that model, provider, result-count, and vector-dimension errors prevent writes. They also check that embedding precedes image storage and that authorization failures prevent model lookup and embedding.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: buua436

Merge Risk: Minimal · up to 79008

No actionable issue identified; the change is ready for normal merge checks.

Security architecture risk: Low · up to 2ecc9

The update preserves existing access checks and rejects embedding failures before changing stored data. No new authorization bypass was identified. Deployment-specific handling of text sent for embedding and concurrent-update guarantees remain incompletely established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — An authenticated editor can now cause document title and request- or stored-field-derived text to reach the configured embedding driver during a chunk update. This is a new update-time data flow compared with base, scoped by the selected knowledge base. Provider deployment and redaction/egress policy were not established.

Trust Boundaries and Controls

  • observed — The handler supplies authenticated user identity. Before embedding, the service checks accessible tenant membership, tenant-scoped dataset lookup, document membership, and stored chunk document identity. The index update retains the authorized tenant index, dataset argument, and original id/document condition; the inspected Elasticsearch entrypoint adds dataset scoping.

Resilience and Maintainability Implications

  • observed — Model resolution, embedding, and vector validation failures return before image storage, index mutation, or Wiki invalidation. Subsequent image/index cleanup ordering remains as in base: image creation precedes index publication, and image removal follows it. The flow is not a cross-store transaction; those later partial-failure limitations predate this PR.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: refreshing Go chunk vectors when embedding input changes.
Description check Passed The description includes the required Summary section and provides clear background, implementation details, regression coverage, and validation results.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the chunk’s new text,
And keeps the questions still intact.
Two vectors merge, the index waits,
No write proceeds when errors rise.
The bunny hops; tests guard the path.

Comment @coderabbitai help to get the list of available commands.

@qinling0210

Copy link
Copy Markdown
Contributor

@GoldArowana Please resolve the conflicts

@GoldArowana

Copy link
Copy Markdown
Author

Merged current main into this branch in 7900894 and resolved the UpdateChunk conflict by retaining the new dataset permission check. Embedding lookup now uses the authorized knowledgebase returned by that check; the regression fixture and denied-access assertion were updated for the current API.

Validation: bash build.sh --cpp and bash build.sh --test ./internal/service/chunk/... passed on Linux; the latter also ran the native and native/croptest unit packages. Relevant formatting and file checks passed. This was Codex-assisted and independently reviewed by another Codex agent; no live model or external service behavior was tested.

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

Walked the refresh logic end to end. Change detection compares the rebuilt embedding text against the stored one through the same buildChunkEmbeddingText fallback chain (trimmed, non-empty questions joined with a newline, otherwise the content), so trim-equivalent edits, the legacy content field, interface-typed stored questions, and explicit-empty replacements all behave consistently - the 16-case table pins each of these. The re-embed itself reuses the exact insert-path shape: Texts [docName, embeddingText], Dimension 0, and mergeChunkEmbeddings at 0.1 title + 0.9 chunk, the same weights and ordering used when chunks are embedded during parsing. I checked the test arithmetic too: the [10,20,30]/[30,40,50] stub produces the expected [28,38,48]. Unrelated updates (available, positions, tags, keywords) correctly skip the model lookup entirely.

Source-level review only, I could not run go test in this environment.

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.

3 participants