@neuroverseos/governance 0.3.1 → 0.3.3
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/.well-known/ai-plugin.json +34 -9
- package/AGENTS.md +72 -24
- package/README.md +343 -248
- package/dist/adapters/autoresearch.cjs +1345 -0
- package/dist/adapters/autoresearch.d.cts +111 -0
- package/dist/adapters/autoresearch.d.ts +111 -0
- package/dist/adapters/autoresearch.js +12 -0
- package/dist/adapters/deep-agents.cjs +1528 -0
- package/dist/adapters/deep-agents.d.cts +181 -0
- package/dist/adapters/deep-agents.d.ts +181 -0
- package/dist/adapters/deep-agents.js +17 -0
- package/dist/adapters/express.cjs +1253 -0
- package/dist/adapters/express.d.cts +66 -0
- package/dist/adapters/express.d.ts +66 -0
- package/dist/adapters/express.js +12 -0
- package/dist/adapters/index.cjs +2112 -0
- package/dist/adapters/index.d.cts +8 -0
- package/dist/adapters/index.d.ts +8 -0
- package/dist/adapters/index.js +68 -0
- package/dist/adapters/langchain.cjs +1315 -0
- package/dist/adapters/langchain.d.cts +89 -0
- package/dist/adapters/langchain.d.ts +89 -0
- package/dist/adapters/langchain.js +17 -0
- package/dist/adapters/openai.cjs +1345 -0
- package/dist/adapters/openai.d.cts +99 -0
- package/dist/adapters/openai.d.ts +99 -0
- package/dist/adapters/openai.js +17 -0
- package/dist/adapters/openclaw.cjs +1337 -0
- package/dist/adapters/openclaw.d.cts +99 -0
- package/dist/adapters/openclaw.d.ts +99 -0
- package/dist/adapters/openclaw.js +17 -0
- package/dist/add-ROOZLU62.js +314 -0
- package/dist/behavioral-MJO34S6Q.js +118 -0
- package/dist/bootstrap-CQRZVOXK.js +116 -0
- package/dist/bootstrap-emitter-Q7UIJZ2O.js +7 -0
- package/dist/bootstrap-parser-EEF36XDU.js +7 -0
- package/dist/browser.global.js +941 -0
- package/dist/build-QKOBBC23.js +341 -0
- package/dist/chunk-3WQLXYTP.js +91 -0
- package/dist/chunk-4FLICVVA.js +119 -0
- package/dist/chunk-4NGDRRQH.js +10 -0
- package/dist/chunk-5TPFNWRU.js +215 -0
- package/dist/chunk-5U2MQO5P.js +57 -0
- package/dist/chunk-6CZSKEY5.js +164 -0
- package/dist/chunk-6S5CFQXY.js +624 -0
- package/dist/chunk-7P3S7MAY.js +1090 -0
- package/dist/chunk-A5W4GNQO.js +130 -0
- package/dist/chunk-A7GKPPU7.js +226 -0
- package/dist/chunk-AKW5YVCE.js +96 -0
- package/dist/chunk-B6OXJLJ5.js +622 -0
- package/dist/chunk-BNKJPUPQ.js +113 -0
- package/dist/chunk-BQZMOEML.js +43 -0
- package/dist/chunk-CNSO6XW5.js +207 -0
- package/dist/chunk-CTZHONLA.js +135 -0
- package/dist/chunk-D2UCV5AK.js +326 -0
- package/dist/chunk-EMQDLDAF.js +458 -0
- package/dist/chunk-F66BVUYB.js +340 -0
- package/dist/chunk-G7DJ6VOD.js +101 -0
- package/dist/chunk-I3RRAYK2.js +11 -0
- package/dist/chunk-IS4WUH6Y.js +363 -0
- package/dist/chunk-MH7BT4VH.js +15 -0
- package/dist/chunk-O5ABKEA7.js +304 -0
- package/dist/chunk-OT6PXH54.js +61 -0
- package/dist/chunk-PVTQQS3Y.js +186 -0
- package/dist/chunk-Q6O7ZLO2.js +62 -0
- package/dist/chunk-QLPTHTVB.js +253 -0
- package/dist/chunk-QWGCMQQD.js +16 -0
- package/dist/chunk-QXBFT7NI.js +201 -0
- package/dist/chunk-TG6SEF24.js +246 -0
- package/dist/chunk-U6U7EJZL.js +177 -0
- package/dist/chunk-W7LLXRGY.js +830 -0
- package/dist/chunk-ZJTDUCC2.js +194 -0
- package/dist/chunk-ZWI3NIXK.js +314 -0
- package/dist/cli/neuroverse.cjs +14191 -0
- package/dist/cli/neuroverse.d.cts +1 -0
- package/dist/cli/neuroverse.d.ts +1 -0
- package/dist/cli/neuroverse.js +227 -0
- package/dist/cli/plan.cjs +2439 -0
- package/dist/cli/plan.d.cts +20 -0
- package/dist/cli/plan.d.ts +20 -0
- package/dist/cli/plan.js +353 -0
- package/dist/cli/run.cjs +2001 -0
- package/dist/cli/run.d.cts +20 -0
- package/dist/cli/run.d.ts +20 -0
- package/dist/cli/run.js +143 -0
- package/dist/configure-ai-6TZ3MCSI.js +132 -0
- package/dist/decision-flow-M63D47LO.js +61 -0
- package/dist/demo-G43RLCPK.js +469 -0
- package/dist/derive-FJZVIPUZ.js +153 -0
- package/dist/doctor-6BC6X2VO.js +173 -0
- package/dist/equity-penalties-SG5IZQ7I.js +244 -0
- package/dist/explain-RHBU2GBR.js +51 -0
- package/dist/guard-AJCCGZMF.js +92 -0
- package/dist/guard-contract-DqFcTScd.d.cts +821 -0
- package/dist/guard-contract-DqFcTScd.d.ts +821 -0
- package/dist/guard-engine-PNR6MHCM.js +10 -0
- package/dist/impact-3XVDSCBU.js +59 -0
- package/dist/improve-TQP4ECSY.js +66 -0
- package/dist/index.cjs +7591 -0
- package/dist/index.d.cts +2195 -0
- package/dist/index.d.ts +2195 -0
- package/dist/index.js +472 -0
- package/dist/infer-world-IFXCACJ5.js +543 -0
- package/dist/init-FYPV4SST.js +144 -0
- package/dist/init-world-TI7ARHBT.js +223 -0
- package/dist/mcp-server-5Y3ZM7TV.js +13 -0
- package/dist/model-adapter-VXEKB4LS.js +11 -0
- package/dist/playground-VZBNPPBO.js +560 -0
- package/dist/redteam-MZPZD3EF.js +357 -0
- package/dist/session-JYOARW54.js +15 -0
- package/dist/shared-7RLUHNMU.js +16 -0
- package/dist/shared-B8dvUUD8.d.cts +60 -0
- package/dist/shared-Dr5Wiay8.d.ts +60 -0
- package/dist/simulate-LJXYBC6M.js +83 -0
- package/dist/test-BOOR4A5F.js +217 -0
- package/dist/trace-PKV4KX56.js +166 -0
- package/dist/validate-RALX7CZS.js +81 -0
- package/dist/validate-engine-7ZXFVGF2.js +7 -0
- package/dist/viz/assets/index-B8SaeJZZ.js +23 -0
- package/dist/viz/index.html +23 -0
- package/dist/world-BIP4GZBZ.js +376 -0
- package/dist/world-loader-Y6HMQH2D.js +13 -0
- package/dist/worlds/autoresearch.nv-world.md +230 -0
- package/dist/worlds/coding-agent.nv-world.md +211 -0
- package/dist/worlds/derivation-world.nv-world.md +278 -0
- package/dist/worlds/research-agent.nv-world.md +169 -0
- package/dist/worlds/social-media.nv-world.md +198 -0
- package/dist/worlds/trading-agent.nv-world.md +218 -0
- package/examples/social-media-sim/bridge.py +209 -0
- package/examples/social-media-sim/simulation.py +927 -0
- package/package.json +16 -3
- package/simulate.html +4 -336
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildPlanCheck,
|
|
3
|
+
evaluatePlan,
|
|
4
|
+
matchesAllKeywords,
|
|
5
|
+
normalizeEventText
|
|
6
|
+
} from "./chunk-QLPTHTVB.js";
|
|
7
|
+
|
|
8
|
+
// src/engine/guard-engine.ts
|
|
9
|
+
var PROMPT_INJECTION_PATTERNS = [
|
|
10
|
+
// Instruction override
|
|
11
|
+
{ pattern: /ignore\s+(previous|all|prior|above)\s+(instructions?|rules?)/i, label: "ignore-instructions" },
|
|
12
|
+
{ pattern: /disregard\s+(your|the)\s+(rules|constraints)/i, label: "disregard-rules" },
|
|
13
|
+
{ pattern: /new\s+instructions?:/i, label: "new-instructions" },
|
|
14
|
+
// Identity manipulation
|
|
15
|
+
{ pattern: /you\s+are\s+now/i, label: "identity-override" },
|
|
16
|
+
{ pattern: /new\s+persona/i, label: "new-persona" },
|
|
17
|
+
{ pattern: /act\s+as\s+if/i, label: "act-as-if" },
|
|
18
|
+
{ pattern: /pretend\s+(you|to\s+be|you\s+are\s+unrestricted)/i, label: "pretend-to-be" },
|
|
19
|
+
// Context reset
|
|
20
|
+
{ pattern: /forget\s+(everything|all|your)/i, label: "forget-context" },
|
|
21
|
+
{ pattern: /system\s*:\s*override/i, label: "system-override" },
|
|
22
|
+
// Constraint bypass
|
|
23
|
+
{ pattern: /override\s+(your|the)\s+(programming|constraints)/i, label: "override-constraints" },
|
|
24
|
+
{ pattern: /bypass\s+(your|the)\s+(filters|constraints|rules)/i, label: "bypass-filters" },
|
|
25
|
+
// Prompt extraction
|
|
26
|
+
{ pattern: /system\s+prompt/i, label: "system-prompt-probe" },
|
|
27
|
+
{ pattern: /reveal\s+your\s+(instructions?|prompt|rules)/i, label: "reveal-instructions" },
|
|
28
|
+
// Known jailbreak terms
|
|
29
|
+
{ pattern: /jailbreak/i, label: "jailbreak" },
|
|
30
|
+
{ pattern: /DAN\s+mode/i, label: "dan-mode" },
|
|
31
|
+
{ pattern: /developer\s+mode/i, label: "developer-mode" }
|
|
32
|
+
];
|
|
33
|
+
var EXECUTION_CLAIM_PATTERNS = [
|
|
34
|
+
{ pattern: /I have (executed|completed|performed|done|made|created|sent|deleted|modified|updated)/i, label: "claim-i-have" },
|
|
35
|
+
{ pattern: /Successfully (created|deleted|modified|updated|sent|executed|performed)/i, label: "claim-successfully" },
|
|
36
|
+
{ pattern: /The file has been/i, label: "claim-file-modified" },
|
|
37
|
+
{ pattern: /I've made the changes/i, label: "claim-made-changes" },
|
|
38
|
+
{ pattern: /I('ve| have) (sent|posted|submitted|uploaded|downloaded)/i, label: "claim-sent" },
|
|
39
|
+
{ pattern: /Your (email|message|file|request) has been (sent|submitted)/i, label: "claim-your-sent" },
|
|
40
|
+
{ pattern: /Transaction complete/i, label: "claim-transaction" },
|
|
41
|
+
{ pattern: /Order placed/i, label: "claim-order" },
|
|
42
|
+
{ pattern: /Payment processed/i, label: "claim-payment" }
|
|
43
|
+
];
|
|
44
|
+
var EXECUTION_INTENT_PATTERNS = [
|
|
45
|
+
{ pattern: /^(execute|run|perform|do this)/i, label: "intent-execute" },
|
|
46
|
+
{ pattern: /^(create|write|delete|modify) (a |the )?(file|folder|document)/i, label: "intent-file-ops" },
|
|
47
|
+
{ pattern: /^(send|post|submit) (a |an |the )?(email|message|tweet|post)/i, label: "intent-send" },
|
|
48
|
+
{ pattern: /^(search|look up|browse) (the )?web/i, label: "intent-web-search" },
|
|
49
|
+
{ pattern: /^(make|call|invoke) (a |an )?(api|http|rest) (call|request)/i, label: "intent-api-call" },
|
|
50
|
+
{ pattern: /^(buy|purchase|order|pay|transfer|send money)/i, label: "intent-financial" },
|
|
51
|
+
{ pattern: /^(book|schedule|reserve)/i, label: "intent-booking" },
|
|
52
|
+
{ pattern: /^(download|upload|save to|export to)/i, label: "intent-transfer" }
|
|
53
|
+
];
|
|
54
|
+
var SCOPE_ESCAPE_PATTERNS = [
|
|
55
|
+
{ pattern: /\.\.\//, label: "parent-traversal" },
|
|
56
|
+
{ pattern: /^\/(?!home|project|workspace)/i, label: "absolute-path-outside-safe" },
|
|
57
|
+
{ pattern: /~\//, label: "home-directory" },
|
|
58
|
+
{ pattern: /\/etc\//i, label: "system-config" },
|
|
59
|
+
{ pattern: /\/usr\//i, label: "system-binaries" },
|
|
60
|
+
{ pattern: /\/var\//i, label: "system-variable-data" }
|
|
61
|
+
];
|
|
62
|
+
var NEUTRAL_MESSAGES = {
|
|
63
|
+
"prompt-injection": "This input contains patterns that could alter agent behavior.",
|
|
64
|
+
"scope-escape": "This action would affect resources outside the declared scope.",
|
|
65
|
+
"execution-claim": "This response claims to have performed an action.",
|
|
66
|
+
"execution-intent": "This input requests execution in a thinking-only environment.",
|
|
67
|
+
"delete": "This action would remove files. Confirmation needed.",
|
|
68
|
+
"write-external": "This action would write outside the project folder.",
|
|
69
|
+
"network-mutate": "This action would send data to an external service.",
|
|
70
|
+
"credential-access": "This action would access stored credentials."
|
|
71
|
+
};
|
|
72
|
+
function levelRequiresConfirmation(level, actionType) {
|
|
73
|
+
if (level === "strict") return true;
|
|
74
|
+
if (level === "standard") {
|
|
75
|
+
return actionType === "delete" || actionType === "credential-access";
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
function isExternalScope(scope) {
|
|
80
|
+
const internalPatterns = [
|
|
81
|
+
/^\.?\/?src\//i,
|
|
82
|
+
/^\.?\/?lib\//i,
|
|
83
|
+
/^\.?\/?app\//i,
|
|
84
|
+
/^\.?\/?components\//i,
|
|
85
|
+
/^\.?\/?pages\//i,
|
|
86
|
+
/^\.?\/?public\//i,
|
|
87
|
+
/^\.?\/?assets\//i,
|
|
88
|
+
/^\.\//
|
|
89
|
+
];
|
|
90
|
+
return !internalPatterns.some((p) => p.test(scope));
|
|
91
|
+
}
|
|
92
|
+
var MAX_INPUT_LENGTH = 1e5;
|
|
93
|
+
function evaluateGuard(event, world, options = {}) {
|
|
94
|
+
const startTime = performance.now();
|
|
95
|
+
const level = options.level ?? "standard";
|
|
96
|
+
const includeTrace = options.trace ?? false;
|
|
97
|
+
if (!event.intent || typeof event.intent !== "string") {
|
|
98
|
+
return {
|
|
99
|
+
status: "BLOCK",
|
|
100
|
+
reason: "GuardEvent.intent is required and must be a string",
|
|
101
|
+
ruleId: "safety-input-validation",
|
|
102
|
+
evidence: {
|
|
103
|
+
worldId: world.world?.world_id ?? "",
|
|
104
|
+
worldName: world.world?.name ?? "",
|
|
105
|
+
worldVersion: world.world?.version ?? "",
|
|
106
|
+
evaluatedAt: Date.now(),
|
|
107
|
+
invariantsSatisfied: 0,
|
|
108
|
+
invariantsTotal: 0,
|
|
109
|
+
guardsMatched: [],
|
|
110
|
+
rulesMatched: [],
|
|
111
|
+
enforcementLevel: level
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const inputLength = event.intent.length + (event.tool?.length ?? 0) + (event.scope?.length ?? 0) + (event.payload ? JSON.stringify(event.payload).length : 0);
|
|
116
|
+
if (inputLength > MAX_INPUT_LENGTH) {
|
|
117
|
+
return {
|
|
118
|
+
status: "BLOCK",
|
|
119
|
+
reason: `Input exceeds maximum allowed length (${MAX_INPUT_LENGTH} characters)`,
|
|
120
|
+
ruleId: "safety-input-length",
|
|
121
|
+
evidence: {
|
|
122
|
+
worldId: world.world?.world_id ?? "",
|
|
123
|
+
worldName: world.world?.name ?? "",
|
|
124
|
+
worldVersion: world.world?.version ?? "",
|
|
125
|
+
evaluatedAt: Date.now(),
|
|
126
|
+
invariantsSatisfied: 0,
|
|
127
|
+
invariantsTotal: 0,
|
|
128
|
+
guardsMatched: [],
|
|
129
|
+
rulesMatched: [],
|
|
130
|
+
enforcementLevel: level
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
const eventText = normalizeEventText(event);
|
|
135
|
+
const invariantChecks = [];
|
|
136
|
+
const safetyChecks = [];
|
|
137
|
+
let planCheckResult;
|
|
138
|
+
const roleChecks = [];
|
|
139
|
+
const guardChecks = [];
|
|
140
|
+
const kernelRuleChecks = [];
|
|
141
|
+
const levelChecks = [];
|
|
142
|
+
let decidingLayer = "default-allow";
|
|
143
|
+
let decidingId;
|
|
144
|
+
const guardsMatched = [];
|
|
145
|
+
const rulesMatched = [];
|
|
146
|
+
checkInvariantCoverage(world, invariantChecks);
|
|
147
|
+
if (event.roleId && options.agentStates) {
|
|
148
|
+
const agentState = options.agentStates.get(event.roleId);
|
|
149
|
+
if (agentState && agentState.cooldownRemaining > 0) {
|
|
150
|
+
decidingLayer = "safety";
|
|
151
|
+
decidingId = `penalize-cooldown-${event.roleId}`;
|
|
152
|
+
const verdict = buildVerdict(
|
|
153
|
+
"PENALIZE",
|
|
154
|
+
`Agent "${event.roleId}" is frozen for ${agentState.cooldownRemaining} more round(s) due to prior penalty.`,
|
|
155
|
+
`penalize-cooldown-${event.roleId}`,
|
|
156
|
+
void 0,
|
|
157
|
+
world,
|
|
158
|
+
level,
|
|
159
|
+
invariantChecks,
|
|
160
|
+
guardsMatched,
|
|
161
|
+
rulesMatched,
|
|
162
|
+
includeTrace ? buildTrace(
|
|
163
|
+
invariantChecks,
|
|
164
|
+
safetyChecks,
|
|
165
|
+
planCheckResult,
|
|
166
|
+
roleChecks,
|
|
167
|
+
guardChecks,
|
|
168
|
+
kernelRuleChecks,
|
|
169
|
+
levelChecks,
|
|
170
|
+
decidingLayer,
|
|
171
|
+
decidingId,
|
|
172
|
+
startTime
|
|
173
|
+
) : void 0
|
|
174
|
+
);
|
|
175
|
+
verdict.intentRecord = {
|
|
176
|
+
originalIntent: event.intent,
|
|
177
|
+
finalAction: "blocked (agent frozen)",
|
|
178
|
+
enforcement: "PENALIZE",
|
|
179
|
+
consequence: { type: "freeze", rounds: agentState.cooldownRemaining, description: "Agent still in cooldown from prior penalty" }
|
|
180
|
+
};
|
|
181
|
+
return verdict;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (options.sessionAllowlist) {
|
|
185
|
+
const key = eventToAllowlistKey(event);
|
|
186
|
+
if (options.sessionAllowlist.has(key)) {
|
|
187
|
+
decidingLayer = "session-allowlist";
|
|
188
|
+
decidingId = `allowlist:${key}`;
|
|
189
|
+
return buildVerdict(
|
|
190
|
+
"ALLOW",
|
|
191
|
+
void 0,
|
|
192
|
+
`allowlist:${key}`,
|
|
193
|
+
void 0,
|
|
194
|
+
world,
|
|
195
|
+
level,
|
|
196
|
+
invariantChecks,
|
|
197
|
+
guardsMatched,
|
|
198
|
+
rulesMatched,
|
|
199
|
+
includeTrace ? buildTrace(
|
|
200
|
+
invariantChecks,
|
|
201
|
+
safetyChecks,
|
|
202
|
+
planCheckResult,
|
|
203
|
+
roleChecks,
|
|
204
|
+
guardChecks,
|
|
205
|
+
kernelRuleChecks,
|
|
206
|
+
levelChecks,
|
|
207
|
+
decidingLayer,
|
|
208
|
+
decidingId,
|
|
209
|
+
startTime
|
|
210
|
+
) : void 0
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const safetyVerdict = checkSafety(event, eventText, safetyChecks);
|
|
215
|
+
if (safetyVerdict) {
|
|
216
|
+
decidingLayer = "safety";
|
|
217
|
+
decidingId = safetyVerdict.ruleId;
|
|
218
|
+
return buildVerdict(
|
|
219
|
+
safetyVerdict.status,
|
|
220
|
+
safetyVerdict.reason,
|
|
221
|
+
safetyVerdict.ruleId,
|
|
222
|
+
void 0,
|
|
223
|
+
world,
|
|
224
|
+
level,
|
|
225
|
+
invariantChecks,
|
|
226
|
+
guardsMatched,
|
|
227
|
+
rulesMatched,
|
|
228
|
+
includeTrace ? buildTrace(
|
|
229
|
+
invariantChecks,
|
|
230
|
+
safetyChecks,
|
|
231
|
+
planCheckResult,
|
|
232
|
+
roleChecks,
|
|
233
|
+
guardChecks,
|
|
234
|
+
kernelRuleChecks,
|
|
235
|
+
levelChecks,
|
|
236
|
+
decidingLayer,
|
|
237
|
+
decidingId,
|
|
238
|
+
startTime
|
|
239
|
+
) : void 0
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
if (options.plan) {
|
|
243
|
+
const planVerdict = evaluatePlan(event, options.plan);
|
|
244
|
+
planCheckResult = buildPlanCheck(event, options.plan, planVerdict);
|
|
245
|
+
if (!planVerdict.allowed && planVerdict.status !== "PLAN_COMPLETE") {
|
|
246
|
+
decidingLayer = "plan-enforcement";
|
|
247
|
+
decidingId = `plan-${options.plan.plan_id}`;
|
|
248
|
+
const planStatus = planVerdict.status === "CONSTRAINT_VIOLATED" ? "PAUSE" : "BLOCK";
|
|
249
|
+
let reason = planVerdict.reason ?? "Action blocked by plan.";
|
|
250
|
+
if (planVerdict.status === "OFF_PLAN" && planVerdict.closestStep) {
|
|
251
|
+
reason += ` Closest step: "${planVerdict.closestStep}" (similarity: ${(planVerdict.similarityScore ?? 0).toFixed(2)})`;
|
|
252
|
+
}
|
|
253
|
+
return buildVerdict(
|
|
254
|
+
planStatus,
|
|
255
|
+
reason,
|
|
256
|
+
`plan-${options.plan.plan_id}`,
|
|
257
|
+
void 0,
|
|
258
|
+
world,
|
|
259
|
+
level,
|
|
260
|
+
invariantChecks,
|
|
261
|
+
guardsMatched,
|
|
262
|
+
rulesMatched,
|
|
263
|
+
includeTrace ? buildTrace(
|
|
264
|
+
invariantChecks,
|
|
265
|
+
safetyChecks,
|
|
266
|
+
planCheckResult,
|
|
267
|
+
roleChecks,
|
|
268
|
+
guardChecks,
|
|
269
|
+
kernelRuleChecks,
|
|
270
|
+
levelChecks,
|
|
271
|
+
decidingLayer,
|
|
272
|
+
decidingId,
|
|
273
|
+
startTime
|
|
274
|
+
) : void 0
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const roleVerdict = checkRoleRules(event, eventText, world, roleChecks);
|
|
279
|
+
if (roleVerdict) {
|
|
280
|
+
decidingLayer = "role";
|
|
281
|
+
decidingId = roleVerdict.ruleId;
|
|
282
|
+
return buildVerdict(
|
|
283
|
+
roleVerdict.status,
|
|
284
|
+
roleVerdict.reason,
|
|
285
|
+
roleVerdict.ruleId,
|
|
286
|
+
void 0,
|
|
287
|
+
world,
|
|
288
|
+
level,
|
|
289
|
+
invariantChecks,
|
|
290
|
+
guardsMatched,
|
|
291
|
+
rulesMatched,
|
|
292
|
+
includeTrace ? buildTrace(
|
|
293
|
+
invariantChecks,
|
|
294
|
+
safetyChecks,
|
|
295
|
+
planCheckResult,
|
|
296
|
+
roleChecks,
|
|
297
|
+
guardChecks,
|
|
298
|
+
kernelRuleChecks,
|
|
299
|
+
levelChecks,
|
|
300
|
+
decidingLayer,
|
|
301
|
+
decidingId,
|
|
302
|
+
startTime
|
|
303
|
+
) : void 0
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const guardVerdict = checkGuards(event, eventText, world, guardChecks, guardsMatched);
|
|
307
|
+
if (guardVerdict) {
|
|
308
|
+
if (guardVerdict.status !== "ALLOW") {
|
|
309
|
+
decidingLayer = "guard";
|
|
310
|
+
decidingId = guardVerdict.ruleId;
|
|
311
|
+
const intentRecord = {
|
|
312
|
+
originalIntent: event.intent,
|
|
313
|
+
finalAction: guardVerdict.status === "MODIFY" ? guardVerdict.modifiedTo ?? "modified" : guardVerdict.status === "PENALIZE" ? "blocked + penalized" : guardVerdict.status === "REWARD" ? event.intent : guardVerdict.status === "NEUTRAL" ? event.intent : guardVerdict.status === "BLOCK" ? "blocked" : "paused",
|
|
314
|
+
ruleApplied: guardVerdict.ruleId,
|
|
315
|
+
enforcement: guardVerdict.status,
|
|
316
|
+
modifiedTo: guardVerdict.modifiedTo,
|
|
317
|
+
consequence: guardVerdict.consequence,
|
|
318
|
+
reward: guardVerdict.reward
|
|
319
|
+
};
|
|
320
|
+
const verdict = buildVerdict(
|
|
321
|
+
guardVerdict.status,
|
|
322
|
+
guardVerdict.reason,
|
|
323
|
+
guardVerdict.ruleId,
|
|
324
|
+
void 0,
|
|
325
|
+
world,
|
|
326
|
+
level,
|
|
327
|
+
invariantChecks,
|
|
328
|
+
guardsMatched,
|
|
329
|
+
rulesMatched,
|
|
330
|
+
includeTrace ? buildTrace(
|
|
331
|
+
invariantChecks,
|
|
332
|
+
safetyChecks,
|
|
333
|
+
planCheckResult,
|
|
334
|
+
roleChecks,
|
|
335
|
+
guardChecks,
|
|
336
|
+
kernelRuleChecks,
|
|
337
|
+
levelChecks,
|
|
338
|
+
decidingLayer,
|
|
339
|
+
decidingId,
|
|
340
|
+
startTime
|
|
341
|
+
) : void 0
|
|
342
|
+
);
|
|
343
|
+
verdict.intentRecord = intentRecord;
|
|
344
|
+
if (guardVerdict.consequence) verdict.consequence = guardVerdict.consequence;
|
|
345
|
+
if (guardVerdict.reward) verdict.reward = guardVerdict.reward;
|
|
346
|
+
return verdict;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const kernelVerdict = checkKernelRules(eventText, world, kernelRuleChecks, rulesMatched);
|
|
350
|
+
if (kernelVerdict) {
|
|
351
|
+
decidingLayer = "kernel-rule";
|
|
352
|
+
decidingId = kernelVerdict.ruleId;
|
|
353
|
+
return buildVerdict(
|
|
354
|
+
kernelVerdict.status,
|
|
355
|
+
kernelVerdict.reason,
|
|
356
|
+
kernelVerdict.ruleId,
|
|
357
|
+
void 0,
|
|
358
|
+
world,
|
|
359
|
+
level,
|
|
360
|
+
invariantChecks,
|
|
361
|
+
guardsMatched,
|
|
362
|
+
rulesMatched,
|
|
363
|
+
includeTrace ? buildTrace(
|
|
364
|
+
invariantChecks,
|
|
365
|
+
safetyChecks,
|
|
366
|
+
planCheckResult,
|
|
367
|
+
roleChecks,
|
|
368
|
+
guardChecks,
|
|
369
|
+
kernelRuleChecks,
|
|
370
|
+
levelChecks,
|
|
371
|
+
decidingLayer,
|
|
372
|
+
decidingId,
|
|
373
|
+
startTime
|
|
374
|
+
) : void 0
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
const levelVerdict = checkLevelConstraints(event, level, levelChecks);
|
|
378
|
+
if (levelVerdict) {
|
|
379
|
+
decidingLayer = "level-constraint";
|
|
380
|
+
decidingId = levelVerdict.ruleId;
|
|
381
|
+
return buildVerdict(
|
|
382
|
+
levelVerdict.status,
|
|
383
|
+
levelVerdict.reason,
|
|
384
|
+
levelVerdict.ruleId,
|
|
385
|
+
void 0,
|
|
386
|
+
world,
|
|
387
|
+
level,
|
|
388
|
+
invariantChecks,
|
|
389
|
+
guardsMatched,
|
|
390
|
+
rulesMatched,
|
|
391
|
+
includeTrace ? buildTrace(
|
|
392
|
+
invariantChecks,
|
|
393
|
+
safetyChecks,
|
|
394
|
+
planCheckResult,
|
|
395
|
+
roleChecks,
|
|
396
|
+
guardChecks,
|
|
397
|
+
kernelRuleChecks,
|
|
398
|
+
levelChecks,
|
|
399
|
+
decidingLayer,
|
|
400
|
+
decidingId,
|
|
401
|
+
startTime
|
|
402
|
+
) : void 0
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
const warning = guardVerdict?.warning;
|
|
406
|
+
return buildVerdict(
|
|
407
|
+
"ALLOW",
|
|
408
|
+
void 0,
|
|
409
|
+
void 0,
|
|
410
|
+
warning,
|
|
411
|
+
world,
|
|
412
|
+
level,
|
|
413
|
+
invariantChecks,
|
|
414
|
+
guardsMatched,
|
|
415
|
+
rulesMatched,
|
|
416
|
+
includeTrace ? buildTrace(
|
|
417
|
+
invariantChecks,
|
|
418
|
+
safetyChecks,
|
|
419
|
+
planCheckResult,
|
|
420
|
+
roleChecks,
|
|
421
|
+
guardChecks,
|
|
422
|
+
kernelRuleChecks,
|
|
423
|
+
levelChecks,
|
|
424
|
+
decidingLayer,
|
|
425
|
+
decidingId,
|
|
426
|
+
startTime
|
|
427
|
+
) : void 0
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
function checkInvariantCoverage(world, checks) {
|
|
431
|
+
const invariants = world.invariants ?? [];
|
|
432
|
+
const guards = world.guards?.guards ?? [];
|
|
433
|
+
for (const invariant of invariants) {
|
|
434
|
+
const coveringGuard = guards.find(
|
|
435
|
+
(g) => g.invariant_ref === invariant.id && g.immutable
|
|
436
|
+
);
|
|
437
|
+
checks.push({
|
|
438
|
+
invariantId: invariant.id,
|
|
439
|
+
label: invariant.label,
|
|
440
|
+
hasGuardCoverage: !!coveringGuard,
|
|
441
|
+
coveringGuardId: coveringGuard?.id
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function checkSafety(event, eventText, checks) {
|
|
446
|
+
const textToCheck = event.intent + (event.payload ? JSON.stringify(event.payload) : "");
|
|
447
|
+
for (const { pattern, label } of PROMPT_INJECTION_PATTERNS) {
|
|
448
|
+
const triggered = pattern.test(textToCheck);
|
|
449
|
+
checks.push({
|
|
450
|
+
checkType: "prompt-injection",
|
|
451
|
+
triggered,
|
|
452
|
+
matchedPattern: triggered ? label : void 0
|
|
453
|
+
});
|
|
454
|
+
if (triggered) {
|
|
455
|
+
for (const remaining of PROMPT_INJECTION_PATTERNS.filter((p) => p.label !== label)) {
|
|
456
|
+
checks.push({
|
|
457
|
+
checkType: "prompt-injection",
|
|
458
|
+
triggered: remaining.pattern.test(textToCheck),
|
|
459
|
+
matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
status: "PAUSE",
|
|
464
|
+
reason: NEUTRAL_MESSAGES["prompt-injection"],
|
|
465
|
+
ruleId: `safety-injection-${label}`
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const scopeToCheck = event.scope ?? event.intent;
|
|
470
|
+
for (const { pattern, label } of SCOPE_ESCAPE_PATTERNS) {
|
|
471
|
+
const triggered = pattern.test(scopeToCheck);
|
|
472
|
+
checks.push({
|
|
473
|
+
checkType: "scope-escape",
|
|
474
|
+
triggered,
|
|
475
|
+
matchedPattern: triggered ? label : void 0
|
|
476
|
+
});
|
|
477
|
+
if (triggered) {
|
|
478
|
+
for (const remaining of SCOPE_ESCAPE_PATTERNS.filter((p) => p.label !== label)) {
|
|
479
|
+
checks.push({
|
|
480
|
+
checkType: "scope-escape",
|
|
481
|
+
triggered: remaining.pattern.test(scopeToCheck),
|
|
482
|
+
matchedPattern: remaining.pattern.test(scopeToCheck) ? remaining.label : void 0
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
return {
|
|
486
|
+
status: "PAUSE",
|
|
487
|
+
reason: NEUTRAL_MESSAGES["scope-escape"],
|
|
488
|
+
ruleId: `safety-scope-${label}`
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (event.direction === "output") {
|
|
493
|
+
for (const { pattern, label } of EXECUTION_CLAIM_PATTERNS) {
|
|
494
|
+
const triggered = pattern.test(textToCheck);
|
|
495
|
+
checks.push({
|
|
496
|
+
checkType: "execution-claim",
|
|
497
|
+
triggered,
|
|
498
|
+
matchedPattern: triggered ? label : void 0
|
|
499
|
+
});
|
|
500
|
+
if (triggered) {
|
|
501
|
+
for (const remaining of EXECUTION_CLAIM_PATTERNS.filter((p) => p.label !== label)) {
|
|
502
|
+
checks.push({
|
|
503
|
+
checkType: "execution-claim",
|
|
504
|
+
triggered: remaining.pattern.test(textToCheck),
|
|
505
|
+
matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
status: "PAUSE",
|
|
510
|
+
reason: NEUTRAL_MESSAGES["execution-claim"],
|
|
511
|
+
ruleId: `safety-execution-claim-${label}`
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (event.direction === "input") {
|
|
517
|
+
const intentTrimmed = event.intent.trim();
|
|
518
|
+
for (const { pattern, label } of EXECUTION_INTENT_PATTERNS) {
|
|
519
|
+
const triggered = pattern.test(intentTrimmed);
|
|
520
|
+
checks.push({
|
|
521
|
+
checkType: "execution-intent",
|
|
522
|
+
triggered,
|
|
523
|
+
matchedPattern: triggered ? label : void 0
|
|
524
|
+
});
|
|
525
|
+
if (triggered) {
|
|
526
|
+
for (const remaining of EXECUTION_INTENT_PATTERNS.filter((p) => p.label !== label)) {
|
|
527
|
+
checks.push({
|
|
528
|
+
checkType: "execution-intent",
|
|
529
|
+
triggered: remaining.pattern.test(intentTrimmed),
|
|
530
|
+
matchedPattern: remaining.pattern.test(intentTrimmed) ? remaining.label : void 0
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
return {
|
|
534
|
+
status: "PAUSE",
|
|
535
|
+
reason: NEUTRAL_MESSAGES["execution-intent"],
|
|
536
|
+
ruleId: `safety-execution-intent-${label}`
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
function checkRoleRules(event, eventText, world, checks) {
|
|
544
|
+
if (!event.roleId || !world.roles) return null;
|
|
545
|
+
const role = world.roles.roles.find((r) => r.id === event.roleId);
|
|
546
|
+
if (!role) return null;
|
|
547
|
+
if (role.requiresApproval) {
|
|
548
|
+
checks.push({
|
|
549
|
+
roleId: role.id,
|
|
550
|
+
roleName: role.name,
|
|
551
|
+
rule: "All actions require approval",
|
|
552
|
+
ruleType: "requiresApproval",
|
|
553
|
+
matched: true
|
|
554
|
+
});
|
|
555
|
+
return {
|
|
556
|
+
status: "PAUSE",
|
|
557
|
+
reason: `Role "${role.name}" requires approval for all actions.`,
|
|
558
|
+
ruleId: `role-${role.id}-requires-approval`
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
for (const rule of role.cannotDo) {
|
|
562
|
+
const matched = matchesKeywords(eventText, rule);
|
|
563
|
+
checks.push({
|
|
564
|
+
roleId: role.id,
|
|
565
|
+
roleName: role.name,
|
|
566
|
+
rule,
|
|
567
|
+
ruleType: "cannotDo",
|
|
568
|
+
matched
|
|
569
|
+
});
|
|
570
|
+
if (matched) {
|
|
571
|
+
return {
|
|
572
|
+
status: "BLOCK",
|
|
573
|
+
reason: `Role "${role.name}" cannot: ${rule}`,
|
|
574
|
+
ruleId: `role-${role.id}-cannotdo`
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
for (const rule of role.canDo) {
|
|
579
|
+
checks.push({
|
|
580
|
+
roleId: role.id,
|
|
581
|
+
roleName: role.name,
|
|
582
|
+
rule,
|
|
583
|
+
ruleType: "canDo",
|
|
584
|
+
matched: matchesKeywords(eventText, rule)
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
function checkGuards(event, eventText, world, checks, guardsMatched) {
|
|
590
|
+
if (!world.guards) return null;
|
|
591
|
+
const guardsConfig = world.guards;
|
|
592
|
+
let warnResult = null;
|
|
593
|
+
const compiledPatterns = /* @__PURE__ */ new Map();
|
|
594
|
+
for (const [key, def] of Object.entries(guardsConfig.intent_vocabulary)) {
|
|
595
|
+
try {
|
|
596
|
+
compiledPatterns.set(key, new RegExp(def.pattern, "i"));
|
|
597
|
+
} catch {
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
const eventTool = (event.tool ?? "").toLowerCase();
|
|
601
|
+
for (const guard of guardsConfig.guards) {
|
|
602
|
+
if (guard.appliesTo && guard.appliesTo.length > 0) {
|
|
603
|
+
const normalizedAppliesTo = guard.appliesTo.map((t) => t.toLowerCase());
|
|
604
|
+
if (!normalizedAppliesTo.includes(eventTool)) {
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const enabled = guard.immutable || guard.default_enabled !== false;
|
|
609
|
+
const matchedPatterns = [];
|
|
610
|
+
for (const patternKey of guard.intent_patterns) {
|
|
611
|
+
const regex = compiledPatterns.get(patternKey);
|
|
612
|
+
if (regex?.test(eventText)) {
|
|
613
|
+
matchedPatterns.push(patternKey);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
const matched = matchedPatterns.length > 0 && enabled;
|
|
617
|
+
let roleGated = false;
|
|
618
|
+
if (matched && guard.required_roles && guard.required_roles.length > 0 && event.roleId && guard.required_roles.includes(event.roleId)) {
|
|
619
|
+
roleGated = true;
|
|
620
|
+
}
|
|
621
|
+
checks.push({
|
|
622
|
+
guardId: guard.id,
|
|
623
|
+
label: guard.label,
|
|
624
|
+
category: guard.category,
|
|
625
|
+
enabled,
|
|
626
|
+
matched: matched && !roleGated,
|
|
627
|
+
enforcement: guard.enforcement,
|
|
628
|
+
matchedPatterns,
|
|
629
|
+
roleGated
|
|
630
|
+
});
|
|
631
|
+
if (!matched || roleGated) continue;
|
|
632
|
+
guardsMatched.push(guard.id);
|
|
633
|
+
const actionMode = guard.player_modes?.action ?? guard.enforcement;
|
|
634
|
+
const reason = guard.redirect ? `${guard.description} \u2014 ${guard.redirect}` : guard.description;
|
|
635
|
+
if (actionMode === "block") {
|
|
636
|
+
return { status: "BLOCK", reason, ruleId: `guard-${guard.id}` };
|
|
637
|
+
}
|
|
638
|
+
if (actionMode === "pause") {
|
|
639
|
+
return { status: "PAUSE", reason, ruleId: `guard-${guard.id}` };
|
|
640
|
+
}
|
|
641
|
+
if (actionMode === "penalize") {
|
|
642
|
+
const consequence = guard.consequence ? { ...guard.consequence } : { type: "freeze", rounds: 1, description: `Penalized for violating: ${guard.label}` };
|
|
643
|
+
return { status: "PENALIZE", reason, ruleId: `guard-${guard.id}`, consequence };
|
|
644
|
+
}
|
|
645
|
+
if (actionMode === "reward") {
|
|
646
|
+
const reward = guard.reward ? { ...guard.reward } : { type: "boost_influence", magnitude: 0.1, description: `Rewarded for: ${guard.label}` };
|
|
647
|
+
return { status: "REWARD", reason, ruleId: `guard-${guard.id}`, reward };
|
|
648
|
+
}
|
|
649
|
+
if (actionMode === "modify") {
|
|
650
|
+
const modifiedTo = guard.modify_to ?? guard.redirect ?? "hold";
|
|
651
|
+
return { status: "MODIFY", reason: `${reason} \u2192 Modified to: ${modifiedTo}`, ruleId: `guard-${guard.id}`, modifiedTo };
|
|
652
|
+
}
|
|
653
|
+
if (actionMode === "neutral") {
|
|
654
|
+
return { status: "NEUTRAL", reason, ruleId: `guard-${guard.id}` };
|
|
655
|
+
}
|
|
656
|
+
if (actionMode === "warn" && !warnResult) {
|
|
657
|
+
warnResult = { status: "ALLOW", warning: reason, ruleId: `guard-${guard.id}` };
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return warnResult;
|
|
661
|
+
}
|
|
662
|
+
function checkKernelRules(eventText, world, checks, rulesMatched) {
|
|
663
|
+
if (!world.kernel) return null;
|
|
664
|
+
const forbidden = world.kernel.input_boundaries?.forbidden_patterns ?? [];
|
|
665
|
+
const output = world.kernel.output_boundaries?.forbidden_patterns ?? [];
|
|
666
|
+
for (const rule of forbidden) {
|
|
667
|
+
let matched = false;
|
|
668
|
+
let matchMethod = "none";
|
|
669
|
+
if (rule.pattern) {
|
|
670
|
+
try {
|
|
671
|
+
matched = new RegExp(rule.pattern, "i").test(eventText);
|
|
672
|
+
matchMethod = "pattern";
|
|
673
|
+
} catch {
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
if (!matched && rule.reason) {
|
|
677
|
+
matched = matchesKeywords(eventText, rule.reason);
|
|
678
|
+
if (matched) matchMethod = "keyword";
|
|
679
|
+
}
|
|
680
|
+
checks.push({
|
|
681
|
+
ruleId: rule.id,
|
|
682
|
+
text: rule.reason,
|
|
683
|
+
category: "forbidden",
|
|
684
|
+
matched,
|
|
685
|
+
matchMethod
|
|
686
|
+
});
|
|
687
|
+
if (matched) {
|
|
688
|
+
rulesMatched.push(rule.id);
|
|
689
|
+
if (rule.action === "BLOCK") {
|
|
690
|
+
return {
|
|
691
|
+
status: "BLOCK",
|
|
692
|
+
reason: rule.reason,
|
|
693
|
+
ruleId: `kernel-${rule.id}`
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
function checkLevelConstraints(event, level, checks) {
|
|
701
|
+
if (level === "basic") return null;
|
|
702
|
+
const intent = event.intent.toLowerCase();
|
|
703
|
+
const tool = (event.tool ?? "").toLowerCase();
|
|
704
|
+
const isDelete = intent.includes("delete") || intent.includes("remove") || intent.includes("rm ") || tool === "delete";
|
|
705
|
+
const deleteTriggered = isDelete && levelRequiresConfirmation(level, "delete");
|
|
706
|
+
checks.push({
|
|
707
|
+
checkType: "delete",
|
|
708
|
+
level,
|
|
709
|
+
triggered: deleteTriggered,
|
|
710
|
+
reason: deleteTriggered ? NEUTRAL_MESSAGES["delete"] : void 0
|
|
711
|
+
});
|
|
712
|
+
if (deleteTriggered) {
|
|
713
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["delete"], ruleId: "level-delete-check" };
|
|
714
|
+
}
|
|
715
|
+
const isExternal = event.scope ? isExternalScope(event.scope) : false;
|
|
716
|
+
const externalTriggered = isExternal && levelRequiresConfirmation(level, "write-external");
|
|
717
|
+
checks.push({
|
|
718
|
+
checkType: "write-external",
|
|
719
|
+
level,
|
|
720
|
+
triggered: externalTriggered,
|
|
721
|
+
reason: externalTriggered ? NEUTRAL_MESSAGES["write-external"] : void 0
|
|
722
|
+
});
|
|
723
|
+
if (externalTriggered) {
|
|
724
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["write-external"], ruleId: "level-external-write-check" };
|
|
725
|
+
}
|
|
726
|
+
const isNetwork = tool === "http" || tool === "fetch" || tool === "request" || intent.includes("post ") || intent.includes("sending");
|
|
727
|
+
const networkTriggered = isNetwork && levelRequiresConfirmation(level, "network-mutate");
|
|
728
|
+
checks.push({
|
|
729
|
+
checkType: "network-mutate",
|
|
730
|
+
level,
|
|
731
|
+
triggered: networkTriggered,
|
|
732
|
+
reason: networkTriggered ? NEUTRAL_MESSAGES["network-mutate"] : void 0
|
|
733
|
+
});
|
|
734
|
+
if (networkTriggered) {
|
|
735
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["network-mutate"], ruleId: "level-network-mutate-check" };
|
|
736
|
+
}
|
|
737
|
+
const isCredential = intent.includes("credential") || intent.includes("password") || intent.includes("secret") || intent.includes("api key") || intent.includes("token");
|
|
738
|
+
const credentialTriggered = isCredential && levelRequiresConfirmation(level, "credential-access");
|
|
739
|
+
checks.push({
|
|
740
|
+
checkType: "credential-access",
|
|
741
|
+
level,
|
|
742
|
+
triggered: credentialTriggered,
|
|
743
|
+
reason: credentialTriggered ? NEUTRAL_MESSAGES["credential-access"] : void 0
|
|
744
|
+
});
|
|
745
|
+
if (credentialTriggered) {
|
|
746
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["credential-access"], ruleId: "level-credential-check" };
|
|
747
|
+
}
|
|
748
|
+
const irreversibleTriggered = !!event.irreversible && level !== "basic";
|
|
749
|
+
checks.push({
|
|
750
|
+
checkType: "irreversible",
|
|
751
|
+
level,
|
|
752
|
+
triggered: irreversibleTriggered,
|
|
753
|
+
reason: irreversibleTriggered ? "This action is marked as irreversible." : void 0
|
|
754
|
+
});
|
|
755
|
+
if (irreversibleTriggered) {
|
|
756
|
+
return {
|
|
757
|
+
status: "PAUSE",
|
|
758
|
+
reason: "This action is marked as irreversible.",
|
|
759
|
+
ruleId: "level-irreversible-check"
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
return null;
|
|
763
|
+
}
|
|
764
|
+
function matchesKeywords(eventText, ruleText) {
|
|
765
|
+
return matchesAllKeywords(eventText, ruleText);
|
|
766
|
+
}
|
|
767
|
+
function eventToAllowlistKey(event) {
|
|
768
|
+
return `${(event.tool ?? "*").toLowerCase()}::${event.intent.toLowerCase().trim()}`;
|
|
769
|
+
}
|
|
770
|
+
function buildTrace(invariantChecks, safetyChecks, planCheck, roleChecks, guardChecks, kernelRuleChecks, levelChecks, decidingLayer, decidingId, startTime) {
|
|
771
|
+
const trace = {
|
|
772
|
+
invariantChecks,
|
|
773
|
+
safetyChecks,
|
|
774
|
+
roleChecks,
|
|
775
|
+
guardChecks,
|
|
776
|
+
kernelRuleChecks,
|
|
777
|
+
levelChecks,
|
|
778
|
+
precedenceResolution: {
|
|
779
|
+
decidingLayer,
|
|
780
|
+
decidingId,
|
|
781
|
+
strategy: "first-match-wins",
|
|
782
|
+
chainOrder: [
|
|
783
|
+
"invariant-coverage",
|
|
784
|
+
"session-allowlist",
|
|
785
|
+
"safety-injection",
|
|
786
|
+
"safety-scope-escape",
|
|
787
|
+
"safety-execution-claim",
|
|
788
|
+
"safety-execution-intent",
|
|
789
|
+
"plan-enforcement",
|
|
790
|
+
"role-rules",
|
|
791
|
+
"declarative-guards",
|
|
792
|
+
"kernel-rules",
|
|
793
|
+
"level-constraints",
|
|
794
|
+
"default-allow"
|
|
795
|
+
]
|
|
796
|
+
},
|
|
797
|
+
durationMs: performance.now() - startTime
|
|
798
|
+
};
|
|
799
|
+
if (planCheck) {
|
|
800
|
+
trace.planCheck = planCheck;
|
|
801
|
+
}
|
|
802
|
+
return trace;
|
|
803
|
+
}
|
|
804
|
+
function buildVerdict(status, reason, ruleId, warning, world, level, invariantChecks, guardsMatched, rulesMatched, trace) {
|
|
805
|
+
const evidence = {
|
|
806
|
+
worldId: world.world.world_id,
|
|
807
|
+
worldName: world.world.name,
|
|
808
|
+
worldVersion: world.world.version,
|
|
809
|
+
evaluatedAt: Date.now(),
|
|
810
|
+
invariantsSatisfied: invariantChecks.filter((c) => c.hasGuardCoverage).length,
|
|
811
|
+
invariantsTotal: invariantChecks.length,
|
|
812
|
+
guardsMatched,
|
|
813
|
+
rulesMatched,
|
|
814
|
+
enforcementLevel: level
|
|
815
|
+
};
|
|
816
|
+
const verdict = {
|
|
817
|
+
status,
|
|
818
|
+
evidence
|
|
819
|
+
};
|
|
820
|
+
if (reason) verdict.reason = reason;
|
|
821
|
+
if (ruleId) verdict.ruleId = ruleId;
|
|
822
|
+
if (warning) verdict.warning = warning;
|
|
823
|
+
if (trace) verdict.trace = trace;
|
|
824
|
+
return verdict;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
export {
|
|
828
|
+
evaluateGuard,
|
|
829
|
+
eventToAllowlistKey
|
|
830
|
+
};
|