mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-08 16:39:42 +02:00
refactor: pre-compile regex patterns and remove dead code
- task.ts: Cache completion phrase regex in constructor instead of re-creating on every checkCompletion() call. Add escapeRegex helper for safe RegExp construction from user-provided phrases. - ralph-tracker.ts: Remove commented-out COMPLETION_SIGNAL_PATTERN Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -209,9 +209,6 @@ const ALL_COUNT_PATTERN = /all\s+(\d+)\s+(?:tasks?|files?|items?)/i;
|
|||||||
*/
|
*/
|
||||||
const TASK_DONE_PATTERN = /(?:task|item|todo)\s*(?:#?\d+|"\s*[^"]+\s*")?\s*(?:is\s+)?(?:done|completed?|finished)|(?:completed?|done|finished)\s+(?:task|item)\s*(?:#?\d+)?|marking\s+(?:.*?\s+)?(?:as\s+)?completed?|marked\s+(?:.*?\s+)?(?:as\s+)?completed?/i;
|
const TASK_DONE_PATTERN = /(?:task|item|todo)\s*(?:#?\d+|"\s*[^"]+\s*")?\s*(?:is\s+)?(?:done|completed?|finished)|(?:completed?|done|finished)\s+(?:task|item)\s*(?:#?\d+)?|marking\s+(?:.*?\s+)?(?:as\s+)?completed?|marked\s+(?:.*?\s+)?(?:as\s+)?completed?/i;
|
||||||
|
|
||||||
// Generic completion signal pattern - commented out due to false positive risk
|
|
||||||
// const COMPLETION_SIGNAL_PATTERN = /^(?:done|completed?|finished|all\s+set)!?\s*$/i;
|
|
||||||
|
|
||||||
// ---------- Utility Patterns ----------
|
// ---------- Utility Patterns ----------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+16
-5
@@ -10,6 +10,14 @@
|
|||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { TaskDefinition, TaskState, TaskStatus } from './types.js';
|
import { TaskDefinition, TaskState, TaskStatus } from './types.js';
|
||||||
|
|
||||||
|
/** Pre-compiled pattern for generic promise tag detection */
|
||||||
|
const PROMISE_TAG_PATTERN = /<promise>[^<]+<\/promise>/;
|
||||||
|
|
||||||
|
/** Escapes special regex characters in a string for safe use in RegExp constructor */
|
||||||
|
function escapeRegex(str: string): string {
|
||||||
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options for creating a new task.
|
* Options for creating a new task.
|
||||||
*/
|
*/
|
||||||
@@ -45,6 +53,8 @@ export class Task {
|
|||||||
private _completedAt: number | null = null;
|
private _completedAt: number | null = null;
|
||||||
private _output: string = '';
|
private _output: string = '';
|
||||||
private _error: string | null = null;
|
private _error: string | null = null;
|
||||||
|
/** Pre-compiled regex for completion phrase detection (avoids re-creation per check) */
|
||||||
|
private readonly _completionPattern: RegExp | null;
|
||||||
|
|
||||||
constructor(options: CreateTaskOptions, id?: string) {
|
constructor(options: CreateTaskOptions, id?: string) {
|
||||||
this.id = id || uuidv4();
|
this.id = id || uuidv4();
|
||||||
@@ -55,6 +65,9 @@ export class Task {
|
|||||||
this.completionPhrase = options.completionPhrase;
|
this.completionPhrase = options.completionPhrase;
|
||||||
this.timeoutMs = options.timeoutMs;
|
this.timeoutMs = options.timeoutMs;
|
||||||
this.createdAt = Date.now();
|
this.createdAt = Date.now();
|
||||||
|
this._completionPattern = options.completionPhrase
|
||||||
|
? new RegExp(`<promise>${escapeRegex(options.completionPhrase)}</promise>`)
|
||||||
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
get status(): TaskStatus {
|
get status(): TaskStatus {
|
||||||
@@ -190,13 +203,11 @@ export class Task {
|
|||||||
|
|
||||||
/** Checks if output contains the completion phrase. */
|
/** Checks if output contains the completion phrase. */
|
||||||
checkCompletion(output: string): boolean {
|
checkCompletion(output: string): boolean {
|
||||||
if (this.completionPhrase) {
|
if (this._completionPattern) {
|
||||||
// Check for exact completion phrase in promise tags
|
return this._completionPattern.test(output);
|
||||||
const pattern = new RegExp(`<promise>${this.completionPhrase}</promise>`);
|
|
||||||
return pattern.test(output);
|
|
||||||
}
|
}
|
||||||
// Default: check for any promise tag completion
|
// Default: check for any promise tag completion
|
||||||
return /<promise>[^<]+<\/promise>/.test(output);
|
return PROMISE_TAG_PATTERN.test(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if the task has exceeded its timeout. */
|
/** Returns true if the task has exceeded its timeout. */
|
||||||
|
|||||||
Reference in New Issue
Block a user