|
| 1 | +package io.sentrius.agent.analysis.agents.memory; |
| 2 | + |
| 3 | +import io.sentrius.sso.core.dto.agents.AgentMemoryDTO; |
| 4 | +import io.sentrius.sso.core.dto.agents.MemoryQueryDTO; |
| 5 | +import io.sentrius.sso.core.services.agents.AgentClientService; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.scheduling.annotation.Scheduled; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * Service to evaluate agent memories and determine which ones should be marked as PUBLIC. |
| 16 | + * This service analyzes PRIVATE memories and recommends which ones can be safely shared |
| 17 | + * with all agents (PUBLIC classification). |
| 18 | + */ |
| 19 | +@Slf4j |
| 20 | +@Service |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class MemoryEvaluationService { |
| 23 | + |
| 24 | + private final AgentClientService agentClientService; |
| 25 | + |
| 26 | + /** |
| 27 | + * Scheduled task to evaluate private memories and identify candidates for PUBLIC classification. |
| 28 | + * Runs every hour to analyze newly created memories. |
| 29 | + */ |
| 30 | + @Scheduled(fixedDelay = 3600000, initialDelay = 300000) // Run every hour, initial delay 5 minutes |
| 31 | + public void evaluateMemoriesForPublicClassification() { |
| 32 | + log.info("Starting memory evaluation for public classification candidates"); |
| 33 | + |
| 34 | + try { |
| 35 | + // Query for PRIVATE memories to evaluate |
| 36 | + MemoryQueryDTO query = MemoryQueryDTO.builder() |
| 37 | + .classification("PRIVATE") |
| 38 | + .size(100) |
| 39 | + .build(); |
| 40 | + |
| 41 | + // This would require an AgentExecution token, which we'll need to handle appropriately |
| 42 | + // For now, this is a placeholder for the logic that would be implemented |
| 43 | + log.info("Memory evaluation task executed - implementation requires proper authentication context"); |
| 44 | + |
| 45 | + } catch (Exception e) { |
| 46 | + log.error("Error during memory evaluation", e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Evaluates a list of memories and determines which ones can be marked as PUBLIC. |
| 52 | + * |
| 53 | + * Criteria for PUBLIC classification: |
| 54 | + * - No sensitive data (credentials, personal info, etc.) |
| 55 | + * - General operational knowledge that benefits all agents |
| 56 | + * - Successfully executed operations that are safe to share |
| 57 | + * - System configuration information that is non-sensitive |
| 58 | + * |
| 59 | + * @param memories List of memories to evaluate |
| 60 | + * @return List of memory keys that should be marked PUBLIC |
| 61 | + */ |
| 62 | + public List<String> identifyPublicCandidates(List<AgentMemoryDTO> memories) { |
| 63 | + List<String> publicCandidates = new ArrayList<>(); |
| 64 | + |
| 65 | + for (AgentMemoryDTO memory : memories) { |
| 66 | + if (shouldBePublic(memory)) { |
| 67 | + publicCandidates.add(memory.getMemoryKey()); |
| 68 | + log.info("Memory {} identified as PUBLIC candidate", memory.getMemoryKey()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return publicCandidates; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Determines if a memory should be marked as PUBLIC based on its content and metadata. |
| 77 | + * |
| 78 | + * @param memory The memory to evaluate |
| 79 | + * @return true if the memory should be PUBLIC, false otherwise |
| 80 | + */ |
| 81 | + private boolean shouldBePublic(AgentMemoryDTO memory) { |
| 82 | + // Safety check: memories with CONFIDENTIAL access should never be PUBLIC |
| 83 | + if ("CONFIDENTIAL".equalsIgnoreCase(memory.getAccessLevel())) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // Check for sensitive markings |
| 88 | + if (memory.getMarkings() != null) { |
| 89 | + for (String marking : memory.getMarkings()) { |
| 90 | + if (isSensitiveMarking(marking)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + String memoryValue = memory.getMemoryValue(); |
| 97 | + if (memoryValue == null) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + // Check for potentially sensitive content patterns |
| 102 | + if (containsSensitivePatterns(memoryValue)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + // If memory is related to general system operations or capabilities, it may be PUBLIC |
| 107 | + String memoryKey = memory.getMemoryKey(); |
| 108 | + if (isGeneralOperationalMemory(memoryKey)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + // Default to keeping it PRIVATE for safety |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Checks if a marking indicates sensitive content. |
| 118 | + */ |
| 119 | + private boolean isSensitiveMarking(String marking) { |
| 120 | + String upperMarking = marking.toUpperCase(); |
| 121 | + return upperMarking.contains("SECRET") || |
| 122 | + upperMarking.contains("CONFIDENTIAL") || |
| 123 | + upperMarking.contains("RESTRICTED") || |
| 124 | + upperMarking.contains("PRIVATE") || |
| 125 | + upperMarking.contains("PII") || |
| 126 | + upperMarking.contains("PHI"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Checks if the memory value contains sensitive patterns. |
| 131 | + */ |
| 132 | + private boolean containsSensitivePatterns(String value) { |
| 133 | + String lowerValue = value.toLowerCase(); |
| 134 | + |
| 135 | + // Check for credential patterns |
| 136 | + if (lowerValue.contains("password") || |
| 137 | + lowerValue.contains("secret") || |
| 138 | + lowerValue.contains("api_key") || |
| 139 | + lowerValue.contains("token") || |
| 140 | + lowerValue.contains("credential")) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + // Check for personal information patterns |
| 145 | + if (lowerValue.contains("ssn") || |
| 146 | + lowerValue.contains("social security") || |
| 147 | + lowerValue.contains("credit card") || |
| 148 | + lowerValue.contains("email") && lowerValue.contains("@")) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Checks if the memory key indicates general operational knowledge. |
| 157 | + */ |
| 158 | + private boolean isGeneralOperationalMemory(String key) { |
| 159 | + String lowerKey = key.toLowerCase(); |
| 160 | + |
| 161 | + // These types of memories can potentially be public |
| 162 | + return lowerKey.contains("endpoint") || |
| 163 | + lowerKey.contains("capability") || |
| 164 | + lowerKey.contains("verb") || |
| 165 | + lowerKey.contains("operation") || |
| 166 | + lowerKey.contains("status") || |
| 167 | + lowerKey.contains("config") && !lowerKey.contains("secret"); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Updates the classification of a memory from PRIVATE to PUBLIC. |
| 172 | + * This should be called after verification that the memory is safe to share. |
| 173 | + * |
| 174 | + * @param memoryKey The key of the memory to update |
| 175 | + * @param agentName The agent that owns the memory |
| 176 | + * @return true if update was successful, false otherwise |
| 177 | + */ |
| 178 | + public boolean updateMemoryToPublic(String memoryKey, String agentName) { |
| 179 | + try { |
| 180 | + log.info("Updating memory {} for agent {} to PUBLIC classification", memoryKey, agentName); |
| 181 | + // Implementation would require proper API call to update memory classification |
| 182 | + // This is a placeholder for the actual implementation |
| 183 | + return true; |
| 184 | + } catch (Exception e) { |
| 185 | + log.error("Failed to update memory {} to PUBLIC", memoryKey, e); |
| 186 | + return false; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments