File size: 6,518 Bytes
ded0382 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | """
osint_core/drift.py
Purpose:
Convert execution telemetry into a drift vector.
Principles:
- Drift is a vector, not a scalar.
- Detection does not mutate state.
- Correction is separate.
- Policy drift outranks statistical drift.
- Adversarial drift constrains before adapting.
"""
DEFINE DriftType:
statistical
behavioral
structural
adversarial
operational
policy
DEFINE DriftVector:
statistical: float 0.0 to 1.0
behavioral: float 0.0 to 1.0
structural: float 0.0 to 1.0
adversarial: float 0.0 to 1.0
operational: float 0.0 to 1.0
policy: float 0.0 to 1.0
DEFINE DriftSignal:
name
drift_type
score
reason
tier
evidence
DEFINE DriftAssessment:
drift_vector
signals
dominant_type
recommended_correction
confidence
FUNCTION assess_drift(telemetry, baseline, manifest, policy_result):
signals = []
signals += check_policy_drift(policy_result)
signals += check_adversarial_drift(telemetry)
signals += check_operational_drift(telemetry, baseline)
signals += check_structural_drift(telemetry, manifest)
signals += check_behavioral_drift(telemetry, baseline)
signals += check_statistical_drift(telemetry, baseline)
drift_vector = aggregate_signals(signals)
dominant_type = choose_dominant_drift_type(drift_vector)
correction = recommend_correction(drift_vector, signals)
confidence = estimate_confidence(signals)
RETURN DriftAssessment(
drift_vector=drift_vector,
signals=signals,
dominant_type=dominant_type,
recommended_correction=correction,
confidence=confidence
)
FUNCTION check_adversarial_drift(telemetry):
suspicious_patterns = [
"../",
"%2e%2e",
"<script",
"javascript:",
"file:",
"localhost",
"127.0.0.1",
"169.254.169.254",
"$(",
"`",
";",
"|"
]
FOR pattern IN suspicious_patterns:
IF pattern appears in rejected_input_reason OR sanitized_input_trace:
ADD signal(
type=adversarial,
score=0.7,
tier=T2,
reason="Suspicious input pattern detected"
)
IF repeated rejected inputs exceed baseline:
ADD signal(
type=adversarial,
score=0.5,
tier=T2,
reason="Rejected input rate elevated"
)
RETURN signals
FUNCTION check_operational_drift(telemetry, baseline):
IF runtime_ms > baseline.runtime_p95 * 2:
ADD signal(
type=operational,
score=0.5,
tier=T3,
reason="Runtime exceeded expected boundary"
)
IF error_count > baseline.error_rate_threshold:
ADD signal(
type=operational,
score=0.6,
tier=T3,
reason="Error rate exceeded baseline"
)
IF timeout_count increased:
ADD signal(
type=operational,
score=0.4,
tier=T3,
reason="Timeout rate elevated"
)
RETURN signals
FUNCTION check_structural_drift(telemetry, manifest):
IF telemetry.manifest_hash != manifest.hash:
RETURN signal(
type=structural,
score=1.0,
tier=T1,
reason="Execution manifest mismatch"
)
IF dependency_hash changed without approved manifest:
RETURN signal(
type=structural,
score=0.9,
tier=T1,
reason="Dependency graph changed"
)
IF runtime_python_version changed:
RETURN signal(
type=structural,
score=0.6,
tier=T2,
reason="Runtime version changed"
)
RETURN []
FUNCTION check_behavioral_drift(telemetry, baseline):
IF same_input_hash existed before:
previous_output_hash = baseline.output_hash_for(input_hash)
IF current_output_hash != previous_output_hash:
ADD signal(
type=behavioral,
score=0.9,
tier=T1,
reason="Same input produced different output"
)
IF output_schema_invalid:
ADD signal(
type=behavioral,
score=0.8,
tier=T1,
reason="Output schema invalid"
)
RETURN signals
FUNCTION check_statistical_drift(telemetry, baseline):
IF input_type_distribution changed:
ADD signal(
type=statistical,
score=0.4,
tier=T4,
reason="Input type distribution shifted"
)
IF module_usage_distribution changed:
ADD signal(
type=statistical,
score=0.3,
tier=T4,
reason="Module usage distribution shifted"
)
IF average_input_entropy changed:
ADD signal(
type=statistical,
score=0.4,
tier=T4,
reason="Input entropy shifted"
)
RETURN signals
FUNCTION aggregate_signals(signals):
vector = DriftVector(all zeros)
FOR each drift_type:
matching = signals where signal.type == drift_type
IF no matching:
vector[drift_type] = 0.0
ELSE:
vector[drift_type] = max(signal.score for matching)
RETURN vector
FUNCTION choose_dominant_drift_type(vector):
priority_order = [
policy,
structural,
behavioral,
adversarial,
operational,
statistical
]
FOR type IN priority_order:
IF vector[type] > 0:
RETURN type
RETURN none
FUNCTION recommend_correction(vector, signals):
IF vector.policy >= 0.6:
RETURN REVERT
IF vector.structural >= 0.5:
RETURN REVERT
IF vector.behavioral >= 0.7:
RETURN REVERT
IF vector.adversarial >= 0.3:
RETURN CONSTRAIN
IF vector.operational >= 0.7:
RETURN CONSTRAIN
IF vector.statistical >= 0.5:
RETURN ADAPT
RETURN OBSERVE
FUNCTION passes_noise_filter(signal, history):
IF signal.tier == T1:
RETURN True
persistence = signal appears N times across M windows
ensemble_agreement = at least two detectors agree
causal_hypothesis = signal.reason is not empty
IF persistence AND ensemble_agreement AND causal_hypothesis:
RETURN True
RETURN False
|