From 45c94b4c4f754d33115852b285d87f9b252e09b6 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Thu, 9 Jul 2026 23:00:23 +0800 Subject: [PATCH 1/4] fix(core): bound glob result collection --- packages/core/src/tools/glob.test.ts | 68 ++++++++++++++---- packages/core/src/tools/glob.ts | 100 +++++++++++++++++---------- 2 files changed, 120 insertions(+), 48 deletions(-) diff --git a/packages/core/src/tools/glob.test.ts b/packages/core/src/tools/glob.test.ts index 0e680c80420..0efac677ad1 100644 --- a/packages/core/src/tools/glob.test.ts +++ b/packages/core/src/tools/glob.test.ts @@ -79,21 +79,43 @@ describe('GlobTool', () => { await fs.rm(tempRootDir, { recursive: true, force: true }); }); - const mockTruncationGlobResults = (prefix: string, count: number) => { + const mockGlobStreamResults = ( + prefix: string, + count: number, + extension = 'streamlimit', + ) => { const baseMtimeMs = Date.now(); - const entries = Array.from( - { length: count }, - (_, index): GlobResultPath => { + let yielded = 0; + + async function* streamEntries() { + for (let index = 0; index < count; index++) { + yielded++; const fileNumber = index + 1; - return { + yield { fullpath: () => - path.join(tempRootDir, `${prefix}${fileNumber}.trunctest`), + path.join(tempRootDir, `${prefix}${fileNumber}.${extension}`), mtimeMs: baseMtimeMs + fileNumber, } as unknown as GlobResultPath; - }, + } + } + + const iterator = streamEntries(); + const stream = { + [Symbol.asyncIterator]: () => iterator, + destroy: vi.fn(() => { + void iterator.return?.(); + }), + }; + + vi.mocked(glob.globStream).mockReturnValueOnce( + stream as unknown as ReturnType, ); - vi.mocked(glob.glob).mockResolvedValueOnce(entries); + return { getYielded: () => yielded, destroy: stream.destroy }; + }; + + const mockTruncationGlobResults = (prefix: string, count: number) => { + mockGlobStreamResults(prefix, count, 'trunctest'); }; describe('execute', () => { @@ -280,7 +302,13 @@ describe('GlobTool', () => { }); it('should return a GLOB_EXECUTION_ERROR on glob failure', async () => { - vi.mocked(glob.glob).mockRejectedValue(new Error('Glob failed')); + vi.mocked(glob.globStream).mockReturnValueOnce({ + [Symbol.asyncIterator]: () => ({ + next: async () => { + throw new Error('Glob failed'); + }, + }), + } as unknown as ReturnType); const params: GlobToolParams = { pattern: '*.txt' }; const invocation = globTool.build(params); const result = await invocation.execute(abortSignal); @@ -288,8 +316,6 @@ describe('GlobTool', () => { expect(result.llmContent).toContain( 'Error during glob search operation: Glob failed', ); - // Reset glob. - vi.mocked(glob.glob).mockReset(); }); }); @@ -701,14 +727,14 @@ describe('GlobTool', () => { ); await fs.mkdir(path.join(tempRootDir, 'node_modules')); - vi.mocked(glob.glob).mockClear(); + vi.mocked(glob.globStream).mockClear(); const invocation = new GlobTool(mockConfig).build({ pattern: '**/*.keep', }); await invocation.execute(abortSignal); - const lastCall = vi.mocked(glob.glob).mock.calls.at(-1); + const lastCall = vi.mocked(glob.globStream).mock.calls.at(-1); const globOptions = lastCall?.[1] as | { ignore?: { ignored?: unknown; childrenIgnored?: unknown } } | undefined; @@ -795,6 +821,22 @@ describe('GlobTool', () => { }); describe('file count truncation', () => { + it('stops collecting glob results at a bounded scan limit', async () => { + const totalResults = 10_005; + const stream = mockGlobStreamResults('limit', totalResults); + + const params: GlobToolParams = { pattern: '*.streamlimit' }; + const invocation = globTool.build(params); + const result = await invocation.execute(abortSignal); + const llmContent = partListUnionToString(result.llmContent); + + expect(stream.getYielded()).toBeLessThan(totalResults); + expect(stream.destroy).not.toHaveBeenCalled(); + expect(llmContent).toContain('Found at least'); + expect(llmContent).toContain('Narrow the pattern or path'); + expect(result.returnDisplay).toContain('(truncated)'); + }); + it('should truncate results when more than 100 files are found', async () => { mockTruncationGlobResults('file', 150); diff --git a/packages/core/src/tools/glob.ts b/packages/core/src/tools/glob.ts index bc399d276b8..87ad2b2942a 100644 --- a/packages/core/src/tools/glob.ts +++ b/packages/core/src/tools/glob.ts @@ -6,7 +6,7 @@ import fs from 'node:fs'; import path from 'node:path'; -import { glob, escape } from 'glob'; +import { globStream, escape } from 'glob'; import type { ToolInvocation, ToolResult } from './tools.js'; import { BaseDeclarativeTool, BaseToolInvocation, Kind } from './tools.js'; import { ToolNames, ToolDisplayNames } from './tool-names.js'; @@ -32,6 +32,7 @@ import { isPathWithinRoot } from '../utils/workspaceContext.js'; const debugLogger = createDebugLogger('GLOB'); const MAX_FILE_COUNT = 100; +const MAX_GLOB_COLLECTED_ENTRIES = 10_000; // Subset of 'Path' interface provided by 'glob' that we can implement for testing export interface GlobPath { @@ -39,6 +40,11 @@ export interface GlobPath { mtimeMs?: number; } +interface GlobDirectoryResult { + entries: GlobPath[]; + hitLimit: boolean; +} + /** * Sorts file entries based on recency and then alphabetically. * Recent files (modified within recencyThresholdMs) are listed first, newest to oldest. @@ -136,7 +142,8 @@ class GlobToolInvocation extends BaseToolInvocation< searchDir: string, pattern: string, signal: AbortSignal, - ): Promise { + entryLimit: number, + ): Promise { let effectivePattern = pattern; const fullPath = path.join(searchDir, effectivePattern); if (fs.existsSync(fullPath)) { @@ -183,7 +190,26 @@ class GlobToolInvocation extends BaseToolInvocation< } }; - const entries = (await glob(effectivePattern, { + const normalizePathForComparison = (p: string) => + process.platform === 'win32' || process.platform === 'darwin' + ? p.toLowerCase() + : p; + + const isAllowedByFileFilters = (entry: GlobPath): boolean => { + const relativePath = path.relative(projectRoot, entry.fullpath()); + const { filteredPaths } = this.fileService.filterFilesWithReport( + [relativePath], + fileFilteringOptions, + ); + const entryFullPath = normalizePathForComparison(entry.fullpath()); + return filteredPaths.some( + (p) => + normalizePathForComparison(path.resolve(projectRoot, p)) === + entryFullPath, + ); + }; + + const stream = globStream(effectivePattern, { cwd: searchDir, withFileTypes: true, nodir: true, @@ -196,35 +222,22 @@ class GlobToolInvocation extends BaseToolInvocation< ignored: isTraversalIgnored, childrenIgnored: isTraversalIgnored, }, - })) as GlobPath[]; - - // Filter using paths relative to the project root (the base that - // FileDiscoveryService uses for .gitignore / .qwenignore evaluation). - // Using searchDir-relative paths would cause ignore rules to be - // evaluated against incorrect paths when searchDir != projectRoot. - const relativePaths = entries.map((p) => - path.relative(projectRoot, p.fullpath()), - ); - - const { filteredPaths } = this.fileService.filterFilesWithReport( - relativePaths, - fileFilteringOptions, - ); + }) as AsyncIterable; - const normalizePathForComparison = (p: string) => - process.platform === 'win32' || process.platform === 'darwin' - ? p.toLowerCase() - : p; - - const filteredAbsolutePaths = new Set( - filteredPaths.map((p) => - normalizePathForComparison(path.resolve(projectRoot, p)), - ), - ); + const entries: GlobPath[] = []; + let hitLimit = false; + for await (const entry of stream) { + if (!isAllowedByFileFilters(entry)) { + continue; + } + if (entries.length >= entryLimit) { + hitLimit = true; + break; + } + entries.push(entry); + } - return entries.filter((entry) => - filteredAbsolutePaths.has(normalizePathForComparison(entry.fullpath())), - ); + return { entries, hitLimit }; } async execute(signal: AbortSignal): Promise { @@ -258,9 +271,22 @@ class GlobToolInvocation extends BaseToolInvocation< const pattern = this.params.pattern; const allFilteredEntries: GlobPath[] = []; const seenPaths = new Set(); + let hitCollectionLimit = false; for (const searchDir of searchDirs) { - const entries = await this.globInDirectory(searchDir, pattern, signal); + const remainingEntries = + MAX_GLOB_COLLECTED_ENTRIES - allFilteredEntries.length; + if (remainingEntries <= 0) { + hitCollectionLimit = true; + break; + } + const { entries, hitLimit } = await this.globInDirectory( + searchDir, + pattern, + signal, + remainingEntries, + ); + hitCollectionLimit ||= hitLimit; for (const entry of entries) { // Deduplicate entries that might appear in overlapping directories const normalized = entry.fullpath(); @@ -296,7 +322,7 @@ class GlobToolInvocation extends BaseToolInvocation< MAX_FILE_COUNT, this.config.getTruncateToolOutputLines(), ); - const truncated = totalFileCount > fileLimit; + const truncated = hitCollectionLimit || totalFileCount > fileLimit; // Limit to fileLimit if needed const entriesToShow = truncated @@ -308,11 +334,15 @@ class GlobToolInvocation extends BaseToolInvocation< ); const fileListDescription = sortedAbsolutePaths.join('\n'); - let resultMessage = `Found ${totalFileCount} file(s) matching "${this.params.pattern}" ${searchLocationDescription}`; + let resultMessage = hitCollectionLimit + ? `Found at least ${totalFileCount} file(s) matching "${this.params.pattern}" ${searchLocationDescription}` + : `Found ${totalFileCount} file(s) matching "${this.params.pattern}" ${searchLocationDescription}`; resultMessage += `, sorted by modification time (newest first):\n---\n${fileListDescription}`; // Add truncation notice if needed - if (truncated) { + if (hitCollectionLimit) { + resultMessage += `\n---\n[Results truncated after scanning ${totalFileCount} matching files. Narrow the pattern or path.]`; + } else if (truncated) { const omittedFiles = totalFileCount - fileLimit; const fileTerm = omittedFiles === 1 ? 'file' : 'files'; resultMessage += `\n---\n[${omittedFiles} ${fileTerm} truncated] ...`; @@ -320,7 +350,7 @@ class GlobToolInvocation extends BaseToolInvocation< return { llmContent: resultMessage, - returnDisplay: `Found ${totalFileCount} matching file(s)${truncated ? ' (truncated)' : ''}`, + returnDisplay: `${hitCollectionLimit ? 'Found at least' : 'Found'} ${totalFileCount} matching file(s)${truncated ? ' (truncated)' : ''}`, resultFilePaths: sortedAbsolutePaths, }; } catch (error) { From acfab75de0c25e1d28e9a38138bcef41a0626a74 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Thu, 9 Jul 2026 23:07:09 +0800 Subject: [PATCH 2/4] fix(core): lower glob collection cap --- packages/core/src/tools/glob.ts | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/packages/core/src/tools/glob.ts b/packages/core/src/tools/glob.ts index 87ad2b2942a..6da5e3c7e5d 100644 --- a/packages/core/src/tools/glob.ts +++ b/packages/core/src/tools/glob.ts @@ -32,7 +32,7 @@ import { isPathWithinRoot } from '../utils/workspaceContext.js'; const debugLogger = createDebugLogger('GLOB'); const MAX_FILE_COUNT = 100; -const MAX_GLOB_COLLECTED_ENTRIES = 10_000; +const MAX_GLOB_COLLECTED_ENTRIES = MAX_FILE_COUNT * 10; // Subset of 'Path' interface provided by 'glob' that we can implement for testing export interface GlobPath { @@ -40,11 +40,6 @@ export interface GlobPath { mtimeMs?: number; } -interface GlobDirectoryResult { - entries: GlobPath[]; - hitLimit: boolean; -} - /** * Sorts file entries based on recency and then alphabetically. * Recent files (modified within recencyThresholdMs) are listed first, newest to oldest. @@ -143,7 +138,7 @@ class GlobToolInvocation extends BaseToolInvocation< pattern: string, signal: AbortSignal, entryLimit: number, - ): Promise { + ): Promise<{ entries: GlobPath[]; hitLimit: boolean }> { let effectivePattern = pattern; const fullPath = path.join(searchDir, effectivePattern); if (fs.existsSync(fullPath)) { @@ -190,22 +185,11 @@ class GlobToolInvocation extends BaseToolInvocation< } }; - const normalizePathForComparison = (p: string) => - process.platform === 'win32' || process.platform === 'darwin' - ? p.toLowerCase() - : p; - const isAllowedByFileFilters = (entry: GlobPath): boolean => { const relativePath = path.relative(projectRoot, entry.fullpath()); - const { filteredPaths } = this.fileService.filterFilesWithReport( - [relativePath], - fileFilteringOptions, - ); - const entryFullPath = normalizePathForComparison(entry.fullpath()); - return filteredPaths.some( - (p) => - normalizePathForComparison(path.resolve(projectRoot, p)) === - entryFullPath, + return ( + this.fileService.filterFiles([relativePath], fileFilteringOptions) + .length > 0 ); }; From 8efc2dfae826c836b5638a6af64e3ef550c3b7f4 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Sat, 11 Jul 2026 14:05:30 +0800 Subject: [PATCH 3/4] fix(core): add Claude Opus 4.6-4.8 token limits --- .../provider/default.test.ts | 14 ++++++++++++++ packages/core/src/core/tokenLimits.test.ts | 13 ++++++++++--- packages/core/src/core/tokenLimits.ts | 3 ++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/core/src/core/openaiContentGenerator/provider/default.test.ts b/packages/core/src/core/openaiContentGenerator/provider/default.test.ts index 010e9fed8ff..ea1a341ae3e 100644 --- a/packages/core/src/core/openaiContentGenerator/provider/default.test.ts +++ b/packages/core/src/core/openaiContentGenerator/provider/default.test.ts @@ -236,6 +236,20 @@ describe('DefaultOpenAICompatibleProvider', () => { expect(result.max_tokens).toBe(16384); }); + it('should set the 128K output default for Claude Opus 4.8', () => { + const requestWithoutMaxTokens: OpenAI.Chat.ChatCompletionCreateParams = { + model: 'vertex/claude-opus-4-8', + messages: [{ role: 'user', content: 'Hello' }], + }; + + const result = provider.buildRequest( + requestWithoutMaxTokens, + 'prompt-id', + ); + + expect(result.max_tokens).toBe(131072); + }); + it('should ignore malformed QWEN_CODE_MAX_OUTPUT_TOKENS values', () => { const request: OpenAI.Chat.ChatCompletionCreateParams = { model: 'gpt-4', diff --git a/packages/core/src/core/tokenLimits.test.ts b/packages/core/src/core/tokenLimits.test.ts index 36ab4775ab2..549711174ab 100644 --- a/packages/core/src/core/tokenLimits.test.ts +++ b/packages/core/src/core/tokenLimits.test.ts @@ -137,8 +137,13 @@ describe('tokenLimit', () => { }); describe('Anthropic Claude', () => { - it('should return 200K for all Claude models', () => { - expect(tokenLimit('claude-opus-4-6')).toBe(200000); + it('should return 1M for Opus 4.6 through 4.8', () => { + expect(tokenLimit('claude-opus-4-6')).toBe(1_000_000); + expect(tokenLimit('claude-opus-4-7')).toBe(1_000_000); + expect(tokenLimit('vertex/claude-opus-4-8')).toBe(1_000_000); + }); + + it('should return 200K for other Claude models', () => { expect(tokenLimit('claude-sonnet-4-6')).toBe(200000); expect(tokenLimit('claude-sonnet-4')).toBe(200000); expect(tokenLimit('claude-opus-4')).toBe(200000); @@ -298,8 +303,10 @@ describe('tokenLimit with output type', () => { expect(tokenLimit('gemini-3-flash-preview', 'output')).toBe(65536); }); - it('should return correct output limits for Claude 4.6', () => { + it('should return correct output limits for Claude Opus 4.6 through 4.8', () => { expect(tokenLimit('claude-opus-4-6', 'output')).toBe(131072); + expect(tokenLimit('claude-opus-4-7', 'output')).toBe(131072); + expect(tokenLimit('vertex/claude-opus-4-8', 'output')).toBe(131072); expect(tokenLimit('claude-sonnet-4-6', 'output')).toBe(65536); }); }); diff --git a/packages/core/src/core/tokenLimits.ts b/packages/core/src/core/tokenLimits.ts index dfaa198fff7..0841b843cbe 100644 --- a/packages/core/src/core/tokenLimits.ts +++ b/packages/core/src/core/tokenLimits.ts @@ -113,6 +113,7 @@ const PATTERNS: Array<[RegExp, TokenCount]> = [ // ------------------- // Anthropic Claude // ------------------- + [/^claude-opus-4-(?:6|7|8)/, LIMITS['1m']], // Opus 4.6-4.8: 1M [/^claude-/, LIMITS['200k']], // All Claude models: 200K // ------------------- @@ -182,7 +183,7 @@ const OUTPUT_PATTERNS: Array<[RegExp, TokenCount]> = [ [/^o\d/, LIMITS['128k']], // o-series: 128K // Anthropic Claude - [/^claude-opus-4-6/, LIMITS['128k']], // Opus 4.6: 128K + [/^claude-opus-4-(?:6|7|8)/, LIMITS['128k']], // Opus 4.6-4.8: 128K [/^claude-sonnet-4-6/, LIMITS['64k']], // Sonnet 4.6: 64K [/^claude-/, LIMITS['64k']], // Claude fallback: 64K From b12999c23adc6dbadeca2d4d8c81db82500d3ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E8=89=AF?= <1204183885@qq.com> Date: Sat, 11 Jul 2026 19:14:26 +0800 Subject: [PATCH 4/4] test(core): update Claude Opus context expectation --- packages/core/src/models/modelConfigResolver.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/models/modelConfigResolver.test.ts b/packages/core/src/models/modelConfigResolver.test.ts index d1f6eb00a46..dd70adda4ad 100644 --- a/packages/core/src/models/modelConfigResolver.test.ts +++ b/packages/core/src/models/modelConfigResolver.test.ts @@ -974,7 +974,7 @@ describe('modelConfigResolver', () => { }); expect(result.config.model).toBe('claude-opus-4-6'); - expect(result.config.contextWindowSize).toBe(200_000); + expect(result.config.contextWindowSize).toBe(1_000_000); expect(result.sources['contextWindowSize'].kind).toBe('computed'); });