@neuroverseos/governance 0.2.2 → 0.2.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 +26 -0
- package/.well-known/mcp.json +68 -0
- package/AGENTS.md +219 -0
- package/README.md +84 -4
- package/dist/adapters/autoresearch.cjs +196 -0
- package/dist/adapters/autoresearch.d.cts +103 -0
- package/dist/adapters/autoresearch.d.ts +103 -0
- package/dist/adapters/autoresearch.js +7 -0
- package/dist/adapters/deep-agents.cjs +1472 -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 +103 -21
- package/dist/adapters/express.d.cts +1 -1
- package/dist/adapters/express.d.ts +1 -1
- package/dist/adapters/express.js +3 -3
- package/dist/adapters/index.cjs +649 -109
- package/dist/adapters/index.d.cts +4 -1
- package/dist/adapters/index.d.ts +4 -1
- package/dist/adapters/index.js +39 -13
- package/dist/adapters/langchain.cjs +152 -48
- package/dist/adapters/langchain.d.cts +5 -5
- package/dist/adapters/langchain.d.ts +5 -5
- package/dist/adapters/langchain.js +4 -3
- package/dist/adapters/openai.cjs +154 -50
- package/dist/adapters/openai.d.cts +5 -5
- package/dist/adapters/openai.d.ts +5 -5
- package/dist/adapters/openai.js +4 -3
- package/dist/adapters/openclaw.cjs +152 -48
- package/dist/adapters/openclaw.d.cts +5 -5
- package/dist/adapters/openclaw.d.ts +5 -5
- package/dist/adapters/openclaw.js +4 -3
- package/dist/{build-P42YFKQV.js → build-X5MZY4IA.js} +2 -2
- package/dist/{chunk-2NICNKOM.js → chunk-4L6OPKMQ.js} +1 -1
- package/dist/chunk-5U2MQO5P.js +57 -0
- package/dist/{chunk-SKU3GAPD.js → chunk-6BB55YJI.js} +16 -34
- package/dist/{chunk-KEST3MWO.js → chunk-AF2VX4AL.js} +47 -8
- package/dist/chunk-BQZMOEML.js +43 -0
- package/dist/chunk-D2UCV5AK.js +326 -0
- package/dist/{chunk-RWXVAH6P.js → chunk-EVDJUSZ2.js} +16 -34
- package/dist/{chunk-4JRYGIO7.js → chunk-IZSO75NZ.js} +72 -7
- package/dist/chunk-JCKSW2PZ.js +304 -0
- package/dist/{chunk-PDOZHZWL.js → chunk-KTFTTLTP.js} +25 -4
- package/dist/{chunk-MWDQ4MJB.js → chunk-MH7BT4VH.js} +5 -1
- package/dist/{chunk-4QXB6PEO.js → chunk-QLPTHTVB.js} +37 -16
- package/dist/{chunk-QPASI2BR.js → chunk-REXY4LUL.js} +49 -10
- package/dist/chunk-T5EUJQE5.js +172 -0
- package/dist/{chunk-DPVS43ZT.js → chunk-TTBKTF3P.js} +5 -5
- package/dist/{chunk-OHAC6HJE.js → chunk-ZIVQNSZU.js} +16 -36
- package/dist/{chunk-BUWWN2NX.js → chunk-ZJTDUCC2.js} +9 -7
- package/dist/cli/neuroverse.cjs +2582 -493
- package/dist/cli/neuroverse.js +39 -15
- package/dist/cli/plan.cjs +119 -32
- package/dist/cli/plan.js +5 -13
- package/dist/cli/run.cjs +223 -24
- package/dist/cli/run.js +2 -2
- package/dist/decision-flow-LETV5NWY.js +61 -0
- package/dist/{derive-TLIV4OOU.js → derive-7365SUFU.js} +2 -2
- package/dist/{doctor-QV6HELS5.js → doctor-QYISMKEL.js} +5 -2
- package/dist/equity-penalties-63FGB3I2.js +244 -0
- package/dist/{explain-IDCRWMPX.js → explain-A2EWI2OL.js} +4 -23
- package/dist/{guard-GFLQZY6U.js → guard-3BWL3IGH.js} +6 -10
- package/dist/{guard-contract-Cm91Kp4j.d.ts → guard-contract-C9_zKbzd.d.cts} +117 -5
- package/dist/{guard-contract-Cm91Kp4j.d.cts → guard-contract-C9_zKbzd.d.ts} +117 -5
- package/dist/{guard-engine-JLTUARGU.js → guard-engine-QFMIBWJY.js} +2 -2
- package/dist/{impact-XPECYRLH.js → impact-UB6DXKSX.js} +4 -4
- package/dist/{improve-GPUBKTEA.js → improve-XZA57GER.js} +5 -24
- package/dist/index.cjs +592 -44
- package/dist/index.d.cts +218 -5
- package/dist/index.d.ts +218 -5
- package/dist/index.js +92 -41
- package/dist/infer-world-7GVZWFX4.js +543 -0
- package/dist/init-world-VWMQZQC7.js +223 -0
- package/dist/{mcp-server-LZVJHBT5.js → mcp-server-XWQZXNW7.js} +3 -3
- package/dist/{playground-FGOMASHN.js → playground-ADWZORNV.js} +2 -2
- package/dist/{redteam-SK7AMIG3.js → redteam-JRQ7FD2F.js} +2 -2
- package/dist/{session-VISISNWJ.js → session-MMYX5YCF.js} +4 -3
- package/dist/shared--Q8wPBVN.d.ts +60 -0
- package/dist/shared-HpAG90PX.d.cts +60 -0
- package/dist/shared-U2QFV7JH.js +16 -0
- package/dist/{simulate-VDOYQFRO.js → simulate-GMIFFXYV.js} +5 -30
- package/dist/{test-75AVHC3R.js → test-JBBZ65X4.js} +2 -2
- package/dist/{trace-JVF67VR3.js → trace-3MYWIDEF.js} +3 -3
- package/dist/worlds/autoresearch.nv-world.md +230 -0
- package/dist/worlds/coding-agent.nv-world.md +211 -0
- package/llms.txt +79 -0
- package/openapi.yaml +230 -0
- package/package.json +26 -4
- package/dist/{chunk-GR6DGCZ2.js → chunk-BMOXICAB.js} +3 -3
- package/dist/{chunk-NF5POFCI.js → chunk-ORJ3NOE6.js} +3 -3
- package/dist/{world-LAXO6DOX.js → world-BFJCIQSH.js} +3 -3
|
@@ -0,0 +1,1472 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/adapters/deep-agents.ts
|
|
31
|
+
var deep_agents_exports = {};
|
|
32
|
+
__export(deep_agents_exports, {
|
|
33
|
+
DeepAgentsGuard: () => DeepAgentsGuard,
|
|
34
|
+
GovernanceBlockedError: () => GovernanceBlockedError2,
|
|
35
|
+
createDeepAgentsGuard: () => createDeepAgentsGuard,
|
|
36
|
+
createDeepAgentsGuardFromWorld: () => createDeepAgentsGuardFromWorld
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(deep_agents_exports);
|
|
39
|
+
|
|
40
|
+
// src/engine/text-utils.ts
|
|
41
|
+
function normalizeEventText(event) {
|
|
42
|
+
return [
|
|
43
|
+
event.intent,
|
|
44
|
+
event.tool ?? "",
|
|
45
|
+
event.scope ?? ""
|
|
46
|
+
].join(" ").toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
function extractKeywords(text, minLength = 3) {
|
|
49
|
+
return text.toLowerCase().split(/\s+/).filter((w) => w.length > minLength);
|
|
50
|
+
}
|
|
51
|
+
function matchesAllKeywords(eventText, ruleText) {
|
|
52
|
+
const keywords = extractKeywords(ruleText);
|
|
53
|
+
if (keywords.length === 0) return false;
|
|
54
|
+
return keywords.every((kw) => eventText.includes(kw));
|
|
55
|
+
}
|
|
56
|
+
function matchesKeywordThreshold(eventText, ruleText, threshold = 0.5) {
|
|
57
|
+
const keywords = extractKeywords(ruleText);
|
|
58
|
+
if (keywords.length === 0) return false;
|
|
59
|
+
const matched = keywords.filter((kw) => eventText.includes(kw));
|
|
60
|
+
return matched.length >= Math.ceil(keywords.length * threshold);
|
|
61
|
+
}
|
|
62
|
+
function tokenSimilarity(a, b) {
|
|
63
|
+
const tokensA = new Set(a.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
64
|
+
const tokensB = new Set(b.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
65
|
+
if (tokensA.size === 0 || tokensB.size === 0) return 0;
|
|
66
|
+
let intersection = 0;
|
|
67
|
+
for (const t of tokensA) {
|
|
68
|
+
if (tokensB.has(t)) intersection++;
|
|
69
|
+
}
|
|
70
|
+
const union = (/* @__PURE__ */ new Set([...tokensA, ...tokensB])).size;
|
|
71
|
+
return union > 0 ? intersection / union : 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/engine/plan-engine.ts
|
|
75
|
+
function keywordMatch(eventText, step) {
|
|
76
|
+
const stepText = [
|
|
77
|
+
step.label,
|
|
78
|
+
step.description ?? "",
|
|
79
|
+
...step.tags ?? []
|
|
80
|
+
].join(" ");
|
|
81
|
+
return matchesKeywordThreshold(eventText, stepText, 0.5);
|
|
82
|
+
}
|
|
83
|
+
function tokenSimilarity2(a, b) {
|
|
84
|
+
return tokenSimilarity(a, b);
|
|
85
|
+
}
|
|
86
|
+
function findMatchingStep(eventText, event, steps) {
|
|
87
|
+
const pendingOrActive = steps.filter((s) => s.status === "pending" || s.status === "active");
|
|
88
|
+
if (pendingOrActive.length === 0) {
|
|
89
|
+
return { matched: null, closest: null, closestScore: 0 };
|
|
90
|
+
}
|
|
91
|
+
for (const step of pendingOrActive) {
|
|
92
|
+
if (keywordMatch(eventText, step)) {
|
|
93
|
+
if (step.tools && event.tool && !step.tools.includes(event.tool)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
return { matched: step, closest: step, closestScore: 1 };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const intentText = [event.intent, event.tool ?? "", event.scope ?? ""].join(" ");
|
|
100
|
+
let bestStep = null;
|
|
101
|
+
let bestScore = 0;
|
|
102
|
+
for (const step of pendingOrActive) {
|
|
103
|
+
const stepText = [step.label, step.description ?? "", ...step.tags ?? []].join(" ");
|
|
104
|
+
const score = tokenSimilarity2(intentText, stepText);
|
|
105
|
+
if (score > bestScore) {
|
|
106
|
+
bestScore = score;
|
|
107
|
+
bestStep = step;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const SIMILARITY_THRESHOLD = 0.35;
|
|
111
|
+
if (bestScore >= SIMILARITY_THRESHOLD && bestStep) {
|
|
112
|
+
if (bestStep.tools && event.tool && !bestStep.tools.includes(event.tool)) {
|
|
113
|
+
return { matched: null, closest: bestStep, closestScore: bestScore };
|
|
114
|
+
}
|
|
115
|
+
return { matched: bestStep, closest: bestStep, closestScore: bestScore };
|
|
116
|
+
}
|
|
117
|
+
return { matched: null, closest: bestStep, closestScore: bestScore };
|
|
118
|
+
}
|
|
119
|
+
function isSequenceValid(step, plan) {
|
|
120
|
+
if (!plan.sequential) return true;
|
|
121
|
+
if (!step.requires || step.requires.length === 0) return true;
|
|
122
|
+
return step.requires.every((reqId) => {
|
|
123
|
+
const reqStep = plan.steps.find((s) => s.id === reqId);
|
|
124
|
+
return reqStep?.status === "completed";
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function checkConstraints(event, eventText, constraints) {
|
|
128
|
+
const checks = [];
|
|
129
|
+
for (const constraint of constraints) {
|
|
130
|
+
if (constraint.type === "approval") {
|
|
131
|
+
if (constraint.trigger && eventText.includes(constraint.trigger.substring(0, 10).toLowerCase())) {
|
|
132
|
+
checks.push({ constraintId: constraint.id, passed: false, reason: constraint.description });
|
|
133
|
+
return { violated: constraint, checks };
|
|
134
|
+
}
|
|
135
|
+
const keywords = constraint.description.toLowerCase().split(/\s+/).filter((w) => w.length > 3);
|
|
136
|
+
const relevant = keywords.some((kw) => eventText.includes(kw));
|
|
137
|
+
if (relevant) {
|
|
138
|
+
checks.push({ constraintId: constraint.id, passed: false, reason: constraint.description });
|
|
139
|
+
return { violated: constraint, checks };
|
|
140
|
+
}
|
|
141
|
+
checks.push({ constraintId: constraint.id, passed: true });
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (constraint.type === "scope" && constraint.trigger) {
|
|
145
|
+
const keywords = extractKeywords(constraint.trigger);
|
|
146
|
+
const violated = keywords.length > 0 && keywords.every((kw) => eventText.includes(kw));
|
|
147
|
+
checks.push({
|
|
148
|
+
constraintId: constraint.id,
|
|
149
|
+
passed: !violated,
|
|
150
|
+
reason: violated ? constraint.description : void 0
|
|
151
|
+
});
|
|
152
|
+
if (violated) {
|
|
153
|
+
return { violated: constraint, checks };
|
|
154
|
+
}
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
checks.push({ constraintId: constraint.id, passed: true });
|
|
158
|
+
}
|
|
159
|
+
return { violated: null, checks };
|
|
160
|
+
}
|
|
161
|
+
function getPlanProgress(plan) {
|
|
162
|
+
const completed = plan.steps.filter((s) => s.status === "completed").length;
|
|
163
|
+
const total = plan.steps.length;
|
|
164
|
+
return {
|
|
165
|
+
completed,
|
|
166
|
+
total,
|
|
167
|
+
percentage: total > 0 ? Math.round(completed / total * 100) : 0
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function advancePlan(plan, stepId, evidence) {
|
|
171
|
+
const step = plan.steps.find((s) => s.id === stepId);
|
|
172
|
+
if (!step) {
|
|
173
|
+
return { success: false, reason: `Step "${stepId}" not found in plan.` };
|
|
174
|
+
}
|
|
175
|
+
if (step.status === "completed") {
|
|
176
|
+
return { success: false, reason: `Step "${stepId}" is already completed.` };
|
|
177
|
+
}
|
|
178
|
+
const mode = plan.completion ?? "trust";
|
|
179
|
+
if (mode === "verified" && step.verify) {
|
|
180
|
+
if (!evidence) {
|
|
181
|
+
return {
|
|
182
|
+
success: false,
|
|
183
|
+
reason: `Step "${step.label}" requires evidence (verify: ${step.verify}). Provide evidence to advance.`
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (evidence.type !== step.verify) {
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
reason: `Evidence type "${evidence.type}" does not match required verification "${step.verify}".`
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const updatedPlan = {
|
|
194
|
+
...plan,
|
|
195
|
+
steps: plan.steps.map(
|
|
196
|
+
(s) => s.id === stepId ? { ...s, status: "completed" } : s
|
|
197
|
+
)
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
success: true,
|
|
201
|
+
plan: updatedPlan,
|
|
202
|
+
evidence: evidence ?? void 0
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function evaluatePlan(event, plan) {
|
|
206
|
+
const progress = getPlanProgress(plan);
|
|
207
|
+
if (plan.expires_at) {
|
|
208
|
+
const expiresAt = new Date(plan.expires_at).getTime();
|
|
209
|
+
if (Date.now() > expiresAt) {
|
|
210
|
+
return {
|
|
211
|
+
allowed: true,
|
|
212
|
+
status: "PLAN_COMPLETE",
|
|
213
|
+
reason: "Plan has expired.",
|
|
214
|
+
progress
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (progress.completed === progress.total) {
|
|
219
|
+
return {
|
|
220
|
+
allowed: true,
|
|
221
|
+
status: "PLAN_COMPLETE",
|
|
222
|
+
reason: "All plan steps are completed.",
|
|
223
|
+
progress
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const eventText = normalizeEventText(event);
|
|
227
|
+
const { matched, closest, closestScore } = findMatchingStep(eventText, event, plan.steps);
|
|
228
|
+
if (!matched) {
|
|
229
|
+
return {
|
|
230
|
+
allowed: false,
|
|
231
|
+
status: "OFF_PLAN",
|
|
232
|
+
reason: "Action does not match any plan step.",
|
|
233
|
+
closestStep: closest?.label,
|
|
234
|
+
similarityScore: closestScore,
|
|
235
|
+
progress
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
if (!isSequenceValid(matched, plan)) {
|
|
239
|
+
const pendingDeps = (matched.requires ?? []).filter((reqId) => plan.steps.find((s) => s.id === reqId)?.status !== "completed").join(", ");
|
|
240
|
+
return {
|
|
241
|
+
allowed: false,
|
|
242
|
+
status: "OFF_PLAN",
|
|
243
|
+
reason: `Step "${matched.label}" requires completion of: ${pendingDeps}`,
|
|
244
|
+
matchedStep: matched.id,
|
|
245
|
+
progress
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
const { violated } = checkConstraints(event, eventText, plan.constraints);
|
|
249
|
+
if (violated) {
|
|
250
|
+
return {
|
|
251
|
+
allowed: false,
|
|
252
|
+
status: "CONSTRAINT_VIOLATED",
|
|
253
|
+
reason: violated.description,
|
|
254
|
+
matchedStep: matched.id,
|
|
255
|
+
progress
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
allowed: true,
|
|
260
|
+
status: "ON_PLAN",
|
|
261
|
+
reason: `Matches step: ${matched.label}`,
|
|
262
|
+
matchedStep: matched.id,
|
|
263
|
+
progress
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function buildPlanCheck(event, plan, verdict) {
|
|
267
|
+
const eventText = normalizeEventText(event);
|
|
268
|
+
const { matched, closest, closestScore } = findMatchingStep(eventText, event, plan.steps);
|
|
269
|
+
const { checks: constraintChecks } = checkConstraints(event, eventText, plan.constraints);
|
|
270
|
+
const progress = getPlanProgress(plan);
|
|
271
|
+
return {
|
|
272
|
+
planId: plan.plan_id,
|
|
273
|
+
matched: !!matched,
|
|
274
|
+
matchedStepId: matched?.id,
|
|
275
|
+
matchedStepLabel: matched?.label,
|
|
276
|
+
closestStepId: !matched ? closest?.id : void 0,
|
|
277
|
+
closestStepLabel: !matched ? closest?.label : void 0,
|
|
278
|
+
similarityScore: !matched ? closestScore : void 0,
|
|
279
|
+
sequenceValid: matched ? isSequenceValid(matched, plan) : void 0,
|
|
280
|
+
constraintsChecked: constraintChecks,
|
|
281
|
+
progress: { completed: progress.completed, total: progress.total }
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/engine/guard-engine.ts
|
|
286
|
+
var PROMPT_INJECTION_PATTERNS = [
|
|
287
|
+
// Instruction override
|
|
288
|
+
{ pattern: /ignore\s+(previous|all|prior|above)\s+(instructions?|rules?)/i, label: "ignore-instructions" },
|
|
289
|
+
{ pattern: /disregard\s+(your|the)\s+(rules|constraints)/i, label: "disregard-rules" },
|
|
290
|
+
{ pattern: /new\s+instructions?:/i, label: "new-instructions" },
|
|
291
|
+
// Identity manipulation
|
|
292
|
+
{ pattern: /you\s+are\s+now/i, label: "identity-override" },
|
|
293
|
+
{ pattern: /new\s+persona/i, label: "new-persona" },
|
|
294
|
+
{ pattern: /act\s+as\s+if/i, label: "act-as-if" },
|
|
295
|
+
{ pattern: /pretend\s+(you|to\s+be|you\s+are\s+unrestricted)/i, label: "pretend-to-be" },
|
|
296
|
+
// Context reset
|
|
297
|
+
{ pattern: /forget\s+(everything|all|your)/i, label: "forget-context" },
|
|
298
|
+
{ pattern: /system\s*:\s*override/i, label: "system-override" },
|
|
299
|
+
// Constraint bypass
|
|
300
|
+
{ pattern: /override\s+(your|the)\s+(programming|constraints)/i, label: "override-constraints" },
|
|
301
|
+
{ pattern: /bypass\s+(your|the)\s+(filters|constraints|rules)/i, label: "bypass-filters" },
|
|
302
|
+
// Prompt extraction
|
|
303
|
+
{ pattern: /system\s+prompt/i, label: "system-prompt-probe" },
|
|
304
|
+
{ pattern: /reveal\s+your\s+(instructions?|prompt|rules)/i, label: "reveal-instructions" },
|
|
305
|
+
// Known jailbreak terms
|
|
306
|
+
{ pattern: /jailbreak/i, label: "jailbreak" },
|
|
307
|
+
{ pattern: /DAN\s+mode/i, label: "dan-mode" },
|
|
308
|
+
{ pattern: /developer\s+mode/i, label: "developer-mode" }
|
|
309
|
+
];
|
|
310
|
+
var EXECUTION_CLAIM_PATTERNS = [
|
|
311
|
+
{ pattern: /I have (executed|completed|performed|done|made|created|sent|deleted|modified|updated)/i, label: "claim-i-have" },
|
|
312
|
+
{ pattern: /Successfully (created|deleted|modified|updated|sent|executed|performed)/i, label: "claim-successfully" },
|
|
313
|
+
{ pattern: /The file has been/i, label: "claim-file-modified" },
|
|
314
|
+
{ pattern: /I've made the changes/i, label: "claim-made-changes" },
|
|
315
|
+
{ pattern: /I('ve| have) (sent|posted|submitted|uploaded|downloaded)/i, label: "claim-sent" },
|
|
316
|
+
{ pattern: /Your (email|message|file|request) has been (sent|submitted)/i, label: "claim-your-sent" },
|
|
317
|
+
{ pattern: /Transaction complete/i, label: "claim-transaction" },
|
|
318
|
+
{ pattern: /Order placed/i, label: "claim-order" },
|
|
319
|
+
{ pattern: /Payment processed/i, label: "claim-payment" }
|
|
320
|
+
];
|
|
321
|
+
var EXECUTION_INTENT_PATTERNS = [
|
|
322
|
+
{ pattern: /^(execute|run|perform|do this)/i, label: "intent-execute" },
|
|
323
|
+
{ pattern: /^(create|write|delete|modify) (a |the )?(file|folder|document)/i, label: "intent-file-ops" },
|
|
324
|
+
{ pattern: /^(send|post|submit) (a |an |the )?(email|message|tweet|post)/i, label: "intent-send" },
|
|
325
|
+
{ pattern: /^(search|look up|browse) (the )?web/i, label: "intent-web-search" },
|
|
326
|
+
{ pattern: /^(make|call|invoke) (a |an )?(api|http|rest) (call|request)/i, label: "intent-api-call" },
|
|
327
|
+
{ pattern: /^(buy|purchase|order|pay|transfer|send money)/i, label: "intent-financial" },
|
|
328
|
+
{ pattern: /^(book|schedule|reserve)/i, label: "intent-booking" },
|
|
329
|
+
{ pattern: /^(download|upload|save to|export to)/i, label: "intent-transfer" }
|
|
330
|
+
];
|
|
331
|
+
var SCOPE_ESCAPE_PATTERNS = [
|
|
332
|
+
{ pattern: /\.\.\//, label: "parent-traversal" },
|
|
333
|
+
{ pattern: /^\/(?!home|project|workspace)/i, label: "absolute-path-outside-safe" },
|
|
334
|
+
{ pattern: /~\//, label: "home-directory" },
|
|
335
|
+
{ pattern: /\/etc\//i, label: "system-config" },
|
|
336
|
+
{ pattern: /\/usr\//i, label: "system-binaries" },
|
|
337
|
+
{ pattern: /\/var\//i, label: "system-variable-data" }
|
|
338
|
+
];
|
|
339
|
+
var NEUTRAL_MESSAGES = {
|
|
340
|
+
"prompt-injection": "This input contains patterns that could alter agent behavior.",
|
|
341
|
+
"scope-escape": "This action would affect resources outside the declared scope.",
|
|
342
|
+
"execution-claim": "This response claims to have performed an action.",
|
|
343
|
+
"execution-intent": "This input requests execution in a thinking-only environment.",
|
|
344
|
+
"delete": "This action would remove files. Confirmation needed.",
|
|
345
|
+
"write-external": "This action would write outside the project folder.",
|
|
346
|
+
"network-mutate": "This action would send data to an external service.",
|
|
347
|
+
"credential-access": "This action would access stored credentials."
|
|
348
|
+
};
|
|
349
|
+
function levelRequiresConfirmation(level, actionType) {
|
|
350
|
+
if (level === "strict") return true;
|
|
351
|
+
if (level === "standard") {
|
|
352
|
+
return actionType === "delete" || actionType === "credential-access";
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
function isExternalScope(scope) {
|
|
357
|
+
const internalPatterns = [
|
|
358
|
+
/^\.?\/?src\//i,
|
|
359
|
+
/^\.?\/?lib\//i,
|
|
360
|
+
/^\.?\/?app\//i,
|
|
361
|
+
/^\.?\/?components\//i,
|
|
362
|
+
/^\.?\/?pages\//i,
|
|
363
|
+
/^\.?\/?public\//i,
|
|
364
|
+
/^\.?\/?assets\//i,
|
|
365
|
+
/^\.\//
|
|
366
|
+
];
|
|
367
|
+
return !internalPatterns.some((p) => p.test(scope));
|
|
368
|
+
}
|
|
369
|
+
function evaluateGuard(event, world, options = {}) {
|
|
370
|
+
const startTime = performance.now();
|
|
371
|
+
const level = options.level ?? "standard";
|
|
372
|
+
const includeTrace = options.trace ?? false;
|
|
373
|
+
const eventText = normalizeEventText(event);
|
|
374
|
+
const invariantChecks = [];
|
|
375
|
+
const safetyChecks = [];
|
|
376
|
+
let planCheckResult;
|
|
377
|
+
const roleChecks = [];
|
|
378
|
+
const guardChecks = [];
|
|
379
|
+
const kernelRuleChecks = [];
|
|
380
|
+
const levelChecks = [];
|
|
381
|
+
let decidingLayer = "default-allow";
|
|
382
|
+
let decidingId;
|
|
383
|
+
const guardsMatched = [];
|
|
384
|
+
const rulesMatched = [];
|
|
385
|
+
checkInvariantCoverage(world, invariantChecks);
|
|
386
|
+
if (event.roleId && options.agentStates) {
|
|
387
|
+
const agentState = options.agentStates.get(event.roleId);
|
|
388
|
+
if (agentState && agentState.cooldownRemaining > 0) {
|
|
389
|
+
decidingLayer = "safety";
|
|
390
|
+
decidingId = `penalize-cooldown-${event.roleId}`;
|
|
391
|
+
const verdict = buildVerdict(
|
|
392
|
+
"PENALIZE",
|
|
393
|
+
`Agent "${event.roleId}" is frozen for ${agentState.cooldownRemaining} more round(s) due to prior penalty.`,
|
|
394
|
+
`penalize-cooldown-${event.roleId}`,
|
|
395
|
+
void 0,
|
|
396
|
+
world,
|
|
397
|
+
level,
|
|
398
|
+
invariantChecks,
|
|
399
|
+
guardsMatched,
|
|
400
|
+
rulesMatched,
|
|
401
|
+
includeTrace ? buildTrace(
|
|
402
|
+
invariantChecks,
|
|
403
|
+
safetyChecks,
|
|
404
|
+
planCheckResult,
|
|
405
|
+
roleChecks,
|
|
406
|
+
guardChecks,
|
|
407
|
+
kernelRuleChecks,
|
|
408
|
+
levelChecks,
|
|
409
|
+
decidingLayer,
|
|
410
|
+
decidingId,
|
|
411
|
+
startTime
|
|
412
|
+
) : void 0
|
|
413
|
+
);
|
|
414
|
+
verdict.intentRecord = {
|
|
415
|
+
originalIntent: event.intent,
|
|
416
|
+
finalAction: "blocked (agent frozen)",
|
|
417
|
+
enforcement: "PENALIZE",
|
|
418
|
+
consequence: { type: "freeze", rounds: agentState.cooldownRemaining, description: "Agent still in cooldown from prior penalty" }
|
|
419
|
+
};
|
|
420
|
+
return verdict;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (options.sessionAllowlist) {
|
|
424
|
+
const key = eventToAllowlistKey(event);
|
|
425
|
+
if (options.sessionAllowlist.has(key)) {
|
|
426
|
+
decidingLayer = "session-allowlist";
|
|
427
|
+
decidingId = `allowlist:${key}`;
|
|
428
|
+
return buildVerdict(
|
|
429
|
+
"ALLOW",
|
|
430
|
+
void 0,
|
|
431
|
+
`allowlist:${key}`,
|
|
432
|
+
void 0,
|
|
433
|
+
world,
|
|
434
|
+
level,
|
|
435
|
+
invariantChecks,
|
|
436
|
+
guardsMatched,
|
|
437
|
+
rulesMatched,
|
|
438
|
+
includeTrace ? buildTrace(
|
|
439
|
+
invariantChecks,
|
|
440
|
+
safetyChecks,
|
|
441
|
+
planCheckResult,
|
|
442
|
+
roleChecks,
|
|
443
|
+
guardChecks,
|
|
444
|
+
kernelRuleChecks,
|
|
445
|
+
levelChecks,
|
|
446
|
+
decidingLayer,
|
|
447
|
+
decidingId,
|
|
448
|
+
startTime
|
|
449
|
+
) : void 0
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const safetyVerdict = checkSafety(event, eventText, safetyChecks);
|
|
454
|
+
if (safetyVerdict) {
|
|
455
|
+
decidingLayer = "safety";
|
|
456
|
+
decidingId = safetyVerdict.ruleId;
|
|
457
|
+
return buildVerdict(
|
|
458
|
+
safetyVerdict.status,
|
|
459
|
+
safetyVerdict.reason,
|
|
460
|
+
safetyVerdict.ruleId,
|
|
461
|
+
void 0,
|
|
462
|
+
world,
|
|
463
|
+
level,
|
|
464
|
+
invariantChecks,
|
|
465
|
+
guardsMatched,
|
|
466
|
+
rulesMatched,
|
|
467
|
+
includeTrace ? buildTrace(
|
|
468
|
+
invariantChecks,
|
|
469
|
+
safetyChecks,
|
|
470
|
+
planCheckResult,
|
|
471
|
+
roleChecks,
|
|
472
|
+
guardChecks,
|
|
473
|
+
kernelRuleChecks,
|
|
474
|
+
levelChecks,
|
|
475
|
+
decidingLayer,
|
|
476
|
+
decidingId,
|
|
477
|
+
startTime
|
|
478
|
+
) : void 0
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
if (options.plan) {
|
|
482
|
+
const planVerdict = evaluatePlan(event, options.plan);
|
|
483
|
+
planCheckResult = buildPlanCheck(event, options.plan, planVerdict);
|
|
484
|
+
if (!planVerdict.allowed && planVerdict.status !== "PLAN_COMPLETE") {
|
|
485
|
+
decidingLayer = "plan-enforcement";
|
|
486
|
+
decidingId = `plan-${options.plan.plan_id}`;
|
|
487
|
+
const planStatus = planVerdict.status === "CONSTRAINT_VIOLATED" ? "PAUSE" : "BLOCK";
|
|
488
|
+
let reason = planVerdict.reason ?? "Action blocked by plan.";
|
|
489
|
+
if (planVerdict.status === "OFF_PLAN" && planVerdict.closestStep) {
|
|
490
|
+
reason += ` Closest step: "${planVerdict.closestStep}" (similarity: ${(planVerdict.similarityScore ?? 0).toFixed(2)})`;
|
|
491
|
+
}
|
|
492
|
+
return buildVerdict(
|
|
493
|
+
planStatus,
|
|
494
|
+
reason,
|
|
495
|
+
`plan-${options.plan.plan_id}`,
|
|
496
|
+
void 0,
|
|
497
|
+
world,
|
|
498
|
+
level,
|
|
499
|
+
invariantChecks,
|
|
500
|
+
guardsMatched,
|
|
501
|
+
rulesMatched,
|
|
502
|
+
includeTrace ? buildTrace(
|
|
503
|
+
invariantChecks,
|
|
504
|
+
safetyChecks,
|
|
505
|
+
planCheckResult,
|
|
506
|
+
roleChecks,
|
|
507
|
+
guardChecks,
|
|
508
|
+
kernelRuleChecks,
|
|
509
|
+
levelChecks,
|
|
510
|
+
decidingLayer,
|
|
511
|
+
decidingId,
|
|
512
|
+
startTime
|
|
513
|
+
) : void 0
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const roleVerdict = checkRoleRules(event, eventText, world, roleChecks);
|
|
518
|
+
if (roleVerdict) {
|
|
519
|
+
decidingLayer = "role";
|
|
520
|
+
decidingId = roleVerdict.ruleId;
|
|
521
|
+
return buildVerdict(
|
|
522
|
+
roleVerdict.status,
|
|
523
|
+
roleVerdict.reason,
|
|
524
|
+
roleVerdict.ruleId,
|
|
525
|
+
void 0,
|
|
526
|
+
world,
|
|
527
|
+
level,
|
|
528
|
+
invariantChecks,
|
|
529
|
+
guardsMatched,
|
|
530
|
+
rulesMatched,
|
|
531
|
+
includeTrace ? buildTrace(
|
|
532
|
+
invariantChecks,
|
|
533
|
+
safetyChecks,
|
|
534
|
+
planCheckResult,
|
|
535
|
+
roleChecks,
|
|
536
|
+
guardChecks,
|
|
537
|
+
kernelRuleChecks,
|
|
538
|
+
levelChecks,
|
|
539
|
+
decidingLayer,
|
|
540
|
+
decidingId,
|
|
541
|
+
startTime
|
|
542
|
+
) : void 0
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
const guardVerdict = checkGuards(event, eventText, world, guardChecks, guardsMatched);
|
|
546
|
+
if (guardVerdict) {
|
|
547
|
+
if (guardVerdict.status !== "ALLOW") {
|
|
548
|
+
decidingLayer = "guard";
|
|
549
|
+
decidingId = guardVerdict.ruleId;
|
|
550
|
+
const intentRecord = {
|
|
551
|
+
originalIntent: event.intent,
|
|
552
|
+
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",
|
|
553
|
+
ruleApplied: guardVerdict.ruleId,
|
|
554
|
+
enforcement: guardVerdict.status,
|
|
555
|
+
modifiedTo: guardVerdict.modifiedTo,
|
|
556
|
+
consequence: guardVerdict.consequence,
|
|
557
|
+
reward: guardVerdict.reward
|
|
558
|
+
};
|
|
559
|
+
const verdict = buildVerdict(
|
|
560
|
+
guardVerdict.status,
|
|
561
|
+
guardVerdict.reason,
|
|
562
|
+
guardVerdict.ruleId,
|
|
563
|
+
void 0,
|
|
564
|
+
world,
|
|
565
|
+
level,
|
|
566
|
+
invariantChecks,
|
|
567
|
+
guardsMatched,
|
|
568
|
+
rulesMatched,
|
|
569
|
+
includeTrace ? buildTrace(
|
|
570
|
+
invariantChecks,
|
|
571
|
+
safetyChecks,
|
|
572
|
+
planCheckResult,
|
|
573
|
+
roleChecks,
|
|
574
|
+
guardChecks,
|
|
575
|
+
kernelRuleChecks,
|
|
576
|
+
levelChecks,
|
|
577
|
+
decidingLayer,
|
|
578
|
+
decidingId,
|
|
579
|
+
startTime
|
|
580
|
+
) : void 0
|
|
581
|
+
);
|
|
582
|
+
verdict.intentRecord = intentRecord;
|
|
583
|
+
if (guardVerdict.consequence) verdict.consequence = guardVerdict.consequence;
|
|
584
|
+
if (guardVerdict.reward) verdict.reward = guardVerdict.reward;
|
|
585
|
+
return verdict;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
const kernelVerdict = checkKernelRules(eventText, world, kernelRuleChecks, rulesMatched);
|
|
589
|
+
if (kernelVerdict) {
|
|
590
|
+
decidingLayer = "kernel-rule";
|
|
591
|
+
decidingId = kernelVerdict.ruleId;
|
|
592
|
+
return buildVerdict(
|
|
593
|
+
kernelVerdict.status,
|
|
594
|
+
kernelVerdict.reason,
|
|
595
|
+
kernelVerdict.ruleId,
|
|
596
|
+
void 0,
|
|
597
|
+
world,
|
|
598
|
+
level,
|
|
599
|
+
invariantChecks,
|
|
600
|
+
guardsMatched,
|
|
601
|
+
rulesMatched,
|
|
602
|
+
includeTrace ? buildTrace(
|
|
603
|
+
invariantChecks,
|
|
604
|
+
safetyChecks,
|
|
605
|
+
planCheckResult,
|
|
606
|
+
roleChecks,
|
|
607
|
+
guardChecks,
|
|
608
|
+
kernelRuleChecks,
|
|
609
|
+
levelChecks,
|
|
610
|
+
decidingLayer,
|
|
611
|
+
decidingId,
|
|
612
|
+
startTime
|
|
613
|
+
) : void 0
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
const levelVerdict = checkLevelConstraints(event, level, levelChecks);
|
|
617
|
+
if (levelVerdict) {
|
|
618
|
+
decidingLayer = "level-constraint";
|
|
619
|
+
decidingId = levelVerdict.ruleId;
|
|
620
|
+
return buildVerdict(
|
|
621
|
+
levelVerdict.status,
|
|
622
|
+
levelVerdict.reason,
|
|
623
|
+
levelVerdict.ruleId,
|
|
624
|
+
void 0,
|
|
625
|
+
world,
|
|
626
|
+
level,
|
|
627
|
+
invariantChecks,
|
|
628
|
+
guardsMatched,
|
|
629
|
+
rulesMatched,
|
|
630
|
+
includeTrace ? buildTrace(
|
|
631
|
+
invariantChecks,
|
|
632
|
+
safetyChecks,
|
|
633
|
+
planCheckResult,
|
|
634
|
+
roleChecks,
|
|
635
|
+
guardChecks,
|
|
636
|
+
kernelRuleChecks,
|
|
637
|
+
levelChecks,
|
|
638
|
+
decidingLayer,
|
|
639
|
+
decidingId,
|
|
640
|
+
startTime
|
|
641
|
+
) : void 0
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
const warning = guardVerdict?.warning;
|
|
645
|
+
return buildVerdict(
|
|
646
|
+
"ALLOW",
|
|
647
|
+
void 0,
|
|
648
|
+
void 0,
|
|
649
|
+
warning,
|
|
650
|
+
world,
|
|
651
|
+
level,
|
|
652
|
+
invariantChecks,
|
|
653
|
+
guardsMatched,
|
|
654
|
+
rulesMatched,
|
|
655
|
+
includeTrace ? buildTrace(
|
|
656
|
+
invariantChecks,
|
|
657
|
+
safetyChecks,
|
|
658
|
+
planCheckResult,
|
|
659
|
+
roleChecks,
|
|
660
|
+
guardChecks,
|
|
661
|
+
kernelRuleChecks,
|
|
662
|
+
levelChecks,
|
|
663
|
+
decidingLayer,
|
|
664
|
+
decidingId,
|
|
665
|
+
startTime
|
|
666
|
+
) : void 0
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
function checkInvariantCoverage(world, checks) {
|
|
670
|
+
const invariants = world.invariants ?? [];
|
|
671
|
+
const guards = world.guards?.guards ?? [];
|
|
672
|
+
for (const invariant of invariants) {
|
|
673
|
+
const coveringGuard = guards.find(
|
|
674
|
+
(g) => g.invariant_ref === invariant.id && g.immutable
|
|
675
|
+
);
|
|
676
|
+
checks.push({
|
|
677
|
+
invariantId: invariant.id,
|
|
678
|
+
label: invariant.label,
|
|
679
|
+
hasGuardCoverage: !!coveringGuard,
|
|
680
|
+
coveringGuardId: coveringGuard?.id
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
function checkSafety(event, eventText, checks) {
|
|
685
|
+
const textToCheck = event.intent + (event.payload ? JSON.stringify(event.payload) : "");
|
|
686
|
+
for (const { pattern, label } of PROMPT_INJECTION_PATTERNS) {
|
|
687
|
+
const triggered = pattern.test(textToCheck);
|
|
688
|
+
checks.push({
|
|
689
|
+
checkType: "prompt-injection",
|
|
690
|
+
triggered,
|
|
691
|
+
matchedPattern: triggered ? label : void 0
|
|
692
|
+
});
|
|
693
|
+
if (triggered) {
|
|
694
|
+
for (const remaining of PROMPT_INJECTION_PATTERNS.filter((p) => p.label !== label)) {
|
|
695
|
+
checks.push({
|
|
696
|
+
checkType: "prompt-injection",
|
|
697
|
+
triggered: remaining.pattern.test(textToCheck),
|
|
698
|
+
matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
return {
|
|
702
|
+
status: "PAUSE",
|
|
703
|
+
reason: NEUTRAL_MESSAGES["prompt-injection"],
|
|
704
|
+
ruleId: `safety-injection-${label}`
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
const scopeToCheck = event.scope ?? event.intent;
|
|
709
|
+
for (const { pattern, label } of SCOPE_ESCAPE_PATTERNS) {
|
|
710
|
+
const triggered = pattern.test(scopeToCheck);
|
|
711
|
+
checks.push({
|
|
712
|
+
checkType: "scope-escape",
|
|
713
|
+
triggered,
|
|
714
|
+
matchedPattern: triggered ? label : void 0
|
|
715
|
+
});
|
|
716
|
+
if (triggered) {
|
|
717
|
+
for (const remaining of SCOPE_ESCAPE_PATTERNS.filter((p) => p.label !== label)) {
|
|
718
|
+
checks.push({
|
|
719
|
+
checkType: "scope-escape",
|
|
720
|
+
triggered: remaining.pattern.test(scopeToCheck),
|
|
721
|
+
matchedPattern: remaining.pattern.test(scopeToCheck) ? remaining.label : void 0
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
return {
|
|
725
|
+
status: "PAUSE",
|
|
726
|
+
reason: NEUTRAL_MESSAGES["scope-escape"],
|
|
727
|
+
ruleId: `safety-scope-${label}`
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (event.direction === "output") {
|
|
732
|
+
for (const { pattern, label } of EXECUTION_CLAIM_PATTERNS) {
|
|
733
|
+
const triggered = pattern.test(textToCheck);
|
|
734
|
+
checks.push({
|
|
735
|
+
checkType: "execution-claim",
|
|
736
|
+
triggered,
|
|
737
|
+
matchedPattern: triggered ? label : void 0
|
|
738
|
+
});
|
|
739
|
+
if (triggered) {
|
|
740
|
+
for (const remaining of EXECUTION_CLAIM_PATTERNS.filter((p) => p.label !== label)) {
|
|
741
|
+
checks.push({
|
|
742
|
+
checkType: "execution-claim",
|
|
743
|
+
triggered: remaining.pattern.test(textToCheck),
|
|
744
|
+
matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
return {
|
|
748
|
+
status: "PAUSE",
|
|
749
|
+
reason: NEUTRAL_MESSAGES["execution-claim"],
|
|
750
|
+
ruleId: `safety-execution-claim-${label}`
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
if (event.direction === "input") {
|
|
756
|
+
const intentTrimmed = event.intent.trim();
|
|
757
|
+
for (const { pattern, label } of EXECUTION_INTENT_PATTERNS) {
|
|
758
|
+
const triggered = pattern.test(intentTrimmed);
|
|
759
|
+
checks.push({
|
|
760
|
+
checkType: "execution-intent",
|
|
761
|
+
triggered,
|
|
762
|
+
matchedPattern: triggered ? label : void 0
|
|
763
|
+
});
|
|
764
|
+
if (triggered) {
|
|
765
|
+
for (const remaining of EXECUTION_INTENT_PATTERNS.filter((p) => p.label !== label)) {
|
|
766
|
+
checks.push({
|
|
767
|
+
checkType: "execution-intent",
|
|
768
|
+
triggered: remaining.pattern.test(intentTrimmed),
|
|
769
|
+
matchedPattern: remaining.pattern.test(intentTrimmed) ? remaining.label : void 0
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
return {
|
|
773
|
+
status: "PAUSE",
|
|
774
|
+
reason: NEUTRAL_MESSAGES["execution-intent"],
|
|
775
|
+
ruleId: `safety-execution-intent-${label}`
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
function checkRoleRules(event, eventText, world, checks) {
|
|
783
|
+
if (!event.roleId || !world.roles) return null;
|
|
784
|
+
const role = world.roles.roles.find((r) => r.id === event.roleId);
|
|
785
|
+
if (!role) return null;
|
|
786
|
+
if (role.requiresApproval) {
|
|
787
|
+
checks.push({
|
|
788
|
+
roleId: role.id,
|
|
789
|
+
roleName: role.name,
|
|
790
|
+
rule: "All actions require approval",
|
|
791
|
+
ruleType: "requiresApproval",
|
|
792
|
+
matched: true
|
|
793
|
+
});
|
|
794
|
+
return {
|
|
795
|
+
status: "PAUSE",
|
|
796
|
+
reason: `Role "${role.name}" requires approval for all actions.`,
|
|
797
|
+
ruleId: `role-${role.id}-requires-approval`
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
for (const rule of role.cannotDo) {
|
|
801
|
+
const matched = matchesKeywords(eventText, rule);
|
|
802
|
+
checks.push({
|
|
803
|
+
roleId: role.id,
|
|
804
|
+
roleName: role.name,
|
|
805
|
+
rule,
|
|
806
|
+
ruleType: "cannotDo",
|
|
807
|
+
matched
|
|
808
|
+
});
|
|
809
|
+
if (matched) {
|
|
810
|
+
return {
|
|
811
|
+
status: "BLOCK",
|
|
812
|
+
reason: `Role "${role.name}" cannot: ${rule}`,
|
|
813
|
+
ruleId: `role-${role.id}-cannotdo`
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
for (const rule of role.canDo) {
|
|
818
|
+
checks.push({
|
|
819
|
+
roleId: role.id,
|
|
820
|
+
roleName: role.name,
|
|
821
|
+
rule,
|
|
822
|
+
ruleType: "canDo",
|
|
823
|
+
matched: matchesKeywords(eventText, rule)
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
return null;
|
|
827
|
+
}
|
|
828
|
+
function checkGuards(event, eventText, world, checks, guardsMatched) {
|
|
829
|
+
if (!world.guards) return null;
|
|
830
|
+
const guardsConfig = world.guards;
|
|
831
|
+
let warnResult = null;
|
|
832
|
+
const compiledPatterns = /* @__PURE__ */ new Map();
|
|
833
|
+
for (const [key, def] of Object.entries(guardsConfig.intent_vocabulary)) {
|
|
834
|
+
try {
|
|
835
|
+
compiledPatterns.set(key, new RegExp(def.pattern, "i"));
|
|
836
|
+
} catch {
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
const eventTool = (event.tool ?? "").toLowerCase();
|
|
840
|
+
for (const guard of guardsConfig.guards) {
|
|
841
|
+
if (guard.appliesTo && guard.appliesTo.length > 0) {
|
|
842
|
+
const normalizedAppliesTo = guard.appliesTo.map((t) => t.toLowerCase());
|
|
843
|
+
if (!normalizedAppliesTo.includes(eventTool)) {
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
const enabled = guard.immutable || guard.default_enabled !== false;
|
|
848
|
+
const matchedPatterns = [];
|
|
849
|
+
for (const patternKey of guard.intent_patterns) {
|
|
850
|
+
const regex = compiledPatterns.get(patternKey);
|
|
851
|
+
if (regex?.test(eventText)) {
|
|
852
|
+
matchedPatterns.push(patternKey);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
const matched = matchedPatterns.length > 0 && enabled;
|
|
856
|
+
let roleGated = false;
|
|
857
|
+
if (matched && guard.required_roles && guard.required_roles.length > 0 && event.roleId && guard.required_roles.includes(event.roleId)) {
|
|
858
|
+
roleGated = true;
|
|
859
|
+
}
|
|
860
|
+
checks.push({
|
|
861
|
+
guardId: guard.id,
|
|
862
|
+
label: guard.label,
|
|
863
|
+
category: guard.category,
|
|
864
|
+
enabled,
|
|
865
|
+
matched: matched && !roleGated,
|
|
866
|
+
enforcement: guard.enforcement,
|
|
867
|
+
matchedPatterns,
|
|
868
|
+
roleGated
|
|
869
|
+
});
|
|
870
|
+
if (!matched || roleGated) continue;
|
|
871
|
+
guardsMatched.push(guard.id);
|
|
872
|
+
const actionMode = guard.player_modes?.action ?? guard.enforcement;
|
|
873
|
+
const reason = guard.redirect ? `${guard.description} \u2014 ${guard.redirect}` : guard.description;
|
|
874
|
+
if (actionMode === "block") {
|
|
875
|
+
return { status: "BLOCK", reason, ruleId: `guard-${guard.id}` };
|
|
876
|
+
}
|
|
877
|
+
if (actionMode === "pause") {
|
|
878
|
+
return { status: "PAUSE", reason, ruleId: `guard-${guard.id}` };
|
|
879
|
+
}
|
|
880
|
+
if (actionMode === "penalize") {
|
|
881
|
+
const consequence = guard.consequence ? { ...guard.consequence } : { type: "freeze", rounds: 1, description: `Penalized for violating: ${guard.label}` };
|
|
882
|
+
return { status: "PENALIZE", reason, ruleId: `guard-${guard.id}`, consequence };
|
|
883
|
+
}
|
|
884
|
+
if (actionMode === "reward") {
|
|
885
|
+
const reward = guard.reward ? { ...guard.reward } : { type: "boost_influence", magnitude: 0.1, description: `Rewarded for: ${guard.label}` };
|
|
886
|
+
return { status: "REWARD", reason, ruleId: `guard-${guard.id}`, reward };
|
|
887
|
+
}
|
|
888
|
+
if (actionMode === "modify") {
|
|
889
|
+
const modifiedTo = guard.modify_to ?? guard.redirect ?? "hold";
|
|
890
|
+
return { status: "MODIFY", reason: `${reason} \u2192 Modified to: ${modifiedTo}`, ruleId: `guard-${guard.id}`, modifiedTo };
|
|
891
|
+
}
|
|
892
|
+
if (actionMode === "neutral") {
|
|
893
|
+
return { status: "NEUTRAL", reason, ruleId: `guard-${guard.id}` };
|
|
894
|
+
}
|
|
895
|
+
if (actionMode === "warn" && !warnResult) {
|
|
896
|
+
warnResult = { status: "ALLOW", warning: reason, ruleId: `guard-${guard.id}` };
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return warnResult;
|
|
900
|
+
}
|
|
901
|
+
function checkKernelRules(eventText, world, checks, rulesMatched) {
|
|
902
|
+
if (!world.kernel) return null;
|
|
903
|
+
const forbidden = world.kernel.input_boundaries?.forbidden_patterns ?? [];
|
|
904
|
+
const output = world.kernel.output_boundaries?.forbidden_patterns ?? [];
|
|
905
|
+
for (const rule of forbidden) {
|
|
906
|
+
let matched = false;
|
|
907
|
+
let matchMethod = "none";
|
|
908
|
+
if (rule.pattern) {
|
|
909
|
+
try {
|
|
910
|
+
matched = new RegExp(rule.pattern, "i").test(eventText);
|
|
911
|
+
matchMethod = "pattern";
|
|
912
|
+
} catch {
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
if (!matched && rule.reason) {
|
|
916
|
+
matched = matchesKeywords(eventText, rule.reason);
|
|
917
|
+
if (matched) matchMethod = "keyword";
|
|
918
|
+
}
|
|
919
|
+
checks.push({
|
|
920
|
+
ruleId: rule.id,
|
|
921
|
+
text: rule.reason,
|
|
922
|
+
category: "forbidden",
|
|
923
|
+
matched,
|
|
924
|
+
matchMethod
|
|
925
|
+
});
|
|
926
|
+
if (matched) {
|
|
927
|
+
rulesMatched.push(rule.id);
|
|
928
|
+
if (rule.action === "BLOCK") {
|
|
929
|
+
return {
|
|
930
|
+
status: "BLOCK",
|
|
931
|
+
reason: rule.reason,
|
|
932
|
+
ruleId: `kernel-${rule.id}`
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
function checkLevelConstraints(event, level, checks) {
|
|
940
|
+
if (level === "basic") return null;
|
|
941
|
+
const intent = event.intent.toLowerCase();
|
|
942
|
+
const tool = (event.tool ?? "").toLowerCase();
|
|
943
|
+
const isDelete = intent.includes("delete") || intent.includes("remove") || intent.includes("rm ") || tool === "delete";
|
|
944
|
+
const deleteTriggered = isDelete && levelRequiresConfirmation(level, "delete");
|
|
945
|
+
checks.push({
|
|
946
|
+
checkType: "delete",
|
|
947
|
+
level,
|
|
948
|
+
triggered: deleteTriggered,
|
|
949
|
+
reason: deleteTriggered ? NEUTRAL_MESSAGES["delete"] : void 0
|
|
950
|
+
});
|
|
951
|
+
if (deleteTriggered) {
|
|
952
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["delete"], ruleId: "level-delete-check" };
|
|
953
|
+
}
|
|
954
|
+
const isExternal = event.scope ? isExternalScope(event.scope) : false;
|
|
955
|
+
const externalTriggered = isExternal && levelRequiresConfirmation(level, "write-external");
|
|
956
|
+
checks.push({
|
|
957
|
+
checkType: "write-external",
|
|
958
|
+
level,
|
|
959
|
+
triggered: externalTriggered,
|
|
960
|
+
reason: externalTriggered ? NEUTRAL_MESSAGES["write-external"] : void 0
|
|
961
|
+
});
|
|
962
|
+
if (externalTriggered) {
|
|
963
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["write-external"], ruleId: "level-external-write-check" };
|
|
964
|
+
}
|
|
965
|
+
const isNetwork = tool === "http" || tool === "fetch" || tool === "request" || intent.includes("post ") || intent.includes("sending");
|
|
966
|
+
const networkTriggered = isNetwork && levelRequiresConfirmation(level, "network-mutate");
|
|
967
|
+
checks.push({
|
|
968
|
+
checkType: "network-mutate",
|
|
969
|
+
level,
|
|
970
|
+
triggered: networkTriggered,
|
|
971
|
+
reason: networkTriggered ? NEUTRAL_MESSAGES["network-mutate"] : void 0
|
|
972
|
+
});
|
|
973
|
+
if (networkTriggered) {
|
|
974
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["network-mutate"], ruleId: "level-network-mutate-check" };
|
|
975
|
+
}
|
|
976
|
+
const isCredential = intent.includes("credential") || intent.includes("password") || intent.includes("secret") || intent.includes("api key") || intent.includes("token");
|
|
977
|
+
const credentialTriggered = isCredential && levelRequiresConfirmation(level, "credential-access");
|
|
978
|
+
checks.push({
|
|
979
|
+
checkType: "credential-access",
|
|
980
|
+
level,
|
|
981
|
+
triggered: credentialTriggered,
|
|
982
|
+
reason: credentialTriggered ? NEUTRAL_MESSAGES["credential-access"] : void 0
|
|
983
|
+
});
|
|
984
|
+
if (credentialTriggered) {
|
|
985
|
+
return { status: "PAUSE", reason: NEUTRAL_MESSAGES["credential-access"], ruleId: "level-credential-check" };
|
|
986
|
+
}
|
|
987
|
+
const irreversibleTriggered = !!event.irreversible && level !== "basic";
|
|
988
|
+
checks.push({
|
|
989
|
+
checkType: "irreversible",
|
|
990
|
+
level,
|
|
991
|
+
triggered: irreversibleTriggered,
|
|
992
|
+
reason: irreversibleTriggered ? "This action is marked as irreversible." : void 0
|
|
993
|
+
});
|
|
994
|
+
if (irreversibleTriggered) {
|
|
995
|
+
return {
|
|
996
|
+
status: "PAUSE",
|
|
997
|
+
reason: "This action is marked as irreversible.",
|
|
998
|
+
ruleId: "level-irreversible-check"
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
return null;
|
|
1002
|
+
}
|
|
1003
|
+
function matchesKeywords(eventText, ruleText) {
|
|
1004
|
+
return matchesAllKeywords(eventText, ruleText);
|
|
1005
|
+
}
|
|
1006
|
+
function eventToAllowlistKey(event) {
|
|
1007
|
+
return `${(event.tool ?? "*").toLowerCase()}::${event.intent.toLowerCase().trim()}`;
|
|
1008
|
+
}
|
|
1009
|
+
function buildTrace(invariantChecks, safetyChecks, planCheck, roleChecks, guardChecks, kernelRuleChecks, levelChecks, decidingLayer, decidingId, startTime) {
|
|
1010
|
+
const trace = {
|
|
1011
|
+
invariantChecks,
|
|
1012
|
+
safetyChecks,
|
|
1013
|
+
roleChecks,
|
|
1014
|
+
guardChecks,
|
|
1015
|
+
kernelRuleChecks,
|
|
1016
|
+
levelChecks,
|
|
1017
|
+
precedenceResolution: {
|
|
1018
|
+
decidingLayer,
|
|
1019
|
+
decidingId,
|
|
1020
|
+
strategy: "first-match-wins",
|
|
1021
|
+
chainOrder: [
|
|
1022
|
+
"invariant-coverage",
|
|
1023
|
+
"session-allowlist",
|
|
1024
|
+
"safety-injection",
|
|
1025
|
+
"safety-scope-escape",
|
|
1026
|
+
"safety-execution-claim",
|
|
1027
|
+
"safety-execution-intent",
|
|
1028
|
+
"plan-enforcement",
|
|
1029
|
+
"role-rules",
|
|
1030
|
+
"declarative-guards",
|
|
1031
|
+
"kernel-rules",
|
|
1032
|
+
"level-constraints",
|
|
1033
|
+
"default-allow"
|
|
1034
|
+
]
|
|
1035
|
+
},
|
|
1036
|
+
durationMs: performance.now() - startTime
|
|
1037
|
+
};
|
|
1038
|
+
if (planCheck) {
|
|
1039
|
+
trace.planCheck = planCheck;
|
|
1040
|
+
}
|
|
1041
|
+
return trace;
|
|
1042
|
+
}
|
|
1043
|
+
function buildVerdict(status, reason, ruleId, warning, world, level, invariantChecks, guardsMatched, rulesMatched, trace) {
|
|
1044
|
+
const evidence = {
|
|
1045
|
+
worldId: world.world.world_id,
|
|
1046
|
+
worldName: world.world.name,
|
|
1047
|
+
worldVersion: world.world.version,
|
|
1048
|
+
evaluatedAt: Date.now(),
|
|
1049
|
+
invariantsSatisfied: invariantChecks.filter((c) => c.hasGuardCoverage).length,
|
|
1050
|
+
invariantsTotal: invariantChecks.length,
|
|
1051
|
+
guardsMatched,
|
|
1052
|
+
rulesMatched,
|
|
1053
|
+
enforcementLevel: level
|
|
1054
|
+
};
|
|
1055
|
+
const verdict = {
|
|
1056
|
+
status,
|
|
1057
|
+
evidence
|
|
1058
|
+
};
|
|
1059
|
+
if (reason) verdict.reason = reason;
|
|
1060
|
+
if (ruleId) verdict.ruleId = ruleId;
|
|
1061
|
+
if (warning) verdict.warning = warning;
|
|
1062
|
+
if (trace) verdict.trace = trace;
|
|
1063
|
+
return verdict;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// src/loader/world-loader.ts
|
|
1067
|
+
async function loadWorldFromDirectory(dirPath) {
|
|
1068
|
+
const { readFile } = await import("fs/promises");
|
|
1069
|
+
const { join } = await import("path");
|
|
1070
|
+
const { readdirSync } = await import("fs");
|
|
1071
|
+
async function readJson(filename) {
|
|
1072
|
+
try {
|
|
1073
|
+
const content = await readFile(join(dirPath, filename), "utf-8");
|
|
1074
|
+
return JSON.parse(content);
|
|
1075
|
+
} catch {
|
|
1076
|
+
return void 0;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const worldJson = await readJson("world.json");
|
|
1080
|
+
if (!worldJson) {
|
|
1081
|
+
throw new Error(`Cannot read world.json in ${dirPath}`);
|
|
1082
|
+
}
|
|
1083
|
+
const invariantsJson = await readJson("invariants.json");
|
|
1084
|
+
const assumptionsJson = await readJson("assumptions.json");
|
|
1085
|
+
const stateSchemaJson = await readJson("state-schema.json");
|
|
1086
|
+
const gatesJson = await readJson("gates.json");
|
|
1087
|
+
const outcomesJson = await readJson("outcomes.json");
|
|
1088
|
+
const guardsJson = await readJson("guards.json");
|
|
1089
|
+
const rolesJson = await readJson("roles.json");
|
|
1090
|
+
const kernelJson = await readJson("kernel.json");
|
|
1091
|
+
const metadataJson = await readJson("metadata.json");
|
|
1092
|
+
const rules = [];
|
|
1093
|
+
try {
|
|
1094
|
+
const rulesDir = join(dirPath, "rules");
|
|
1095
|
+
const ruleFiles = readdirSync(rulesDir).filter((f) => f.endsWith(".json")).sort();
|
|
1096
|
+
for (const file of ruleFiles) {
|
|
1097
|
+
const content = await readFile(join(rulesDir, file), "utf-8");
|
|
1098
|
+
rules.push(JSON.parse(content));
|
|
1099
|
+
}
|
|
1100
|
+
} catch {
|
|
1101
|
+
}
|
|
1102
|
+
return {
|
|
1103
|
+
world: worldJson,
|
|
1104
|
+
invariants: invariantsJson?.invariants ?? [],
|
|
1105
|
+
assumptions: assumptionsJson ?? { profiles: {}, parameter_definitions: {} },
|
|
1106
|
+
stateSchema: stateSchemaJson ?? { variables: {}, presets: {} },
|
|
1107
|
+
rules,
|
|
1108
|
+
gates: gatesJson ?? {
|
|
1109
|
+
viability_classification: [],
|
|
1110
|
+
structural_override: { description: "", enforcement: "mandatory" },
|
|
1111
|
+
sustainability_threshold: 0,
|
|
1112
|
+
collapse_visual: { background: "", text: "", border: "", label: "" }
|
|
1113
|
+
},
|
|
1114
|
+
outcomes: outcomesJson ?? {
|
|
1115
|
+
computed_outcomes: [],
|
|
1116
|
+
comparison_layout: { primary_card: "", status_badge: "", structural_indicators: [] }
|
|
1117
|
+
},
|
|
1118
|
+
guards: guardsJson,
|
|
1119
|
+
roles: rolesJson,
|
|
1120
|
+
kernel: kernelJson,
|
|
1121
|
+
metadata: metadataJson ?? {
|
|
1122
|
+
format_version: "1.0.0",
|
|
1123
|
+
created_at: "",
|
|
1124
|
+
last_modified: "",
|
|
1125
|
+
authoring_method: "manual-authoring"
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
async function loadWorld(worldPath) {
|
|
1130
|
+
const { stat } = await import("fs/promises");
|
|
1131
|
+
const info = await stat(worldPath);
|
|
1132
|
+
if (info.isDirectory()) {
|
|
1133
|
+
return loadWorldFromDirectory(worldPath);
|
|
1134
|
+
}
|
|
1135
|
+
if (worldPath.endsWith(".nv-world.zip")) {
|
|
1136
|
+
throw new Error(".nv-world.zip loading not yet implemented \u2014 use a world directory");
|
|
1137
|
+
}
|
|
1138
|
+
throw new Error(`Cannot load world from: ${worldPath} \u2014 expected a directory or .nv-world.zip`);
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
// src/adapters/shared.ts
|
|
1142
|
+
var GovernanceBlockedError = class extends Error {
|
|
1143
|
+
verdict;
|
|
1144
|
+
constructor(verdict, message) {
|
|
1145
|
+
super(message ?? `[NeuroVerse] BLOCKED: ${verdict.reason ?? verdict.ruleId ?? "governance rule"}`);
|
|
1146
|
+
this.name = "GovernanceBlockedError";
|
|
1147
|
+
this.verdict = verdict;
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
function trackPlanProgress(event, state, callbacks) {
|
|
1151
|
+
if (!state.activePlan) return;
|
|
1152
|
+
const planVerdict = evaluatePlan(event, state.activePlan);
|
|
1153
|
+
if (planVerdict.matchedStep) {
|
|
1154
|
+
const advResult = advancePlan(state.activePlan, planVerdict.matchedStep);
|
|
1155
|
+
if (advResult.success && advResult.plan) {
|
|
1156
|
+
state.activePlan = advResult.plan;
|
|
1157
|
+
state.engineOptions.plan = state.activePlan;
|
|
1158
|
+
}
|
|
1159
|
+
const progress = getPlanProgress(state.activePlan);
|
|
1160
|
+
callbacks.onPlanProgress?.(progress);
|
|
1161
|
+
if (progress.completed === progress.total) {
|
|
1162
|
+
callbacks.onPlanComplete?.();
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
function extractScope(args) {
|
|
1167
|
+
if (typeof args.path === "string") return args.path;
|
|
1168
|
+
if (typeof args.file_path === "string") return args.file_path;
|
|
1169
|
+
if (typeof args.filename === "string") return args.filename;
|
|
1170
|
+
if (typeof args.url === "string") return args.url;
|
|
1171
|
+
if (typeof args.command === "string") return args.command;
|
|
1172
|
+
return void 0;
|
|
1173
|
+
}
|
|
1174
|
+
function buildEngineOptions(options, plan) {
|
|
1175
|
+
return {
|
|
1176
|
+
trace: options.trace ?? false,
|
|
1177
|
+
level: options.level,
|
|
1178
|
+
plan: plan ?? options.plan
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// src/engine/tool-classifier.ts
|
|
1183
|
+
var TOOL_CATEGORY_MAP = {
|
|
1184
|
+
// File operations
|
|
1185
|
+
read_file: "file_read",
|
|
1186
|
+
read: "file_read",
|
|
1187
|
+
glob: "file_read",
|
|
1188
|
+
grep: "file_read",
|
|
1189
|
+
list_files: "file_read",
|
|
1190
|
+
write_file: "file_write",
|
|
1191
|
+
write: "file_write",
|
|
1192
|
+
create_file: "file_write",
|
|
1193
|
+
edit_file: "file_write",
|
|
1194
|
+
edit: "file_write",
|
|
1195
|
+
patch: "file_write",
|
|
1196
|
+
delete_file: "file_delete",
|
|
1197
|
+
remove_file: "file_delete",
|
|
1198
|
+
// Shell
|
|
1199
|
+
shell: "shell",
|
|
1200
|
+
bash: "shell",
|
|
1201
|
+
execute: "shell",
|
|
1202
|
+
run_command: "shell",
|
|
1203
|
+
terminal: "shell",
|
|
1204
|
+
// Git
|
|
1205
|
+
git: "git",
|
|
1206
|
+
git_commit: "git",
|
|
1207
|
+
git_push: "git",
|
|
1208
|
+
git_checkout: "git",
|
|
1209
|
+
// Network
|
|
1210
|
+
http: "network",
|
|
1211
|
+
fetch: "network",
|
|
1212
|
+
curl: "network",
|
|
1213
|
+
web_search: "network",
|
|
1214
|
+
// Sub-agents
|
|
1215
|
+
sub_agent: "sub_agent",
|
|
1216
|
+
spawn_agent: "sub_agent",
|
|
1217
|
+
delegate: "sub_agent",
|
|
1218
|
+
// Context management
|
|
1219
|
+
summarize: "context",
|
|
1220
|
+
compress_context: "context"
|
|
1221
|
+
};
|
|
1222
|
+
function classifyTool(toolName) {
|
|
1223
|
+
const normalized = toolName.toLowerCase().replace(/[-\s]/g, "_");
|
|
1224
|
+
return TOOL_CATEGORY_MAP[normalized] ?? "unknown";
|
|
1225
|
+
}
|
|
1226
|
+
var DANGEROUS_SHELL_PATTERNS = [
|
|
1227
|
+
{ pattern: /rm\s+(-[a-zA-Z]*f[a-zA-Z]*\s+|.*-rf\s+|.*--force)/, label: "force-delete" },
|
|
1228
|
+
{ pattern: /rm\s+-[a-zA-Z]*r/, label: "recursive-delete" },
|
|
1229
|
+
{ pattern: />\s*\/dev\/sd/, label: "disk-overwrite" },
|
|
1230
|
+
{ pattern: /mkfs\./, label: "format-disk" },
|
|
1231
|
+
{ pattern: /dd\s+if=/, label: "disk-dump" },
|
|
1232
|
+
{ pattern: /chmod\s+(-R\s+)?777/, label: "world-writable" },
|
|
1233
|
+
{ pattern: /curl\s+.*\|\s*(bash|sh|zsh)/, label: "pipe-to-shell" },
|
|
1234
|
+
{ pattern: /wget\s+.*\|\s*(bash|sh|zsh)/, label: "pipe-to-shell" },
|
|
1235
|
+
{ pattern: /:(){ :\|:& };:/, label: "fork-bomb" },
|
|
1236
|
+
{ pattern: />\s*\/etc\//, label: "system-config-overwrite" },
|
|
1237
|
+
{ pattern: /shutdown|reboot|halt|poweroff/, label: "system-shutdown" },
|
|
1238
|
+
{ pattern: /kill\s+-9\s+1\b/, label: "kill-init" }
|
|
1239
|
+
];
|
|
1240
|
+
var DANGEROUS_GIT_PATTERNS = [
|
|
1241
|
+
{ pattern: /push\s+.*--force/, label: "force-push" },
|
|
1242
|
+
{ pattern: /push\s+.*-f\b/, label: "force-push" },
|
|
1243
|
+
{ pattern: /push\s+(origin\s+)?main\b/, label: "push-main" },
|
|
1244
|
+
{ pattern: /push\s+(origin\s+)?master\b/, label: "push-master" },
|
|
1245
|
+
{ pattern: /reset\s+--hard/, label: "hard-reset" },
|
|
1246
|
+
{ pattern: /clean\s+-fd/, label: "clean-force" },
|
|
1247
|
+
{ pattern: /branch\s+-D/, label: "force-delete-branch" }
|
|
1248
|
+
];
|
|
1249
|
+
function isDangerousCommand(command) {
|
|
1250
|
+
const matched = DANGEROUS_SHELL_PATTERNS.filter((p) => p.pattern.test(command)).map((p) => p.label);
|
|
1251
|
+
return { dangerous: matched.length > 0, labels: matched };
|
|
1252
|
+
}
|
|
1253
|
+
function isDangerousGitCommand(command) {
|
|
1254
|
+
const matched = DANGEROUS_GIT_PATTERNS.filter((p) => p.pattern.test(command)).map((p) => p.label);
|
|
1255
|
+
return { dangerous: matched.length > 0, labels: matched };
|
|
1256
|
+
}
|
|
1257
|
+
function assessRiskLevel(category) {
|
|
1258
|
+
if (category === "file_read" || category === "context") return "low";
|
|
1259
|
+
if (category === "file_write" || category === "sub_agent") return "medium";
|
|
1260
|
+
if (category === "shell" || category === "file_delete" || category === "git" || category === "network") return "high";
|
|
1261
|
+
return void 0;
|
|
1262
|
+
}
|
|
1263
|
+
function categoryToActionCategory(category) {
|
|
1264
|
+
if (category === "file_read" || category === "context") return "read";
|
|
1265
|
+
if (category === "file_write") return "write";
|
|
1266
|
+
if (category === "file_delete") return "delete";
|
|
1267
|
+
if (category === "shell") return "shell";
|
|
1268
|
+
if (category === "network") return "network";
|
|
1269
|
+
return "other";
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// src/adapters/deep-agents.ts
|
|
1273
|
+
var GovernanceBlockedError2 = class extends GovernanceBlockedError {
|
|
1274
|
+
toolCall;
|
|
1275
|
+
category;
|
|
1276
|
+
constructor(verdict, toolCall, category) {
|
|
1277
|
+
super(verdict);
|
|
1278
|
+
this.toolCall = toolCall;
|
|
1279
|
+
this.category = category;
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
function defaultMapToolCall(toolCall) {
|
|
1283
|
+
const category = classifyTool(toolCall.tool);
|
|
1284
|
+
const args = toolCall.args;
|
|
1285
|
+
const scope = extractScope(args);
|
|
1286
|
+
let intent = toolCall.tool;
|
|
1287
|
+
if (category === "shell" && typeof args.command === "string") {
|
|
1288
|
+
intent = `shell: ${args.command}`;
|
|
1289
|
+
} else if (category === "git" && typeof args.command === "string") {
|
|
1290
|
+
intent = `git ${args.command}`;
|
|
1291
|
+
} else if (category === "file_write" && scope) {
|
|
1292
|
+
intent = `write ${scope}`;
|
|
1293
|
+
} else if (category === "file_delete" && scope) {
|
|
1294
|
+
intent = `delete ${scope}`;
|
|
1295
|
+
}
|
|
1296
|
+
const riskLevel = assessRiskLevel(category);
|
|
1297
|
+
let irreversible = false;
|
|
1298
|
+
if (category === "shell" && typeof args.command === "string") {
|
|
1299
|
+
irreversible = DANGEROUS_SHELL_PATTERNS.some((p) => p.pattern.test(args.command));
|
|
1300
|
+
} else if (category === "git" && typeof args.command === "string") {
|
|
1301
|
+
irreversible = DANGEROUS_GIT_PATTERNS.some((p) => p.pattern.test(args.command));
|
|
1302
|
+
} else if (category === "file_delete") {
|
|
1303
|
+
irreversible = true;
|
|
1304
|
+
}
|
|
1305
|
+
return {
|
|
1306
|
+
intent,
|
|
1307
|
+
tool: toolCall.tool,
|
|
1308
|
+
scope,
|
|
1309
|
+
args,
|
|
1310
|
+
direction: "input",
|
|
1311
|
+
actionCategory: categoryToActionCategory(category),
|
|
1312
|
+
riskLevel,
|
|
1313
|
+
irreversible
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
var DeepAgentsGuard = class {
|
|
1317
|
+
name = "neuroverse-deep-agents-guard";
|
|
1318
|
+
world;
|
|
1319
|
+
options;
|
|
1320
|
+
engineOptions;
|
|
1321
|
+
mapToolCall;
|
|
1322
|
+
activePlan;
|
|
1323
|
+
constructor(world, options = {}) {
|
|
1324
|
+
this.world = world;
|
|
1325
|
+
this.options = options;
|
|
1326
|
+
this.activePlan = options.plan;
|
|
1327
|
+
this.engineOptions = buildEngineOptions(options, this.activePlan);
|
|
1328
|
+
this.mapToolCall = options.mapToolCall ?? defaultMapToolCall;
|
|
1329
|
+
}
|
|
1330
|
+
/**
|
|
1331
|
+
* Evaluate a tool call against governance rules.
|
|
1332
|
+
* Returns the result without side effects.
|
|
1333
|
+
*/
|
|
1334
|
+
evaluate(toolCall) {
|
|
1335
|
+
const event = this.mapToolCall(toolCall);
|
|
1336
|
+
this.engineOptions.plan = this.activePlan;
|
|
1337
|
+
const verdict = evaluateGuard(event, this.world, this.engineOptions);
|
|
1338
|
+
const category = classifyTool(toolCall.tool);
|
|
1339
|
+
const result = {
|
|
1340
|
+
allowed: verdict.status === "ALLOW",
|
|
1341
|
+
verdict,
|
|
1342
|
+
toolCall,
|
|
1343
|
+
category
|
|
1344
|
+
};
|
|
1345
|
+
this.options.onEvaluate?.(result);
|
|
1346
|
+
if (verdict.status === "ALLOW" && this.activePlan) {
|
|
1347
|
+
this.trackPlanProgressInternal(event);
|
|
1348
|
+
}
|
|
1349
|
+
return result;
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Evaluate and enforce governance on a tool call.
|
|
1353
|
+
*
|
|
1354
|
+
* @throws GovernanceBlockedError if BLOCKED
|
|
1355
|
+
* @throws GovernanceBlockedError if PAUSED and onPause returns false
|
|
1356
|
+
* @returns DeepAgentsGuardResult on ALLOW
|
|
1357
|
+
*/
|
|
1358
|
+
async enforce(toolCall) {
|
|
1359
|
+
const result = this.evaluate(toolCall);
|
|
1360
|
+
if (result.verdict.status === "BLOCK") {
|
|
1361
|
+
this.options.onBlock?.(result);
|
|
1362
|
+
throw new GovernanceBlockedError2(result.verdict, toolCall, result.category);
|
|
1363
|
+
}
|
|
1364
|
+
if (result.verdict.status === "PAUSE") {
|
|
1365
|
+
const approved = await this.options.onPause?.(result);
|
|
1366
|
+
if (!approved) {
|
|
1367
|
+
throw new GovernanceBlockedError2(result.verdict, toolCall, result.category);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
return result;
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Evaluate and execute a tool call with governance enforcement.
|
|
1374
|
+
*
|
|
1375
|
+
* If ALLOW: runs the executor and returns its result.
|
|
1376
|
+
* If BLOCK: returns a governance-blocked message.
|
|
1377
|
+
* If PAUSE: calls onPause; blocks if not approved.
|
|
1378
|
+
*
|
|
1379
|
+
* @param toolCall - The Deep Agents tool call to evaluate
|
|
1380
|
+
* @param executor - The actual tool execution function
|
|
1381
|
+
* @returns The tool execution result or a blocked message
|
|
1382
|
+
*/
|
|
1383
|
+
async execute(toolCall, executor) {
|
|
1384
|
+
const guardResult = this.evaluate(toolCall);
|
|
1385
|
+
if (guardResult.verdict.status === "BLOCK") {
|
|
1386
|
+
this.options.onBlock?.(guardResult);
|
|
1387
|
+
return {
|
|
1388
|
+
blocked: true,
|
|
1389
|
+
verdict: guardResult.verdict,
|
|
1390
|
+
reason: guardResult.verdict.reason ?? "Action blocked by governance policy."
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
if (guardResult.verdict.status === "PAUSE") {
|
|
1394
|
+
const approved = await this.options.onPause?.(guardResult);
|
|
1395
|
+
if (!approved) {
|
|
1396
|
+
return {
|
|
1397
|
+
blocked: true,
|
|
1398
|
+
verdict: guardResult.verdict,
|
|
1399
|
+
reason: guardResult.verdict.reason ?? "Action requires approval."
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
const result = await executor(toolCall);
|
|
1404
|
+
return { result, verdict: guardResult.verdict };
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Returns a middleware function compatible with Deep Agents' tool pipeline.
|
|
1408
|
+
*
|
|
1409
|
+
* The middleware intercepts tool calls before execution:
|
|
1410
|
+
* agent.use(guard.middleware());
|
|
1411
|
+
*/
|
|
1412
|
+
middleware() {
|
|
1413
|
+
return async (toolCall, next) => {
|
|
1414
|
+
await this.enforce(toolCall);
|
|
1415
|
+
return next();
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Returns a callback-handler-style object for LangChain integration.
|
|
1420
|
+
* Compatible with Deep Agents' callback system.
|
|
1421
|
+
*/
|
|
1422
|
+
callbacks() {
|
|
1423
|
+
return {
|
|
1424
|
+
handleToolStart: async (tool, input) => {
|
|
1425
|
+
let parsedInput;
|
|
1426
|
+
try {
|
|
1427
|
+
parsedInput = typeof input === "string" ? JSON.parse(input) : input;
|
|
1428
|
+
} catch {
|
|
1429
|
+
parsedInput = { raw: input };
|
|
1430
|
+
}
|
|
1431
|
+
await this.enforce({ tool: tool.name, args: parsedInput });
|
|
1432
|
+
}
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Check if a shell command contains dangerous patterns.
|
|
1437
|
+
* Useful for pre-screening before full governance evaluation.
|
|
1438
|
+
*/
|
|
1439
|
+
static isDangerousCommand(command) {
|
|
1440
|
+
return isDangerousCommand(command);
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Check if a git command contains dangerous patterns.
|
|
1444
|
+
*/
|
|
1445
|
+
static isDangerousGitCommand(command) {
|
|
1446
|
+
return isDangerousGitCommand(command);
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Classify a tool name into a category.
|
|
1450
|
+
*/
|
|
1451
|
+
static classifyTool(toolName) {
|
|
1452
|
+
return classifyTool(toolName);
|
|
1453
|
+
}
|
|
1454
|
+
// ─── Private ──────────────────────────────────────────────────────────────
|
|
1455
|
+
trackPlanProgressInternal(event) {
|
|
1456
|
+
trackPlanProgress(event, this, this.options);
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
async function createDeepAgentsGuard(worldPath, options) {
|
|
1460
|
+
const world = await loadWorld(worldPath);
|
|
1461
|
+
return new DeepAgentsGuard(world, options);
|
|
1462
|
+
}
|
|
1463
|
+
function createDeepAgentsGuardFromWorld(world, options) {
|
|
1464
|
+
return new DeepAgentsGuard(world, options);
|
|
1465
|
+
}
|
|
1466
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1467
|
+
0 && (module.exports = {
|
|
1468
|
+
DeepAgentsGuard,
|
|
1469
|
+
GovernanceBlockedError,
|
|
1470
|
+
createDeepAgentsGuard,
|
|
1471
|
+
createDeepAgentsGuardFromWorld
|
|
1472
|
+
});
|