mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-08 16:39:42 +02:00
perf: pre-compile regex patterns in task-tracker
- Move launch and completion detection patterns to module-level constants - Use iterator directly instead of Array.from for deduplication check - Reduces regex compilation overhead on every terminal output Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+23
-17
@@ -3,6 +3,18 @@ import { EventEmitter } from 'node:events';
|
|||||||
// Maximum number of completed tasks to keep in memory
|
// Maximum number of completed tasks to keep in memory
|
||||||
const MAX_COMPLETED_TASKS = 100;
|
const MAX_COMPLETED_TASKS = 100;
|
||||||
|
|
||||||
|
// Pre-compiled patterns for performance
|
||||||
|
const LAUNCH_PATTERNS = [
|
||||||
|
/Launching\s+(\w+)\s+agent/i,
|
||||||
|
/Starting\s+(\w+)\s+task/i,
|
||||||
|
/Spawning\s+(\w+)\s+agent/i,
|
||||||
|
];
|
||||||
|
const COMPLETE_PATTERNS = [
|
||||||
|
/Task\s+completed/i,
|
||||||
|
/Agent\s+finished/i,
|
||||||
|
/Background\s+task\s+done/i,
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a background task spawned by Claude Code
|
* Represents a background task spawned by Claude Code
|
||||||
*/
|
*/
|
||||||
@@ -59,25 +71,25 @@ export class TaskTracker extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Process raw terminal output to detect task patterns
|
* Process raw terminal output to detect task patterns
|
||||||
* This is a fallback for when JSON parsing doesn't capture everything
|
* This is a fallback for when JSON parsing doesn't capture everything
|
||||||
|
* Uses pre-compiled patterns for performance
|
||||||
*/
|
*/
|
||||||
processTerminalOutput(data: string): void {
|
processTerminalOutput(data: string): void {
|
||||||
// Detect task launch patterns in terminal output
|
// Detect task launch patterns in terminal output
|
||||||
// Claude Code shows things like "Launching explore agent..." or similar
|
// Claude Code shows things like "Launching explore agent..." or similar
|
||||||
const launchPatterns = [
|
for (const pattern of LAUNCH_PATTERNS) {
|
||||||
/Launching\s+(\w+)\s+agent/i,
|
|
||||||
/Starting\s+(\w+)\s+task/i,
|
|
||||||
/Spawning\s+(\w+)\s+agent/i,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const pattern of launchPatterns) {
|
|
||||||
const match = data.match(pattern);
|
const match = data.match(pattern);
|
||||||
if (match) {
|
if (match) {
|
||||||
// This is a heuristic detection - might create duplicate tasks
|
// This is a heuristic detection - might create duplicate tasks
|
||||||
// but we dedupe by checking if we already have a running task of this type
|
// but we dedupe by checking if we already have a running task of this type
|
||||||
const agentType = match[1].toLowerCase();
|
const agentType = match[1].toLowerCase();
|
||||||
const existingRunning = Array.from(this.tasks.values()).find(
|
// Optimize: use iterator directly instead of Array.from
|
||||||
t => t.subagentType === agentType && t.status === 'running'
|
let existingRunning = false;
|
||||||
);
|
for (const task of this.tasks.values()) {
|
||||||
|
if (task.subagentType === agentType && task.status === 'running') {
|
||||||
|
existingRunning = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!existingRunning) {
|
if (!existingRunning) {
|
||||||
this.createTaskFromTerminal(agentType, data);
|
this.createTaskFromTerminal(agentType, data);
|
||||||
}
|
}
|
||||||
@@ -85,13 +97,7 @@ export class TaskTracker extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detect task completion patterns
|
// Detect task completion patterns
|
||||||
const completePatterns = [
|
for (const pattern of COMPLETE_PATTERNS) {
|
||||||
/Task\s+completed/i,
|
|
||||||
/Agent\s+finished/i,
|
|
||||||
/Background\s+task\s+done/i,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const pattern of completePatterns) {
|
|
||||||
if (pattern.test(data)) {
|
if (pattern.test(data)) {
|
||||||
// Complete the most recent running task
|
// Complete the most recent running task
|
||||||
const runningTask = this.getMostRecentRunningTask();
|
const runningTask = this.getMostRecentRunningTask();
|
||||||
|
|||||||
Reference in New Issue
Block a user