refactor: consolidate shared regex patterns and token validation

- Create src/utils/regex-patterns.ts with ANSI_ESCAPE_PATTERN_FULL,
  ANSI_ESCAPE_PATTERN_SIMPLE, TOKEN_PATTERN, and helper functions
- Create src/utils/token-validation.ts with MAX_SESSION_TOKENS and
  validation utilities
- Update session.ts, respawn-controller.ts, ai-checker-base.ts,
  ralph-tracker.ts, state-store.ts to use shared utilities
- Export all new utilities from src/utils/index.ts

Reduces code duplication while maintaining pre-compiled patterns
for performance.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-29 10:55:49 +01:00
co-authored by Claude Opus 4.5
parent 451f0854eb
commit 6c07541288
10 changed files with 187 additions and 45 deletions
+2 -2
View File
@@ -18,6 +18,7 @@ import { readFileSync, writeFileSync, existsSync, mkdirSync, renameSync, unlinkS
import { homedir } from 'node:os';
import { dirname, join } from 'node:path';
import { AppState, createInitialState, RalphSessionState, createInitialRalphSessionState, GlobalStats, createInitialGlobalStats, TokenStats, TokenUsageEntry } from './types.js';
import { MAX_SESSION_TOKENS } from './utils/index.js';
/** Debounce delay for batching state writes (ms) */
const SAVE_DEBOUNCE_MS = 500;
@@ -369,8 +370,7 @@ export class StateStore {
* Call when a session is deleted to preserve its usage in lifetime stats.
*/
addToGlobalStats(inputTokens: number, outputTokens: number, cost: number): void {
// Sanity check: reject absurdly large values (max 500k tokens per session)
const MAX_SESSION_TOKENS = 500_000;
// Sanity check: reject absurdly large values
if (inputTokens > MAX_SESSION_TOKENS || outputTokens > MAX_SESSION_TOKENS) {
console.warn(`[StateStore] Rejected absurd global stats: input=${inputTokens}, output=${outputTokens}`);
return;