( agentId: string, currentData: PreProcessorResult )
| 82 | * Returns false if no significant change (skip iteration) |
| 83 | */ |
| 84 | export async function detectSignificantChange( |
| 85 | agentId: string, |
| 86 | currentData: PreProcessorResult |
| 87 | ): Promise<boolean> { |
| 88 | const previous = previousIterationData.get(agentId); |
| 89 | |
| 90 | // First run always counts as significant |
| 91 | if (!previous) { |
| 92 | Logger.debug('CHANGE_DETECTOR', 'First iteration - storing baseline data'); |
| 93 | previousIterationData.set(agentId, currentData); |
| 94 | // Emit event indicating first iteration (no change detection performed) |
| 95 | Logger.info('CHANGE_DETECTOR', 'First iteration - no change detection performed', { |
| 96 | logType: 'change-detection-result', |
| 97 | content: { agentId, isFirstIteration: true } |
| 98 | }); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Compare text using Levenshtein |
| 103 | const isSameText = compareText(previous.modifiedPrompt, currentData.modifiedPrompt); |
| 104 | |
| 105 | // Compare images using the configured strategy |
| 106 | Logger.debug('CHANGE_DETECTOR', `Starting image comparison with mode: "${currentDetectionMode}"`); |
| 107 | let imageComparisonResult; |
| 108 | let isSameImages = false; |
| 109 | try { |
| 110 | imageComparisonResult = await compareImages( |
| 111 | previous.images || [], |
| 112 | currentData.images || [] |
| 113 | ); |
| 114 | isSameImages = imageComparisonResult.isSame; |
| 115 | } catch(error) { |
| 116 | Logger.error('CHANGE_DETECTOR', `Image comparison failed: ${error}. Assuming significant change to continue agent execution`) |
| 117 | isSameImages = false; |
| 118 | imageComparisonResult = { isSame: false, metadata: []}; |
| 119 | } |
| 120 | |
| 121 | Logger.debug('CHANGE_DETECTOR', `Change detection: text=${isSameText ? 'same' : 'changed'}, images=${isSameImages ? 'same' : 'changed'}`); |
| 122 | |
| 123 | // Only skip if BOTH text AND images are the same |
| 124 | const isSignificant = !isSameText || !isSameImages; |
| 125 | |
| 126 | // Store current data for next comparison if significant |
| 127 | if (isSignificant) { |
| 128 | previousIterationData.set(agentId, currentData); |
| 129 | } |
| 130 | |
| 131 | // Emit detailed change detection result for UI display |
| 132 | Logger.info('CHANGE_DETECTOR', 'Change detection completed', { |
| 133 | logType: 'change-detection-result', |
| 134 | content: { |
| 135 | agentId, |
| 136 | textChanged: !isSameText, |
| 137 | imagesChanged: !isSameImages, |
| 138 | isSignificant, |
| 139 | detectionMode: currentDetectionMode, |
| 140 | // Include thresholds for UI display |
| 141 | thresholds: { |
no test coverage detected