Skip to content

Commit de2dd41

Browse files
committed
removed redundant null safety fallbacks, added cache corruption prevention, update search filter logic to regex word boundaries
1 parent a5c0ceb commit de2dd41

File tree

1 file changed

+19
-40
lines changed

1 file changed

+19
-40
lines changed

src/workspacesProvider.ts

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,20 @@ export class WorkspaceProvider
375375
agentMetadataText: string;
376376
} {
377377
// Handle null/undefined workspace data safely
378-
const workspaceName = (workspace.workspace.name || "").toLowerCase();
379-
const ownerName = (workspace.workspace.owner_name || "").toLowerCase();
378+
const workspaceName = workspace.workspace.name.toLowerCase();
379+
const ownerName = workspace.workspace.owner_name.toLowerCase();
380380
const templateName = (
381381
workspace.workspace.template_display_name ||
382-
workspace.workspace.template_name ||
383-
""
382+
workspace.workspace.template_name
384383
).toLowerCase();
385384
const status = (
386-
workspace.workspace.latest_build?.status || ""
385+
workspace.workspace.latest_build.status || ""
387386
).toLowerCase();
388387

389388
// Extract agent names with null safety
390-
const agents = extractAgents(
391-
workspace.workspace.latest_build?.resources || [],
392-
);
389+
const agents = extractAgents(workspace.workspace.latest_build.resources);
393390
const agentNames = agents
394-
.map((agent) => (agent.name || "").toLowerCase())
391+
.map((agent) => agent.name.toLowerCase())
395392
.filter((name) => name.length > 0);
396393

397394
// Extract and cache agent metadata with error handling
@@ -402,13 +399,16 @@ export class WorkspaceProvider
402399
agentMetadataText = this.metadataCache[metadataCacheKey];
403400
} else {
404401
const metadataStrings: string[] = [];
402+
let hasSerializationErrors = false;
403+
405404
agents.forEach((agent) => {
406405
const watcher = this.agentWatchers[agent.id];
407406
if (watcher?.metadata) {
408407
watcher.metadata.forEach((metadata) => {
409408
try {
410409
metadataStrings.push(JSON.stringify(metadata).toLowerCase());
411410
} catch (error) {
411+
hasSerializationErrors = true;
412412
// Handle JSON serialization errors gracefully
413413
this.storage.output.warn(
414414
`Failed to serialize metadata for agent ${agent.id}: ${error}`,
@@ -417,8 +417,13 @@ export class WorkspaceProvider
417417
});
418418
}
419419
});
420+
420421
agentMetadataText = metadataStrings.join(" ");
421-
this.metadataCache[metadataCacheKey] = agentMetadataText;
422+
423+
// Only cache if all metadata serialized successfully
424+
if (!hasSerializationErrors) {
425+
this.metadataCache[metadataCacheKey] = agentMetadataText;
426+
}
422427
}
423428

424429
return {
@@ -454,18 +459,8 @@ export class WorkspaceProvider
454459

455460
const regexPatterns: RegExp[] = [];
456461
for (const word of searchWords) {
457-
try {
458-
// Escape special regex characters to prevent injection
459-
const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
460-
regexPatterns.push(new RegExp(`\\b${escapedWord}\\b`, "i"));
461-
} catch (error) {
462-
// Handle invalid regex patterns
463-
this.storage.output.warn(
464-
`Invalid regex pattern for search word "${word}": ${error}`,
465-
);
466-
// Fall back to simple substring matching for this word
467-
continue;
468-
}
462+
// Simple word boundary search
463+
regexPatterns.push(new RegExp(`\\b${word}\\b`, "i"));
469464
}
470465

471466
// Combine all text for exact word matching
@@ -481,27 +476,11 @@ export class WorkspaceProvider
481476
// Check for exact word matches (higher priority)
482477
const hasExactWordMatch =
483478
regexPatterns.length > 0 &&
484-
regexPatterns.some((pattern) => {
485-
try {
486-
return pattern.test(allText);
487-
} catch (error) {
488-
// Handle regex test errors gracefully
489-
this.storage.output.warn(
490-
`Regex test failed for pattern ${pattern}: ${error}`,
491-
);
492-
return false;
493-
}
494-
});
479+
regexPatterns.some((pattern) => pattern.test(allText));
495480

496481
// Check for substring matches (lower priority) - only if no exact word match
497482
const hasSubstringMatch =
498-
!hasExactWordMatch &&
499-
(fields.workspaceName.includes(searchTerm) ||
500-
fields.ownerName.includes(searchTerm) ||
501-
fields.templateName.includes(searchTerm) ||
502-
fields.status.includes(searchTerm) ||
503-
fields.agentNames.some((agentName) => agentName.includes(searchTerm)) ||
504-
fields.agentMetadataText.includes(searchTerm));
483+
!hasExactWordMatch && allText.includes(searchTerm);
505484

506485
// Return true if either exact word match or substring match
507486
return hasExactWordMatch || hasSubstringMatch;

0 commit comments

Comments
 (0)