src/services/error-handling.ts.
PentestError Class
The core error class that extendsError with additional context:
src/services/error-handling.ts:18
Fields
name- Always'PentestError'for type identificationtype- Coarse error category (seePentestErrorType)retryable- Whether the error should trigger a retrycontext- Additional error context (agent name, attempt number, etc.)timestamp- ISO 8601 timestamp when error occurredcode- Optional specificErrorCodefor reliable classification
ErrorCode Enum
Specific error codes for reliable classification:src/types/errors.ts:18
Error Code Categories
Configuration Errors (Non-Retryable)
CONFIG_NOT_FOUND- Configuration file not foundCONFIG_VALIDATION_FAILED- Configuration failed JSON Schema validationCONFIG_PARSE_ERROR- YAML parsing error
Agent Execution Errors
AGENT_EXECUTION_FAILED- Agent execution failed (retryable depends on error)OUTPUT_VALIDATION_FAILED- Agent didn’t produce expected output (retryable)
Billing Errors (Retryable)
API_RATE_LIMITED- API rate limit hit (429)SPENDING_CAP_REACHED- Anthropic spending cap reachedINSUFFICIENT_CREDITS- Anthropic account out of credits
Git Errors (Non-Retryable)
GIT_CHECKPOINT_FAILED- Failed to create git checkpointGIT_ROLLBACK_FAILED- Failed to rollback git workspace
Prompt Errors (Non-Retryable)
PROMPT_LOAD_FAILED- Failed to load prompt template
Validation Errors (Retryable)
DELIVERABLE_NOT_FOUND- Expected deliverable file not found
Preflight Errors
REPO_NOT_FOUND- Target repository not found (non-retryable)AUTH_FAILED- Authentication failed (non-retryable)BILLING_ERROR- Billing error detected (retryable)
PentestErrorType
Coarse error categories for classification:src/types/errors.ts:49
These are used for grouping errors in logs and metrics. The ErrorCode enum provides more specific classification within these categories.
Error Classification
Classification Priority
TheclassifyErrorForTemporal function uses a two-tier classification system:
- Code-Based Classification (Preferred) - If error is
PentestErrorwithErrorCode, use reliable code-based logic - String-Based Classification (Fallback) - For external errors (SDK, network), match error message patterns
src/services/error-handling.ts:179
Retryable vs Non-Retryable
Retryable Errors (Temporal will retry with backoff):- Network errors (connection, timeout, ECONNRESET)
- Rate limiting (429, “too many requests”)
- Server errors (5xx, service unavailable, bad gateway)
- MCP server errors (“mcp server”, “terminated”)
- Max turns reached (agent may succeed with different approach)
- Billing errors (spending cap, insufficient credits)
- Output validation errors (agent may succeed on retry)
- Authentication errors (401, invalid API key)
- Permission errors (403, forbidden)
- Invalid request (400, malformed request)
- Request too large (413)
- Configuration errors (missing files, validation failed)
- Execution limits (max turns, budget exceeded)
- Invalid target URL
String Pattern Matching
For external errors without error codes, the classifier uses pattern matching:src/services/error-handling.ts:60
Retry Logic
Agent-Level Retries
Agents are retried up to 3 times with exponential backoff:- Attempt 1: Immediate execution
- Attempt 2: 1 minute delay
- Attempt 3: 5 minutes delay
Billing Error Backoff
Billing errors use extended backoff times:- Minimum: 5 minutes
- Maximum: 30 minutes
- Human to add credits to Anthropic account
- Anthropic spending cap to reset (auto-resets after some duration)
Conservative Retry Classification
Unknown errors default to non-retryable (fail-safe):src/services/error-handling.ts:100
Rationale: Better to fail fast on unknown errors than waste time and credits retrying permanent failures.
Error Context
Errors carry additional context for debugging:src/types/errors.ts:59
Common context fields:
agentName- Name of agent that failedattemptNumber- Which retry attempt failedpromptName- Prompt template that failed to loadoriginalError- Original error message from external system
Temporal Integration
Activities wrap errors in Temporal’sApplicationFailure:
Spending Cap Detection
Billing errors are detected using both API patterns and text patterns:src/utils/billing-detection.ts
Note: Anthropic returns billing errors as 400 invalid_request_error, not 402 Payment Required.
Error Logging
Errors are logged with structured context:src/types/errors.ts:63
Logs are written to:
audit-logs/{sessionId}/{agentName}/error.log- Per-agent error logsaudit-logs/{sessionId}/workflow.log- Unified workflow log
Result Type
Services use theResult<T,E> type for explicit error handling:
src/types/result.ts:18
This forces callers to handle errors explicitly:
Best Practices
Creating Errors
Always include anErrorCode when throwing PentestError:
Error Propagation
Services returnResult<T,E> instead of throwing:
Activity Error Handling
Activities catch and classify errors:Related Documentation
- Agent Registry - Agent definitions and execution flow
- Metrics Tracking - Error tracking in metrics
- Key Files - Error handling file locations
