diff --git a/src/main/services/discovery/SubagentLocator.ts b/src/main/services/discovery/SubagentLocator.ts index be73ca6f..e4e795e3 100644 --- a/src/main/services/discovery/SubagentLocator.ts +++ b/src/main/services/discovery/SubagentLocator.ts @@ -14,6 +14,7 @@ import { LocalFileSystemProvider } from '@main/services/infrastructure/LocalFile import { buildSubagentsPath, extractBaseDir } from '@main/utils/pathDecoder'; import { createLogger } from '@shared/utils/logger'; import * as path from 'path'; +import * as readline from 'readline'; import type { FileSystemProvider } from '@main/services/infrastructure/FileSystemProvider'; @@ -50,16 +51,13 @@ export class SubagentLocator { ); // Check if at least one subagent file has content (not empty) + // Uses stat() to check file size instead of reading file contents for (const entry of subagentFiles) { const filePath = path.join(newSubagentsPath, entry.name); try { const stats = await this.fsProvider.stat(filePath); - // File must have size > 0 and contain at least one line if (stats.size > 0) { - const content = await this.fsProvider.readFile(filePath); - if (content.trim().length > 0) { - return true; - } + return true; } } catch (error) { // Skip this file if we can't read it - log for debugging @@ -164,17 +162,26 @@ export class SubagentLocator { */ async subagentBelongsToSession(filePath: string, sessionId: string): Promise { try { - // Read just the first line to check sessionId - const content = await this.fsProvider.readFile(filePath); - const firstNewline = content.indexOf('\n'); - const firstLine = firstNewline > 0 ? content.slice(0, firstNewline) : content; + // Stream only the first line instead of reading the entire file + const fileStream = this.fsProvider.createReadStream(filePath, { encoding: 'utf8' }); + const rl = readline.createInterface({ + input: fileStream, + crlfDelay: Infinity, + }); - if (!firstLine.trim()) { - return false; + try { + for await (const line of rl) { + if (!line.trim()) continue; + + const entry = JSON.parse(line) as { sessionId?: string }; + return entry.sessionId === sessionId; + } + } finally { + rl.close(); + fileStream.destroy(); } - const entry = JSON.parse(firstLine) as { sessionId?: string }; - return entry.sessionId === sessionId; + return false; } catch (error) { // If we can't read or parse the file, don't include it - log for debugging logger.debug(`SubagentLocator: Could not parse file ${filePath}:`, error);