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
43 changes: 22 additions & 21 deletions src/main/services/discovery/SubagentLocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { createLogger } from '@shared/utils/logger';
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';

import type { FileSystemProvider } from '@main/services/infrastructure/FileSystemProvider';

Expand Down Expand Up @@ -51,16 +52,13 @@
);

// 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 All @@ -76,10 +74,7 @@
return false;
}

/**
* Checks if a session has subagent files (session-specific only).
* Only checks the NEW structure: {projectId}/{sessionId}/subagents/
* Verifies that at least one subagent file has non-empty content.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

';' expected.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected token. A constructor, method, accessor, or property was expected.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unknown keyword or identifier. Did you mean 'let'?

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected keyword or identifier.

Check failure on line 77 in src/main/services/discovery/SubagentLocator.ts

View workflow job for this annotation

GitHub Actions / validate

'(' expected.
*
* @param projectId - The project ID
* @param sessionId - The session ID
Expand All @@ -96,16 +91,13 @@
);

// Check if at least one subagent file has content (not empty)
// Uses statSync() to check file size instead of reading file contents
for (const fileName of subagentFiles) {
const filePath = path.join(newSubagentsPath, fileName);
try {
const stats = fs.statSync(filePath);
// File must have size > 0 and contain at least one line
if (stats.size > 0) {
const content = fs.readFileSync(filePath, 'utf8');
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 @@ -210,17 +202,26 @@
*/
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,
});

try {
for await (const line of rl) {
if (!line.trim()) continue;

if (!firstLine.trim()) {
return false;
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