Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/main/services/discovery/SubagentLocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -164,17 +162,26 @@ export class SubagentLocator {
*/
async subagentBelongsToSession(filePath: string, sessionId: string): Promise<boolean> {
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);
Expand Down
Loading