-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(knowledge): bound chunking separators so one config can't stall processing #6735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| chunkingStrategyOptionsSchema, | ||
| createKnowledgeBaseBodySchema, | ||
| knowledgeBaseDataSchema, | ||
| } from '@/lib/api/contracts/knowledge/base' | ||
| import { MAX_CHUNKING_SEPARATOR_LENGTH, MAX_CHUNKING_SEPARATORS } from '@/lib/chunkers/constants' | ||
|
|
||
| const separators = (count: number) => Array.from({ length: count }, (_, i) => `@@sep${i}@@`) | ||
|
|
||
| describe('chunkingStrategyOptionsSchema.separators', () => { | ||
| it('accepts a separator list at the bound', () => { | ||
| const parsed = chunkingStrategyOptionsSchema.parse({ | ||
| separators: separators(MAX_CHUNKING_SEPARATORS), | ||
| }) | ||
| expect(parsed.separators).toHaveLength(MAX_CHUNKING_SEPARATORS) | ||
| }) | ||
|
|
||
| it('rejects more separators than the bound', () => { | ||
| const result = chunkingStrategyOptionsSchema.safeParse({ | ||
| separators: separators(MAX_CHUNKING_SEPARATORS + 1), | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects a separator longer than the per-item bound', () => { | ||
| const result = chunkingStrategyOptionsSchema.safeParse({ | ||
| separators: ['|'.repeat(MAX_CHUNKING_SEPARATOR_LENGTH + 1)], | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects an oversized list on the knowledge base create body', () => { | ||
| const result = createKnowledgeBaseBodySchema.safeParse({ | ||
| name: 'kb', | ||
| workspaceId: 'ws', | ||
| chunkingConfig: { | ||
| maxSize: 1024, | ||
| minSize: 100, | ||
| overlap: 200, | ||
| strategy: 'recursive', | ||
| strategyOptions: { separators: separators(5000) }, | ||
| }, | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('knowledgeBaseDataSchema.chunkingConfig', () => { | ||
| it('still reads a stored config written before the separator bound', () => { | ||
| const result = knowledgeBaseDataSchema.safeParse({ | ||
| id: 'kb-1', | ||
| userId: 'u-1', | ||
| name: 'kb', | ||
| description: null, | ||
| tokenCount: 0, | ||
| embeddingModel: 'text-embedding-3-small', | ||
| embeddingDimension: 1536, | ||
| chunkingConfig: { | ||
| maxSize: 1024, | ||
| minSize: 100, | ||
| overlap: 200, | ||
| strategy: 'recursive', | ||
| strategyOptions: { separators: separators(5000) }, | ||
| }, | ||
| createdAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| deletedAt: null, | ||
| workspaceId: 'ws', | ||
| folderId: null, | ||
| }) | ||
| expect(result.success).toBe(true) | ||
| }) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| * Bounds on the separator list a recursive chunking config may carry. | ||
| * | ||
| * `RecursiveChunker` scans the whole document once per separator and walks the | ||
| * list from the top for every oversized fragment, so the separator count is a | ||
| * direct multiplier on synchronous CPU per document. The work happens inside a | ||
| * split loop, which neither the processing `Promise.race` timeout nor the | ||
| * after-the-fact chunk-count cap can interrupt — the list has to be bounded on | ||
| * the way in instead. | ||
| * | ||
| * The largest built-in recipe (`markdown`) uses 16 separators, so 32 leaves room | ||
| * for a hand-tuned list without letting one config stall the processing tier. | ||
| */ | ||
| export const MAX_CHUNKING_SEPARATORS = 32 | ||
|
|
||
| /** Max characters in a single chunking separator. Real delimiters are a few characters. */ | ||
| export const MAX_CHUNKING_SEPARATOR_LENGTH = 100 |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.