fix(agent): resolve fabricated chunk citations#1325
Open
wolfkill wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #1323.
Smart-reasoning final answers can sometimes cite fabricated chunk IDs such as
<knowledge_id>_chunk_<index>instead of the UUID chunk IDs returned byknowledge_search/grep_chunks. The frontend then calls/api/v1/chunks/by-id/{chunk_id}and receivesChunk not found.This PR keeps the normal UUID lookup path unchanged, then adds a compatibility fallback only when the direct chunk lookup misses:
uuid_chunk_<index>references.knowledge_id + chunk_index, preferring text chunks when multiple chunk types share an index.<kb doc="..." chunk_id="..."/>citations with the exact tool-returnedchunk_id, and does not derive IDs fromknowledge_id/chunk_index.Testing
CGO_LDFLAGS='-lc++' go test ./internal/application/service -run 'TestGetChunkByIDOnly(ResolvesFabricatedKnowledgeChunkReference|RejectsMalformedFabricatedReference)' -count=1CGO_LDFLAGS='-lc++' go test ./internal/application/repository -run TestGetChunkByKnowledgeIDAndIndexOnlyPrefersTextChunk -count=1CGO_LDFLAGS='-lc++' go test ./internal/application/repository ./internal/application/service ./internal/handler -run '^$' -count=1CGO_LDFLAGS='-lc++' go test ./internal/agent -run '^$' -count=1git diff --checkgo run ./cmd/server: inserted a temporary KB/knowledge/chunk, verified both the real UUID andknowledge_id_chunk_7return the same chunk through/api/v1/chunks/by-id/{id}, verified malformed fallback IDs still return 404, then cleaned up the temp rows and restored the local test tenant API key.Notes
No database migration is required. The fallback query is only used after direct UUID lookup returns not found.