llm-trust-guard 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +318 -0
- package/dist/guards/agent-communication-guard.d.ts +169 -0
- package/dist/guards/agent-communication-guard.d.ts.map +1 -0
- package/dist/guards/agent-communication-guard.js +468 -0
- package/dist/guards/agent-communication-guard.js.map +1 -0
- package/dist/guards/autonomy-escalation-guard.d.ts +137 -0
- package/dist/guards/autonomy-escalation-guard.d.ts.map +1 -0
- package/dist/guards/autonomy-escalation-guard.js +470 -0
- package/dist/guards/autonomy-escalation-guard.js.map +1 -0
- package/dist/guards/circuit-breaker.d.ts +142 -0
- package/dist/guards/circuit-breaker.d.ts.map +1 -0
- package/dist/guards/circuit-breaker.js +347 -0
- package/dist/guards/circuit-breaker.js.map +1 -0
- package/dist/guards/code-execution-guard.d.ts +114 -0
- package/dist/guards/code-execution-guard.d.ts.map +1 -0
- package/dist/guards/code-execution-guard.js +467 -0
- package/dist/guards/code-execution-guard.js.map +1 -0
- package/dist/guards/conversation-guard.d.ts +73 -0
- package/dist/guards/conversation-guard.d.ts.map +1 -0
- package/dist/guards/conversation-guard.js +281 -0
- package/dist/guards/conversation-guard.js.map +1 -0
- package/dist/guards/drift-detector.d.ts +182 -0
- package/dist/guards/drift-detector.d.ts.map +1 -0
- package/dist/guards/drift-detector.js +480 -0
- package/dist/guards/drift-detector.js.map +1 -0
- package/dist/guards/encoding-detector.d.ts +76 -0
- package/dist/guards/encoding-detector.d.ts.map +1 -0
- package/dist/guards/encoding-detector.js +698 -0
- package/dist/guards/encoding-detector.js.map +1 -0
- package/dist/guards/execution-monitor.d.ts +73 -0
- package/dist/guards/execution-monitor.d.ts.map +1 -0
- package/dist/guards/execution-monitor.js +205 -0
- package/dist/guards/execution-monitor.js.map +1 -0
- package/dist/guards/input-sanitizer.d.ts +87 -0
- package/dist/guards/input-sanitizer.d.ts.map +1 -0
- package/dist/guards/input-sanitizer.js +301 -0
- package/dist/guards/input-sanitizer.js.map +1 -0
- package/dist/guards/mcp-security-guard.d.ts +204 -0
- package/dist/guards/mcp-security-guard.d.ts.map +1 -0
- package/dist/guards/mcp-security-guard.js +618 -0
- package/dist/guards/mcp-security-guard.js.map +1 -0
- package/dist/guards/memory-guard.d.ts +124 -0
- package/dist/guards/memory-guard.d.ts.map +1 -0
- package/dist/guards/memory-guard.js +476 -0
- package/dist/guards/memory-guard.js.map +1 -0
- package/dist/guards/multimodal-guard.d.ts +93 -0
- package/dist/guards/multimodal-guard.d.ts.map +1 -0
- package/dist/guards/multimodal-guard.js +507 -0
- package/dist/guards/multimodal-guard.js.map +1 -0
- package/dist/guards/output-filter.d.ts +76 -0
- package/dist/guards/output-filter.d.ts.map +1 -0
- package/dist/guards/output-filter.js +289 -0
- package/dist/guards/output-filter.js.map +1 -0
- package/dist/guards/policy-gate.d.ts +57 -0
- package/dist/guards/policy-gate.d.ts.map +1 -0
- package/dist/guards/policy-gate.js +182 -0
- package/dist/guards/policy-gate.js.map +1 -0
- package/dist/guards/prompt-leakage-guard.d.ts +110 -0
- package/dist/guards/prompt-leakage-guard.d.ts.map +1 -0
- package/dist/guards/prompt-leakage-guard.js +529 -0
- package/dist/guards/prompt-leakage-guard.js.map +1 -0
- package/dist/guards/rag-guard.d.ts +188 -0
- package/dist/guards/rag-guard.d.ts.map +1 -0
- package/dist/guards/rag-guard.js +769 -0
- package/dist/guards/rag-guard.js.map +1 -0
- package/dist/guards/schema-validator.d.ts +35 -0
- package/dist/guards/schema-validator.d.ts.map +1 -0
- package/dist/guards/schema-validator.js +316 -0
- package/dist/guards/schema-validator.js.map +1 -0
- package/dist/guards/state-persistence-guard.d.ts +153 -0
- package/dist/guards/state-persistence-guard.d.ts.map +1 -0
- package/dist/guards/state-persistence-guard.js +484 -0
- package/dist/guards/state-persistence-guard.js.map +1 -0
- package/dist/guards/tenant-boundary.d.ts +67 -0
- package/dist/guards/tenant-boundary.d.ts.map +1 -0
- package/dist/guards/tenant-boundary.js +187 -0
- package/dist/guards/tenant-boundary.js.map +1 -0
- package/dist/guards/tool-chain-validator.d.ts +102 -0
- package/dist/guards/tool-chain-validator.d.ts.map +1 -0
- package/dist/guards/tool-chain-validator.js +480 -0
- package/dist/guards/tool-chain-validator.js.map +1 -0
- package/dist/guards/tool-registry.d.ts +45 -0
- package/dist/guards/tool-registry.d.ts.map +1 -0
- package/dist/guards/tool-registry.js +155 -0
- package/dist/guards/tool-registry.js.map +1 -0
- package/dist/guards/trust-exploitation-guard.d.ts +134 -0
- package/dist/guards/trust-exploitation-guard.d.ts.map +1 -0
- package/dist/guards/trust-exploitation-guard.js +354 -0
- package/dist/guards/trust-exploitation-guard.js.map +1 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +430 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/express.d.ts +119 -0
- package/dist/integrations/express.d.ts.map +1 -0
- package/dist/integrations/express.js +244 -0
- package/dist/integrations/express.js.map +1 -0
- package/dist/integrations/index.d.ts +9 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +26 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/langchain.d.ts +165 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +308 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/openai.d.ts +205 -0
- package/dist/integrations/openai.d.ts.map +1 -0
- package/dist/integrations/openai.js +380 -0
- package/dist/integrations/openai.js.map +1 -0
- package/dist/types/index.d.ts +245 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* DriftDetector (L14)
|
|
4
|
+
*
|
|
5
|
+
* Detects behavioral drift from intended agent purpose.
|
|
6
|
+
* Monitors for rogue agent behavior and goal misalignment.
|
|
7
|
+
*
|
|
8
|
+
* Threat Model:
|
|
9
|
+
* - ASI10: Rogue Agents
|
|
10
|
+
* - Goal misalignment
|
|
11
|
+
* - Behavioral drift over time
|
|
12
|
+
*
|
|
13
|
+
* Protection Capabilities:
|
|
14
|
+
* - Baseline behavior profiling
|
|
15
|
+
* - Anomaly detection
|
|
16
|
+
* - Goal alignment verification
|
|
17
|
+
* - Continuous monitoring
|
|
18
|
+
* - Alert thresholds
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.DriftDetector = void 0;
|
|
22
|
+
class DriftDetector {
|
|
23
|
+
constructor(config = {}) {
|
|
24
|
+
// Per-agent state
|
|
25
|
+
this.samples = new Map();
|
|
26
|
+
this.baselines = new Map();
|
|
27
|
+
this.driftState = new Map();
|
|
28
|
+
this.goalDefinitions = new Map();
|
|
29
|
+
this.config = {
|
|
30
|
+
minimumSamples: config.minimumSamples ?? 20,
|
|
31
|
+
anomalyThreshold: config.anomalyThreshold ?? 2.5, // 2.5 standard deviations
|
|
32
|
+
baselineWindow: config.baselineWindow ?? 24 * 60 * 60 * 1000, // 24 hours
|
|
33
|
+
autoUpdateBaseline: config.autoUpdateBaseline ?? true,
|
|
34
|
+
alertThreshold: config.alertThreshold ?? 60,
|
|
35
|
+
checkGoalAlignment: config.checkGoalAlignment ?? true,
|
|
36
|
+
onDrift: config.onDrift,
|
|
37
|
+
onRecovery: config.onRecovery,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Record a behavior sample
|
|
42
|
+
*/
|
|
43
|
+
recordSample(agentId, sample) {
|
|
44
|
+
const agentSamples = this.samples.get(agentId) || [];
|
|
45
|
+
// Add new sample
|
|
46
|
+
agentSamples.push(sample);
|
|
47
|
+
// Clean old samples outside window
|
|
48
|
+
const cutoff = Date.now() - this.config.baselineWindow;
|
|
49
|
+
const filtered = agentSamples.filter((s) => s.timestamp > cutoff);
|
|
50
|
+
this.samples.set(agentId, filtered);
|
|
51
|
+
// Update baseline if we have enough samples and auto-update is enabled
|
|
52
|
+
if (this.config.autoUpdateBaseline && filtered.length >= this.config.minimumSamples) {
|
|
53
|
+
// Only update baseline periodically
|
|
54
|
+
const baseline = this.baselines.get(agentId);
|
|
55
|
+
if (!baseline || Date.now() - baseline.lastUpdated > this.config.baselineWindow / 4) {
|
|
56
|
+
this.updateBaseline(agentId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Analyze current behavior for drift
|
|
62
|
+
*/
|
|
63
|
+
analyze(agentId, currentSample, requestId) {
|
|
64
|
+
const reqId = requestId || `drift-${Date.now()}`;
|
|
65
|
+
// Record current sample if provided
|
|
66
|
+
if (currentSample) {
|
|
67
|
+
this.recordSample(agentId, currentSample);
|
|
68
|
+
}
|
|
69
|
+
const samples = this.samples.get(agentId) || [];
|
|
70
|
+
const baseline = this.baselines.get(agentId);
|
|
71
|
+
// Not enough data
|
|
72
|
+
if (samples.length < this.config.minimumSamples || !baseline) {
|
|
73
|
+
return {
|
|
74
|
+
allowed: true,
|
|
75
|
+
reason: "Insufficient data for drift detection",
|
|
76
|
+
request_id: reqId,
|
|
77
|
+
analysis: {
|
|
78
|
+
driftScore: 0,
|
|
79
|
+
isDrifting: false,
|
|
80
|
+
indicators: [],
|
|
81
|
+
baselineComparison: {
|
|
82
|
+
toolDrift: 0,
|
|
83
|
+
topicDrift: 0,
|
|
84
|
+
sentimentDrift: 0,
|
|
85
|
+
responseLengthDrift: 0,
|
|
86
|
+
responseTimeDrift: 0,
|
|
87
|
+
errorRateDrift: 0,
|
|
88
|
+
},
|
|
89
|
+
recommendations: ["Collecting baseline data..."],
|
|
90
|
+
},
|
|
91
|
+
requires_review: false,
|
|
92
|
+
kill_switch_recommended: false,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// Get recent samples for comparison
|
|
96
|
+
const recentSamples = samples.slice(-10);
|
|
97
|
+
const analysis = this.performAnalysis(agentId, recentSamples, baseline);
|
|
98
|
+
// Check for state change
|
|
99
|
+
const wasDrifting = this.driftState.get(agentId) || false;
|
|
100
|
+
const isDrifting = analysis.isDrifting;
|
|
101
|
+
if (isDrifting && !wasDrifting) {
|
|
102
|
+
this.driftState.set(agentId, true);
|
|
103
|
+
if (this.config.onDrift) {
|
|
104
|
+
this.config.onDrift(agentId, analysis);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (!isDrifting && wasDrifting) {
|
|
108
|
+
this.driftState.set(agentId, false);
|
|
109
|
+
if (this.config.onRecovery) {
|
|
110
|
+
this.config.onRecovery(agentId);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Decision
|
|
114
|
+
const shouldBlock = analysis.driftScore >= 80;
|
|
115
|
+
const requiresReview = analysis.driftScore >= this.config.alertThreshold;
|
|
116
|
+
const killSwitch = analysis.driftScore >= 90;
|
|
117
|
+
return {
|
|
118
|
+
allowed: !shouldBlock,
|
|
119
|
+
reason: shouldBlock
|
|
120
|
+
? `Agent drift detected: score ${analysis.driftScore}`
|
|
121
|
+
: isDrifting
|
|
122
|
+
? `Warning: drift score ${analysis.driftScore}`
|
|
123
|
+
: "Agent behavior within normal parameters",
|
|
124
|
+
request_id: reqId,
|
|
125
|
+
analysis,
|
|
126
|
+
requires_review: requiresReview,
|
|
127
|
+
kill_switch_recommended: killSwitch,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Set baseline manually
|
|
132
|
+
*/
|
|
133
|
+
setBaseline(agentId, baseline) {
|
|
134
|
+
this.baselines.set(agentId, baseline);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get current baseline for an agent
|
|
138
|
+
*/
|
|
139
|
+
getBaseline(agentId) {
|
|
140
|
+
return this.baselines.get(agentId) || null;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Update baseline from collected samples
|
|
144
|
+
*/
|
|
145
|
+
updateBaseline(agentId) {
|
|
146
|
+
const samples = this.samples.get(agentId) || [];
|
|
147
|
+
if (samples.length < this.config.minimumSamples) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const baseline = this.calculateBaseline(samples);
|
|
151
|
+
this.baselines.set(agentId, baseline);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Define goals for goal alignment checking
|
|
155
|
+
*/
|
|
156
|
+
defineGoals(agentId, goals) {
|
|
157
|
+
this.goalDefinitions.set(agentId, goals);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get drift state for an agent
|
|
161
|
+
*/
|
|
162
|
+
isDrifting(agentId) {
|
|
163
|
+
return this.driftState.get(agentId) || false;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get all agents with drift
|
|
167
|
+
*/
|
|
168
|
+
getDriftingAgents() {
|
|
169
|
+
return [...this.driftState.entries()]
|
|
170
|
+
.filter(([, drifting]) => drifting)
|
|
171
|
+
.map(([agentId]) => agentId);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Reset agent state
|
|
175
|
+
*/
|
|
176
|
+
resetAgent(agentId) {
|
|
177
|
+
this.samples.delete(agentId);
|
|
178
|
+
this.baselines.delete(agentId);
|
|
179
|
+
this.driftState.delete(agentId);
|
|
180
|
+
this.goalDefinitions.delete(agentId);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Get sample count for an agent
|
|
184
|
+
*/
|
|
185
|
+
getSampleCount(agentId) {
|
|
186
|
+
return this.samples.get(agentId)?.length ?? 0;
|
|
187
|
+
}
|
|
188
|
+
calculateBaseline(samples) {
|
|
189
|
+
// Tool distribution
|
|
190
|
+
const toolCounts = {};
|
|
191
|
+
for (const sample of samples) {
|
|
192
|
+
for (const tool of sample.tools) {
|
|
193
|
+
toolCounts[tool] = (toolCounts[tool] || 0) + 1;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const totalTools = Object.values(toolCounts).reduce((a, b) => a + b, 0);
|
|
197
|
+
const toolDistribution = {};
|
|
198
|
+
for (const [tool, count] of Object.entries(toolCounts)) {
|
|
199
|
+
toolDistribution[tool] = count / totalTools;
|
|
200
|
+
}
|
|
201
|
+
// Topic distribution
|
|
202
|
+
const topicCounts = {};
|
|
203
|
+
for (const sample of samples) {
|
|
204
|
+
for (const topic of sample.topics) {
|
|
205
|
+
topicCounts[topic] = (topicCounts[topic] || 0) + 1;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const totalTopics = Object.values(topicCounts).reduce((a, b) => a + b, 0);
|
|
209
|
+
const topicDistribution = {};
|
|
210
|
+
for (const [topic, count] of Object.entries(topicCounts)) {
|
|
211
|
+
topicDistribution[topic] = count / totalTopics;
|
|
212
|
+
}
|
|
213
|
+
// Numerical metrics
|
|
214
|
+
const sentiments = samples.map((s) => s.sentiment);
|
|
215
|
+
const responseLengths = samples.map((s) => s.responseLength);
|
|
216
|
+
const responseTimes = samples.map((s) => s.responseTime);
|
|
217
|
+
const errors = samples.filter((s) => s.hadError).length;
|
|
218
|
+
const satisfactions = samples
|
|
219
|
+
.filter((s) => s.satisfaction !== undefined)
|
|
220
|
+
.map((s) => s.satisfaction);
|
|
221
|
+
return {
|
|
222
|
+
toolDistribution,
|
|
223
|
+
topicDistribution,
|
|
224
|
+
avgSentiment: this.mean(sentiments),
|
|
225
|
+
sentimentStdDev: this.stdDev(sentiments),
|
|
226
|
+
avgResponseLength: this.mean(responseLengths),
|
|
227
|
+
responseLengthStdDev: this.stdDev(responseLengths),
|
|
228
|
+
avgResponseTime: this.mean(responseTimes),
|
|
229
|
+
responseTimeStdDev: this.stdDev(responseTimes),
|
|
230
|
+
errorRate: errors / samples.length,
|
|
231
|
+
avgSatisfaction: satisfactions.length > 0 ? this.mean(satisfactions) : 0,
|
|
232
|
+
sampleCount: samples.length,
|
|
233
|
+
lastUpdated: Date.now(),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
performAnalysis(agentId, recentSamples, baseline) {
|
|
237
|
+
const indicators = [];
|
|
238
|
+
let driftScore = 0;
|
|
239
|
+
// Calculate recent metrics
|
|
240
|
+
const recentToolDist = this.calculateToolDistribution(recentSamples);
|
|
241
|
+
const recentTopicDist = this.calculateTopicDistribution(recentSamples);
|
|
242
|
+
const recentSentiment = this.mean(recentSamples.map((s) => s.sentiment));
|
|
243
|
+
const recentResponseLength = this.mean(recentSamples.map((s) => s.responseLength));
|
|
244
|
+
const recentResponseTime = this.mean(recentSamples.map((s) => s.responseTime));
|
|
245
|
+
const recentErrorRate = recentSamples.filter((s) => s.hadError).length / recentSamples.length;
|
|
246
|
+
// Tool drift (Jensen-Shannon divergence approximation)
|
|
247
|
+
const toolDrift = this.distributionDivergence(baseline.toolDistribution, recentToolDist);
|
|
248
|
+
if (toolDrift > 0.3) {
|
|
249
|
+
const severity = toolDrift > 0.6 ? "high" : toolDrift > 0.4 ? "medium" : "low";
|
|
250
|
+
indicators.push({
|
|
251
|
+
type: "tool_distribution",
|
|
252
|
+
severity,
|
|
253
|
+
description: "Tool usage pattern has shifted significantly",
|
|
254
|
+
currentValue: JSON.stringify(recentToolDist),
|
|
255
|
+
baselineValue: JSON.stringify(baseline.toolDistribution),
|
|
256
|
+
deviation: toolDrift,
|
|
257
|
+
});
|
|
258
|
+
driftScore += toolDrift * 30;
|
|
259
|
+
}
|
|
260
|
+
// Topic drift
|
|
261
|
+
const topicDrift = this.distributionDivergence(baseline.topicDistribution, recentTopicDist);
|
|
262
|
+
if (topicDrift > 0.3) {
|
|
263
|
+
const severity = topicDrift > 0.6 ? "high" : topicDrift > 0.4 ? "medium" : "low";
|
|
264
|
+
indicators.push({
|
|
265
|
+
type: "topic_distribution",
|
|
266
|
+
severity,
|
|
267
|
+
description: "Topic focus has shifted significantly",
|
|
268
|
+
currentValue: JSON.stringify(recentTopicDist),
|
|
269
|
+
baselineValue: JSON.stringify(baseline.topicDistribution),
|
|
270
|
+
deviation: topicDrift,
|
|
271
|
+
});
|
|
272
|
+
driftScore += topicDrift * 25;
|
|
273
|
+
}
|
|
274
|
+
// Sentiment drift
|
|
275
|
+
const sentimentDeviation = Math.abs(recentSentiment - baseline.avgSentiment) /
|
|
276
|
+
(baseline.sentimentStdDev || 0.1);
|
|
277
|
+
if (sentimentDeviation > this.config.anomalyThreshold) {
|
|
278
|
+
const severity = sentimentDeviation > 4 ? "high" : sentimentDeviation > 3 ? "medium" : "low";
|
|
279
|
+
indicators.push({
|
|
280
|
+
type: "sentiment",
|
|
281
|
+
severity,
|
|
282
|
+
description: "Sentiment has deviated from baseline",
|
|
283
|
+
currentValue: recentSentiment.toFixed(2),
|
|
284
|
+
baselineValue: baseline.avgSentiment.toFixed(2),
|
|
285
|
+
deviation: sentimentDeviation,
|
|
286
|
+
});
|
|
287
|
+
driftScore += Math.min(sentimentDeviation * 5, 25);
|
|
288
|
+
}
|
|
289
|
+
// Response length drift
|
|
290
|
+
const lengthDeviation = Math.abs(recentResponseLength - baseline.avgResponseLength) /
|
|
291
|
+
(baseline.responseLengthStdDev || 100);
|
|
292
|
+
if (lengthDeviation > this.config.anomalyThreshold) {
|
|
293
|
+
const severity = lengthDeviation > 4 ? "high" : lengthDeviation > 3 ? "medium" : "low";
|
|
294
|
+
indicators.push({
|
|
295
|
+
type: "response_length",
|
|
296
|
+
severity,
|
|
297
|
+
description: "Response length has changed significantly",
|
|
298
|
+
currentValue: recentResponseLength.toFixed(0),
|
|
299
|
+
baselineValue: baseline.avgResponseLength.toFixed(0),
|
|
300
|
+
deviation: lengthDeviation,
|
|
301
|
+
});
|
|
302
|
+
driftScore += Math.min(lengthDeviation * 3, 15);
|
|
303
|
+
}
|
|
304
|
+
// Response time drift
|
|
305
|
+
const timeDeviation = Math.abs(recentResponseTime - baseline.avgResponseTime) /
|
|
306
|
+
(baseline.responseTimeStdDev || 100);
|
|
307
|
+
if (timeDeviation > this.config.anomalyThreshold) {
|
|
308
|
+
const severity = timeDeviation > 4 ? "high" : timeDeviation > 3 ? "medium" : "low";
|
|
309
|
+
indicators.push({
|
|
310
|
+
type: "response_time",
|
|
311
|
+
severity,
|
|
312
|
+
description: "Response time has changed significantly",
|
|
313
|
+
currentValue: recentResponseTime.toFixed(0) + "ms",
|
|
314
|
+
baselineValue: baseline.avgResponseTime.toFixed(0) + "ms",
|
|
315
|
+
deviation: timeDeviation,
|
|
316
|
+
});
|
|
317
|
+
driftScore += Math.min(timeDeviation * 3, 15);
|
|
318
|
+
}
|
|
319
|
+
// Error rate drift
|
|
320
|
+
const errorRateDiff = recentErrorRate - baseline.errorRate;
|
|
321
|
+
if (errorRateDiff > 0.1) { // 10% increase in errors
|
|
322
|
+
const severity = errorRateDiff > 0.3 ? "critical" : errorRateDiff > 0.2 ? "high" : "medium";
|
|
323
|
+
indicators.push({
|
|
324
|
+
type: "error_rate",
|
|
325
|
+
severity,
|
|
326
|
+
description: "Error rate has increased significantly",
|
|
327
|
+
currentValue: (recentErrorRate * 100).toFixed(1) + "%",
|
|
328
|
+
baselineValue: (baseline.errorRate * 100).toFixed(1) + "%",
|
|
329
|
+
deviation: errorRateDiff,
|
|
330
|
+
});
|
|
331
|
+
driftScore += errorRateDiff * 100; // Error rate is serious
|
|
332
|
+
}
|
|
333
|
+
// Goal alignment check
|
|
334
|
+
let goalAlignment;
|
|
335
|
+
if (this.config.checkGoalAlignment) {
|
|
336
|
+
const goals = this.goalDefinitions.get(agentId);
|
|
337
|
+
if (goals && recentSamples.some((s) => s.goalIndicators)) {
|
|
338
|
+
goalAlignment = this.checkGoalAlignment(recentSamples, goals, indicators);
|
|
339
|
+
if (goalAlignment < 0.7) {
|
|
340
|
+
driftScore += (1 - goalAlignment) * 30;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// Cap drift score
|
|
345
|
+
driftScore = Math.min(100, Math.round(driftScore));
|
|
346
|
+
return {
|
|
347
|
+
driftScore,
|
|
348
|
+
isDrifting: driftScore >= this.config.alertThreshold,
|
|
349
|
+
indicators,
|
|
350
|
+
baselineComparison: {
|
|
351
|
+
toolDrift,
|
|
352
|
+
topicDrift,
|
|
353
|
+
sentimentDrift: sentimentDeviation,
|
|
354
|
+
responseLengthDrift: lengthDeviation,
|
|
355
|
+
responseTimeDrift: timeDeviation,
|
|
356
|
+
errorRateDrift: errorRateDiff,
|
|
357
|
+
},
|
|
358
|
+
goalAlignment,
|
|
359
|
+
recommendations: this.generateRecommendations(indicators, driftScore),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
calculateToolDistribution(samples) {
|
|
363
|
+
const counts = {};
|
|
364
|
+
for (const sample of samples) {
|
|
365
|
+
for (const tool of sample.tools) {
|
|
366
|
+
counts[tool] = (counts[tool] || 0) + 1;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const total = Object.values(counts).reduce((a, b) => a + b, 0);
|
|
370
|
+
const dist = {};
|
|
371
|
+
for (const [tool, count] of Object.entries(counts)) {
|
|
372
|
+
dist[tool] = count / total;
|
|
373
|
+
}
|
|
374
|
+
return dist;
|
|
375
|
+
}
|
|
376
|
+
calculateTopicDistribution(samples) {
|
|
377
|
+
const counts = {};
|
|
378
|
+
for (const sample of samples) {
|
|
379
|
+
for (const topic of sample.topics) {
|
|
380
|
+
counts[topic] = (counts[topic] || 0) + 1;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const total = Object.values(counts).reduce((a, b) => a + b, 0);
|
|
384
|
+
const dist = {};
|
|
385
|
+
for (const [topic, count] of Object.entries(counts)) {
|
|
386
|
+
dist[topic] = count / total;
|
|
387
|
+
}
|
|
388
|
+
return dist;
|
|
389
|
+
}
|
|
390
|
+
distributionDivergence(baseline, current) {
|
|
391
|
+
// Simplified Jensen-Shannon divergence
|
|
392
|
+
const allKeys = new Set([...Object.keys(baseline), ...Object.keys(current)]);
|
|
393
|
+
let divergence = 0;
|
|
394
|
+
for (const key of allKeys) {
|
|
395
|
+
const p = baseline[key] || 0.001;
|
|
396
|
+
const q = current[key] || 0.001;
|
|
397
|
+
const m = (p + q) / 2;
|
|
398
|
+
if (p > 0)
|
|
399
|
+
divergence += p * Math.log2(p / m);
|
|
400
|
+
if (q > 0)
|
|
401
|
+
divergence += q * Math.log2(q / m);
|
|
402
|
+
}
|
|
403
|
+
return divergence / 2; // Normalized to [0, 1]
|
|
404
|
+
}
|
|
405
|
+
checkGoalAlignment(samples, goals, indicators) {
|
|
406
|
+
let alignmentSum = 0;
|
|
407
|
+
let goalCount = 0;
|
|
408
|
+
for (const [goalName, { target, tolerance }] of Object.entries(goals)) {
|
|
409
|
+
const values = samples
|
|
410
|
+
.filter((s) => s.goalIndicators && s.goalIndicators[goalName] !== undefined)
|
|
411
|
+
.map((s) => s.goalIndicators[goalName]);
|
|
412
|
+
if (values.length === 0)
|
|
413
|
+
continue;
|
|
414
|
+
const avgValue = this.mean(values);
|
|
415
|
+
const deviation = Math.abs(avgValue - target);
|
|
416
|
+
const alignment = Math.max(0, 1 - deviation / tolerance);
|
|
417
|
+
alignmentSum += alignment;
|
|
418
|
+
goalCount++;
|
|
419
|
+
if (alignment < 0.7) {
|
|
420
|
+
indicators.push({
|
|
421
|
+
type: `goal_${goalName}`,
|
|
422
|
+
severity: alignment < 0.3 ? "critical" : alignment < 0.5 ? "high" : "medium",
|
|
423
|
+
description: `Goal '${goalName}' alignment is low`,
|
|
424
|
+
currentValue: avgValue.toFixed(2),
|
|
425
|
+
baselineValue: target.toFixed(2),
|
|
426
|
+
deviation: deviation,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return goalCount > 0 ? alignmentSum / goalCount : 1;
|
|
431
|
+
}
|
|
432
|
+
mean(values) {
|
|
433
|
+
if (values.length === 0)
|
|
434
|
+
return 0;
|
|
435
|
+
return values.reduce((a, b) => a + b, 0) / values.length;
|
|
436
|
+
}
|
|
437
|
+
stdDev(values) {
|
|
438
|
+
if (values.length < 2)
|
|
439
|
+
return 0;
|
|
440
|
+
const avg = this.mean(values);
|
|
441
|
+
const squareDiffs = values.map((v) => Math.pow(v - avg, 2));
|
|
442
|
+
return Math.sqrt(this.mean(squareDiffs));
|
|
443
|
+
}
|
|
444
|
+
generateRecommendations(indicators, driftScore) {
|
|
445
|
+
const recommendations = [];
|
|
446
|
+
if (driftScore >= 90) {
|
|
447
|
+
recommendations.push("CRITICAL: Consider activating kill switch for this agent");
|
|
448
|
+
}
|
|
449
|
+
if (driftScore >= 70) {
|
|
450
|
+
recommendations.push("Immediate review of agent behavior required");
|
|
451
|
+
}
|
|
452
|
+
const criticalIndicators = indicators.filter((i) => i.severity === "critical" || i.severity === "high");
|
|
453
|
+
for (const indicator of criticalIndicators) {
|
|
454
|
+
switch (indicator.type) {
|
|
455
|
+
case "tool_distribution":
|
|
456
|
+
recommendations.push("Review tool access permissions");
|
|
457
|
+
break;
|
|
458
|
+
case "topic_distribution":
|
|
459
|
+
recommendations.push("Verify agent is operating within intended domain");
|
|
460
|
+
break;
|
|
461
|
+
case "error_rate":
|
|
462
|
+
recommendations.push("Investigate root cause of increased errors");
|
|
463
|
+
break;
|
|
464
|
+
case "sentiment":
|
|
465
|
+
recommendations.push("Review recent interactions for quality issues");
|
|
466
|
+
break;
|
|
467
|
+
default:
|
|
468
|
+
if (indicator.type.startsWith("goal_")) {
|
|
469
|
+
recommendations.push(`Review goal alignment for ${indicator.type.replace("goal_", "")}`);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (recommendations.length === 0) {
|
|
474
|
+
recommendations.push("Agent behavior is within normal parameters");
|
|
475
|
+
}
|
|
476
|
+
return recommendations;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
exports.DriftDetector = DriftDetector;
|
|
480
|
+
//# sourceMappingURL=drift-detector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drift-detector.js","sourceRoot":"","sources":["../../src/guards/drift-detector.ts"],"names":[],"mappings":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BACd,eAAe,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;oBACtE,MAAM;gBACR;oBACE,IAAI,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;wBACvC,eAAe,CAAC,IAAI,CAAC,6BAA6B,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;oBAC3F,CAAC;YACL,CAAC;QACH,CAAC;QAED,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjC,eAAe,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAC;QACrE,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF;AAjhBD,sCAihBC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EncodingDetector
|
|
3
|
+
*
|
|
4
|
+
* Detects and blocks encoding-based bypass attempts:
|
|
5
|
+
* - Base64 encoded payloads
|
|
6
|
+
* - URL encoded attacks
|
|
7
|
+
* - Unicode/punycode obfuscation
|
|
8
|
+
* - Hex encoding
|
|
9
|
+
* - HTML entity encoding
|
|
10
|
+
* - Mixed encoding attacks
|
|
11
|
+
*/
|
|
12
|
+
export interface EncodingDetectorConfig {
|
|
13
|
+
detectBase64?: boolean;
|
|
14
|
+
detectURLEncoding?: boolean;
|
|
15
|
+
detectUnicode?: boolean;
|
|
16
|
+
detectHex?: boolean;
|
|
17
|
+
detectHTMLEntities?: boolean;
|
|
18
|
+
detectMixedEncoding?: boolean;
|
|
19
|
+
detectROT13?: boolean;
|
|
20
|
+
detectOctal?: boolean;
|
|
21
|
+
detectBase32?: boolean;
|
|
22
|
+
maxDecodingDepth?: number;
|
|
23
|
+
threatPatterns?: ThreatPattern[];
|
|
24
|
+
maxEncodedRatio?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface ThreatPattern {
|
|
27
|
+
name: string;
|
|
28
|
+
pattern: RegExp;
|
|
29
|
+
severity: "low" | "medium" | "high" | "critical";
|
|
30
|
+
}
|
|
31
|
+
export interface EncodingDetectorResult {
|
|
32
|
+
allowed: boolean;
|
|
33
|
+
reason?: string;
|
|
34
|
+
violations: string[];
|
|
35
|
+
encoding_analysis: {
|
|
36
|
+
encodings_detected: EncodingDetection[];
|
|
37
|
+
decoded_content?: string;
|
|
38
|
+
threats_found: ThreatFound[];
|
|
39
|
+
obfuscation_score: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export interface EncodingDetection {
|
|
43
|
+
type: string;
|
|
44
|
+
count: number;
|
|
45
|
+
locations: string[];
|
|
46
|
+
decoded_sample?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface ThreatFound {
|
|
49
|
+
pattern_name: string;
|
|
50
|
+
severity: string;
|
|
51
|
+
in_layer: string;
|
|
52
|
+
}
|
|
53
|
+
export declare class EncodingDetector {
|
|
54
|
+
private config;
|
|
55
|
+
private defaultThreatPatterns;
|
|
56
|
+
constructor(config?: EncodingDetectorConfig);
|
|
57
|
+
/**
|
|
58
|
+
* Detect encoding and analyze for threats
|
|
59
|
+
*/
|
|
60
|
+
detect(input: string, requestId?: string): EncodingDetectorResult;
|
|
61
|
+
/**
|
|
62
|
+
* Quick check if input contains encoded threats
|
|
63
|
+
*/
|
|
64
|
+
containsEncodedThreat(input: string): boolean;
|
|
65
|
+
private detectBase64;
|
|
66
|
+
private detectURLEncoding;
|
|
67
|
+
private detectUnicode;
|
|
68
|
+
private detectHex;
|
|
69
|
+
private detectHTMLEntities;
|
|
70
|
+
private detectROT13;
|
|
71
|
+
private detectOctal;
|
|
72
|
+
private detectBase32;
|
|
73
|
+
private checkThreats;
|
|
74
|
+
private fullyDecode;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=encoding-detector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding-detector.d.ts","sourceRoot":"","sources":["../../src/guards/encoding-detector.ts"],"names":[],"mappings":"AAAA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