/** * Self-Verification Middleware * * Implements the "Build & Self-Verify" pattern from harness engineering: * - Intercepts agent before completion to remind verification * - Tracks file edits to detect potential issues * - Injects verification reminders into context * * Inspired by: https://blog.langchain.com/improving-deep-agents-with-harness-engineering/ */ export interface FileEditRecord { path: string; editCount: number; lastEditTime: number; operations: string[]; } export interface SelfVerifyConfig { /** Max edits to same file before warning (default: 5) */ maxEditsPerFile: number; /** Enable pre-completion verification check (default: true) */ enablePreCompletionCheck: boolean; /** Min turns before triggering verification reminder (default: 3) */ minTurnsForVerification: number; /** Reset counters on successful verification (default: true) */ resetOnVerification: boolean; } export interface VerificationTask { isError?: boolean; result?: unknown; } export interface VerificationRecord { toolName: string; command?: string; success: boolean; exitCode?: number | null; status?: string; timedOut?: boolean; } export interface SelfVerifyState { hasUnverifiedEdits: boolean; changedFiles: string[]; diffReviewed: boolean; verificationAttempted: boolean; lastVerificationFailed: boolean; lastVerification?: VerificationRecord; lastMutationTool?: string; } export type SelfVerifyAgentKind = 'coder' | 'data' | 'writer' | 'researcher' | 'creative' | 'generic'; /** * Tracks file modifications and provides verification guidance */ export declare class SelfVerifyMiddleware { private readonly states; private config; constructor(config?: Partial); private getState; /** * Record a file edit operation */ recordEdit(filePath: string, operation: 'write' | 'edit' | 'command', sessionKey?: string): void; recordVerification(toolName: string, args?: unknown, task?: VerificationTask, sessionKey?: string): void; getVerificationState(sessionKey?: string): SelfVerifyState; getPendingVerificationContext(sessionKey?: string, agentId?: string): string; private extractExecCommand; private extractResultDetails; private isSuccessfulExecVerification; private isVerificationCommand; private isDiffReviewCommand; /** * Get edit count for a specific file */ getEditCount(filePath: string, sessionKey?: string): number; /** * Check if any file has excessive edits (potential doom loop) */ hasExcessiveEdits(sessionKey?: string): { file: string; count: number; } | null; /** * Increment turn counter */ onTurnStart(sessionKey?: string): void; /** * Reset all tracking (e.g., after successful task completion) */ reset(sessionKey?: string): void; consumePostEditReminder(sessionKey?: string): string; /** * Get context injection for system prompt * Adds verification guidance based on current state */ getContextInjection(sessionKey?: string, agentId?: string): string; /** * Check if we should inject verification reminder */ private shouldPromptForVerification; /** * Mark verification as requested (to avoid spam) */ markVerificationRequested(sessionKey?: string): void; /** * Build workflow guidance section */ private buildWorkflowGuidance; private classifyAgent; private buildPendingVerificationReminder; private verificationProfile; /** * Build warning for excessive file edits (doom loop detection) */ private buildExcessiveEditWarning; /** * Build pre-completion verification reminder */ private buildPreCompletionReminder; /** * Get summary of file edits for debugging */ getEditSummary(sessionKey?: string): { totalFiles: number; totalEdits: number; topFiles: Array<{ path: string; count: number; }>; }; /** * Update configuration */ setConfig(config: Partial): void; /** * Get current configuration */ getConfig(): SelfVerifyConfig; }