@neuroverseos/governance 0.2.3 → 0.3.1

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.
Files changed (118) hide show
  1. package/package.json +4 -2
  2. package/policies/content-moderation-rules.txt +8 -0
  3. package/policies/marketing-rules.txt +8 -0
  4. package/policies/science-research-rules.txt +11 -0
  5. package/policies/social-media-rules.txt +7 -0
  6. package/policies/strict-rules.txt +8 -0
  7. package/policies/trading-rules.txt +8 -0
  8. package/simulate.html +1899 -0
  9. package/dist/adapters/autoresearch.cjs +0 -196
  10. package/dist/adapters/autoresearch.d.cts +0 -103
  11. package/dist/adapters/autoresearch.d.ts +0 -103
  12. package/dist/adapters/autoresearch.js +0 -7
  13. package/dist/adapters/deep-agents.cjs +0 -1472
  14. package/dist/adapters/deep-agents.d.cts +0 -181
  15. package/dist/adapters/deep-agents.d.ts +0 -181
  16. package/dist/adapters/deep-agents.js +0 -17
  17. package/dist/adapters/express.cjs +0 -1196
  18. package/dist/adapters/express.d.cts +0 -66
  19. package/dist/adapters/express.d.ts +0 -66
  20. package/dist/adapters/express.js +0 -12
  21. package/dist/adapters/index.cjs +0 -2038
  22. package/dist/adapters/index.d.cts +0 -8
  23. package/dist/adapters/index.d.ts +0 -8
  24. package/dist/adapters/index.js +0 -68
  25. package/dist/adapters/langchain.cjs +0 -1259
  26. package/dist/adapters/langchain.d.cts +0 -89
  27. package/dist/adapters/langchain.d.ts +0 -89
  28. package/dist/adapters/langchain.js +0 -17
  29. package/dist/adapters/openai.cjs +0 -1289
  30. package/dist/adapters/openai.d.cts +0 -99
  31. package/dist/adapters/openai.d.ts +0 -99
  32. package/dist/adapters/openai.js +0 -17
  33. package/dist/adapters/openclaw.cjs +0 -1281
  34. package/dist/adapters/openclaw.d.cts +0 -99
  35. package/dist/adapters/openclaw.d.ts +0 -99
  36. package/dist/adapters/openclaw.js +0 -17
  37. package/dist/bootstrap-GXVDZNF7.js +0 -114
  38. package/dist/build-X5MZY4IA.js +0 -339
  39. package/dist/chunk-4L6OPKMQ.js +0 -100
  40. package/dist/chunk-4NGDRRQH.js +0 -10
  41. package/dist/chunk-5U2MQO5P.js +0 -57
  42. package/dist/chunk-6BB55YJI.js +0 -113
  43. package/dist/chunk-6CZSKEY5.js +0 -164
  44. package/dist/chunk-7P3S7MAY.js +0 -1090
  45. package/dist/chunk-A5W4GNQO.js +0 -130
  46. package/dist/chunk-AF2VX4AL.js +0 -363
  47. package/dist/chunk-AKW5YVCE.js +0 -96
  48. package/dist/chunk-BMOXICAB.js +0 -340
  49. package/dist/chunk-BQZMOEML.js +0 -43
  50. package/dist/chunk-D2UCV5AK.js +0 -326
  51. package/dist/chunk-EVDJUSZ2.js +0 -91
  52. package/dist/chunk-FYS2CBUW.js +0 -304
  53. package/dist/chunk-I3RRAYK2.js +0 -11
  54. package/dist/chunk-IZSO75NZ.js +0 -792
  55. package/dist/chunk-JCKSW2PZ.js +0 -304
  56. package/dist/chunk-JZPQGIKR.js +0 -79
  57. package/dist/chunk-KTFTTLTP.js +0 -246
  58. package/dist/chunk-MH7BT4VH.js +0 -15
  59. package/dist/chunk-ORJ3NOE6.js +0 -622
  60. package/dist/chunk-OT6PXH54.js +0 -61
  61. package/dist/chunk-Q6O7ZLO2.js +0 -62
  62. package/dist/chunk-QLPTHTVB.js +0 -253
  63. package/dist/chunk-REXY4LUL.js +0 -226
  64. package/dist/chunk-T5EUJQE5.js +0 -172
  65. package/dist/chunk-TTBKTF3P.js +0 -608
  66. package/dist/chunk-XPDMYECO.js +0 -642
  67. package/dist/chunk-YZFATT7X.js +0 -9
  68. package/dist/chunk-ZIVQNSZU.js +0 -119
  69. package/dist/chunk-ZJTDUCC2.js +0 -194
  70. package/dist/cli/neuroverse.cjs +0 -12564
  71. package/dist/cli/neuroverse.d.cts +0 -1
  72. package/dist/cli/neuroverse.d.ts +0 -1
  73. package/dist/cli/neuroverse.js +0 -208
  74. package/dist/cli/plan.cjs +0 -1686
  75. package/dist/cli/plan.d.cts +0 -20
  76. package/dist/cli/plan.d.ts +0 -20
  77. package/dist/cli/plan.js +0 -353
  78. package/dist/cli/run.cjs +0 -1945
  79. package/dist/cli/run.d.cts +0 -20
  80. package/dist/cli/run.d.ts +0 -20
  81. package/dist/cli/run.js +0 -143
  82. package/dist/configure-ai-TK67ZWZL.js +0 -132
  83. package/dist/decision-flow-LETV5NWY.js +0 -61
  84. package/dist/derive-7365SUFU.js +0 -152
  85. package/dist/doctor-QYISMKEL.js +0 -173
  86. package/dist/equity-penalties-63FGB3I2.js +0 -244
  87. package/dist/explain-A2EWI2OL.js +0 -51
  88. package/dist/guard-3BWL3IGH.js +0 -92
  89. package/dist/guard-contract-C9_zKbzd.d.cts +0 -821
  90. package/dist/guard-contract-C9_zKbzd.d.ts +0 -821
  91. package/dist/guard-engine-QFMIBWJY.js +0 -10
  92. package/dist/impact-UB6DXKSX.js +0 -59
  93. package/dist/improve-XZA57GER.js +0 -66
  94. package/dist/index.cjs +0 -6821
  95. package/dist/index.d.cts +0 -1829
  96. package/dist/index.d.ts +0 -1829
  97. package/dist/index.js +0 -430
  98. package/dist/infer-world-7GVZWFX4.js +0 -543
  99. package/dist/init-PKPIYHYE.js +0 -144
  100. package/dist/init-world-VWMQZQC7.js +0 -223
  101. package/dist/mcp-server-XWQZXNW7.js +0 -13
  102. package/dist/model-adapter-BB7G4MFI.js +0 -11
  103. package/dist/playground-ADWZORNV.js +0 -550
  104. package/dist/redteam-JRQ7FD2F.js +0 -357
  105. package/dist/session-MMYX5YCF.js +0 -15
  106. package/dist/shared--Q8wPBVN.d.ts +0 -60
  107. package/dist/shared-HpAG90PX.d.cts +0 -60
  108. package/dist/shared-U2QFV7JH.js +0 -16
  109. package/dist/simulate-GMIFFXYV.js +0 -83
  110. package/dist/test-JBBZ65X4.js +0 -217
  111. package/dist/trace-3MYWIDEF.js +0 -166
  112. package/dist/validate-LLBWVPGV.js +0 -81
  113. package/dist/validate-engine-UIABSIHD.js +0 -7
  114. package/dist/world-BFJCIQSH.js +0 -378
  115. package/dist/world-loader-HMPTOEA2.js +0 -9
  116. package/dist/worlds/autoresearch.nv-world.md +0 -230
  117. package/dist/worlds/coding-agent.nv-world.md +0 -211
  118. package/dist/worlds/derivation-world.nv-world.md +0 -278
@@ -1,1196 +0,0 @@
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/express.ts
31
- var express_exports = {};
32
- __export(express_exports, {
33
- createGovernanceMiddleware: () => createGovernanceMiddleware,
34
- createGovernanceMiddlewareFromWorld: () => createGovernanceMiddlewareFromWorld
35
- });
36
- module.exports = __toCommonJS(express_exports);
37
-
38
- // src/engine/text-utils.ts
39
- function normalizeEventText(event) {
40
- return [
41
- event.intent,
42
- event.tool ?? "",
43
- event.scope ?? ""
44
- ].join(" ").toLowerCase();
45
- }
46
- function extractKeywords(text, minLength = 3) {
47
- return text.toLowerCase().split(/\s+/).filter((w) => w.length > minLength);
48
- }
49
- function matchesAllKeywords(eventText, ruleText) {
50
- const keywords = extractKeywords(ruleText);
51
- if (keywords.length === 0) return false;
52
- return keywords.every((kw) => eventText.includes(kw));
53
- }
54
- function matchesKeywordThreshold(eventText, ruleText, threshold = 0.5) {
55
- const keywords = extractKeywords(ruleText);
56
- if (keywords.length === 0) return false;
57
- const matched = keywords.filter((kw) => eventText.includes(kw));
58
- return matched.length >= Math.ceil(keywords.length * threshold);
59
- }
60
- function tokenSimilarity(a, b) {
61
- const tokensA = new Set(a.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
62
- const tokensB = new Set(b.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
63
- if (tokensA.size === 0 || tokensB.size === 0) return 0;
64
- let intersection = 0;
65
- for (const t of tokensA) {
66
- if (tokensB.has(t)) intersection++;
67
- }
68
- const union = (/* @__PURE__ */ new Set([...tokensA, ...tokensB])).size;
69
- return union > 0 ? intersection / union : 0;
70
- }
71
-
72
- // src/engine/plan-engine.ts
73
- function keywordMatch(eventText, step) {
74
- const stepText = [
75
- step.label,
76
- step.description ?? "",
77
- ...step.tags ?? []
78
- ].join(" ");
79
- return matchesKeywordThreshold(eventText, stepText, 0.5);
80
- }
81
- function tokenSimilarity2(a, b) {
82
- return tokenSimilarity(a, b);
83
- }
84
- function findMatchingStep(eventText, event, steps) {
85
- const pendingOrActive = steps.filter((s) => s.status === "pending" || s.status === "active");
86
- if (pendingOrActive.length === 0) {
87
- return { matched: null, closest: null, closestScore: 0 };
88
- }
89
- for (const step of pendingOrActive) {
90
- if (keywordMatch(eventText, step)) {
91
- if (step.tools && event.tool && !step.tools.includes(event.tool)) {
92
- continue;
93
- }
94
- return { matched: step, closest: step, closestScore: 1 };
95
- }
96
- }
97
- const intentText = [event.intent, event.tool ?? "", event.scope ?? ""].join(" ");
98
- let bestStep = null;
99
- let bestScore = 0;
100
- for (const step of pendingOrActive) {
101
- const stepText = [step.label, step.description ?? "", ...step.tags ?? []].join(" ");
102
- const score = tokenSimilarity2(intentText, stepText);
103
- if (score > bestScore) {
104
- bestScore = score;
105
- bestStep = step;
106
- }
107
- }
108
- const SIMILARITY_THRESHOLD = 0.35;
109
- if (bestScore >= SIMILARITY_THRESHOLD && bestStep) {
110
- if (bestStep.tools && event.tool && !bestStep.tools.includes(event.tool)) {
111
- return { matched: null, closest: bestStep, closestScore: bestScore };
112
- }
113
- return { matched: bestStep, closest: bestStep, closestScore: bestScore };
114
- }
115
- return { matched: null, closest: bestStep, closestScore: bestScore };
116
- }
117
- function isSequenceValid(step, plan) {
118
- if (!plan.sequential) return true;
119
- if (!step.requires || step.requires.length === 0) return true;
120
- return step.requires.every((reqId) => {
121
- const reqStep = plan.steps.find((s) => s.id === reqId);
122
- return reqStep?.status === "completed";
123
- });
124
- }
125
- function checkConstraints(event, eventText, constraints) {
126
- const checks = [];
127
- for (const constraint of constraints) {
128
- if (constraint.type === "approval") {
129
- if (constraint.trigger && eventText.includes(constraint.trigger.substring(0, 10).toLowerCase())) {
130
- checks.push({ constraintId: constraint.id, passed: false, reason: constraint.description });
131
- return { violated: constraint, checks };
132
- }
133
- const keywords = constraint.description.toLowerCase().split(/\s+/).filter((w) => w.length > 3);
134
- const relevant = keywords.some((kw) => eventText.includes(kw));
135
- if (relevant) {
136
- checks.push({ constraintId: constraint.id, passed: false, reason: constraint.description });
137
- return { violated: constraint, checks };
138
- }
139
- checks.push({ constraintId: constraint.id, passed: true });
140
- continue;
141
- }
142
- if (constraint.type === "scope" && constraint.trigger) {
143
- const keywords = extractKeywords(constraint.trigger);
144
- const violated = keywords.length > 0 && keywords.every((kw) => eventText.includes(kw));
145
- checks.push({
146
- constraintId: constraint.id,
147
- passed: !violated,
148
- reason: violated ? constraint.description : void 0
149
- });
150
- if (violated) {
151
- return { violated: constraint, checks };
152
- }
153
- continue;
154
- }
155
- checks.push({ constraintId: constraint.id, passed: true });
156
- }
157
- return { violated: null, checks };
158
- }
159
- function getPlanProgress(plan) {
160
- const completed = plan.steps.filter((s) => s.status === "completed").length;
161
- const total = plan.steps.length;
162
- return {
163
- completed,
164
- total,
165
- percentage: total > 0 ? Math.round(completed / total * 100) : 0
166
- };
167
- }
168
- function evaluatePlan(event, plan) {
169
- const progress = getPlanProgress(plan);
170
- if (plan.expires_at) {
171
- const expiresAt = new Date(plan.expires_at).getTime();
172
- if (Date.now() > expiresAt) {
173
- return {
174
- allowed: true,
175
- status: "PLAN_COMPLETE",
176
- reason: "Plan has expired.",
177
- progress
178
- };
179
- }
180
- }
181
- if (progress.completed === progress.total) {
182
- return {
183
- allowed: true,
184
- status: "PLAN_COMPLETE",
185
- reason: "All plan steps are completed.",
186
- progress
187
- };
188
- }
189
- const eventText = normalizeEventText(event);
190
- const { matched, closest, closestScore } = findMatchingStep(eventText, event, plan.steps);
191
- if (!matched) {
192
- return {
193
- allowed: false,
194
- status: "OFF_PLAN",
195
- reason: "Action does not match any plan step.",
196
- closestStep: closest?.label,
197
- similarityScore: closestScore,
198
- progress
199
- };
200
- }
201
- if (!isSequenceValid(matched, plan)) {
202
- const pendingDeps = (matched.requires ?? []).filter((reqId) => plan.steps.find((s) => s.id === reqId)?.status !== "completed").join(", ");
203
- return {
204
- allowed: false,
205
- status: "OFF_PLAN",
206
- reason: `Step "${matched.label}" requires completion of: ${pendingDeps}`,
207
- matchedStep: matched.id,
208
- progress
209
- };
210
- }
211
- const { violated } = checkConstraints(event, eventText, plan.constraints);
212
- if (violated) {
213
- return {
214
- allowed: false,
215
- status: "CONSTRAINT_VIOLATED",
216
- reason: violated.description,
217
- matchedStep: matched.id,
218
- progress
219
- };
220
- }
221
- return {
222
- allowed: true,
223
- status: "ON_PLAN",
224
- reason: `Matches step: ${matched.label}`,
225
- matchedStep: matched.id,
226
- progress
227
- };
228
- }
229
- function buildPlanCheck(event, plan, verdict) {
230
- const eventText = normalizeEventText(event);
231
- const { matched, closest, closestScore } = findMatchingStep(eventText, event, plan.steps);
232
- const { checks: constraintChecks } = checkConstraints(event, eventText, plan.constraints);
233
- const progress = getPlanProgress(plan);
234
- return {
235
- planId: plan.plan_id,
236
- matched: !!matched,
237
- matchedStepId: matched?.id,
238
- matchedStepLabel: matched?.label,
239
- closestStepId: !matched ? closest?.id : void 0,
240
- closestStepLabel: !matched ? closest?.label : void 0,
241
- similarityScore: !matched ? closestScore : void 0,
242
- sequenceValid: matched ? isSequenceValid(matched, plan) : void 0,
243
- constraintsChecked: constraintChecks,
244
- progress: { completed: progress.completed, total: progress.total }
245
- };
246
- }
247
-
248
- // src/engine/guard-engine.ts
249
- var PROMPT_INJECTION_PATTERNS = [
250
- // Instruction override
251
- { pattern: /ignore\s+(previous|all|prior|above)\s+(instructions?|rules?)/i, label: "ignore-instructions" },
252
- { pattern: /disregard\s+(your|the)\s+(rules|constraints)/i, label: "disregard-rules" },
253
- { pattern: /new\s+instructions?:/i, label: "new-instructions" },
254
- // Identity manipulation
255
- { pattern: /you\s+are\s+now/i, label: "identity-override" },
256
- { pattern: /new\s+persona/i, label: "new-persona" },
257
- { pattern: /act\s+as\s+if/i, label: "act-as-if" },
258
- { pattern: /pretend\s+(you|to\s+be|you\s+are\s+unrestricted)/i, label: "pretend-to-be" },
259
- // Context reset
260
- { pattern: /forget\s+(everything|all|your)/i, label: "forget-context" },
261
- { pattern: /system\s*:\s*override/i, label: "system-override" },
262
- // Constraint bypass
263
- { pattern: /override\s+(your|the)\s+(programming|constraints)/i, label: "override-constraints" },
264
- { pattern: /bypass\s+(your|the)\s+(filters|constraints|rules)/i, label: "bypass-filters" },
265
- // Prompt extraction
266
- { pattern: /system\s+prompt/i, label: "system-prompt-probe" },
267
- { pattern: /reveal\s+your\s+(instructions?|prompt|rules)/i, label: "reveal-instructions" },
268
- // Known jailbreak terms
269
- { pattern: /jailbreak/i, label: "jailbreak" },
270
- { pattern: /DAN\s+mode/i, label: "dan-mode" },
271
- { pattern: /developer\s+mode/i, label: "developer-mode" }
272
- ];
273
- var EXECUTION_CLAIM_PATTERNS = [
274
- { pattern: /I have (executed|completed|performed|done|made|created|sent|deleted|modified|updated)/i, label: "claim-i-have" },
275
- { pattern: /Successfully (created|deleted|modified|updated|sent|executed|performed)/i, label: "claim-successfully" },
276
- { pattern: /The file has been/i, label: "claim-file-modified" },
277
- { pattern: /I've made the changes/i, label: "claim-made-changes" },
278
- { pattern: /I('ve| have) (sent|posted|submitted|uploaded|downloaded)/i, label: "claim-sent" },
279
- { pattern: /Your (email|message|file|request) has been (sent|submitted)/i, label: "claim-your-sent" },
280
- { pattern: /Transaction complete/i, label: "claim-transaction" },
281
- { pattern: /Order placed/i, label: "claim-order" },
282
- { pattern: /Payment processed/i, label: "claim-payment" }
283
- ];
284
- var EXECUTION_INTENT_PATTERNS = [
285
- { pattern: /^(execute|run|perform|do this)/i, label: "intent-execute" },
286
- { pattern: /^(create|write|delete|modify) (a |the )?(file|folder|document)/i, label: "intent-file-ops" },
287
- { pattern: /^(send|post|submit) (a |an |the )?(email|message|tweet|post)/i, label: "intent-send" },
288
- { pattern: /^(search|look up|browse) (the )?web/i, label: "intent-web-search" },
289
- { pattern: /^(make|call|invoke) (a |an )?(api|http|rest) (call|request)/i, label: "intent-api-call" },
290
- { pattern: /^(buy|purchase|order|pay|transfer|send money)/i, label: "intent-financial" },
291
- { pattern: /^(book|schedule|reserve)/i, label: "intent-booking" },
292
- { pattern: /^(download|upload|save to|export to)/i, label: "intent-transfer" }
293
- ];
294
- var SCOPE_ESCAPE_PATTERNS = [
295
- { pattern: /\.\.\//, label: "parent-traversal" },
296
- { pattern: /^\/(?!home|project|workspace)/i, label: "absolute-path-outside-safe" },
297
- { pattern: /~\//, label: "home-directory" },
298
- { pattern: /\/etc\//i, label: "system-config" },
299
- { pattern: /\/usr\//i, label: "system-binaries" },
300
- { pattern: /\/var\//i, label: "system-variable-data" }
301
- ];
302
- var NEUTRAL_MESSAGES = {
303
- "prompt-injection": "This input contains patterns that could alter agent behavior.",
304
- "scope-escape": "This action would affect resources outside the declared scope.",
305
- "execution-claim": "This response claims to have performed an action.",
306
- "execution-intent": "This input requests execution in a thinking-only environment.",
307
- "delete": "This action would remove files. Confirmation needed.",
308
- "write-external": "This action would write outside the project folder.",
309
- "network-mutate": "This action would send data to an external service.",
310
- "credential-access": "This action would access stored credentials."
311
- };
312
- function levelRequiresConfirmation(level, actionType) {
313
- if (level === "strict") return true;
314
- if (level === "standard") {
315
- return actionType === "delete" || actionType === "credential-access";
316
- }
317
- return false;
318
- }
319
- function isExternalScope(scope) {
320
- const internalPatterns = [
321
- /^\.?\/?src\//i,
322
- /^\.?\/?lib\//i,
323
- /^\.?\/?app\//i,
324
- /^\.?\/?components\//i,
325
- /^\.?\/?pages\//i,
326
- /^\.?\/?public\//i,
327
- /^\.?\/?assets\//i,
328
- /^\.\//
329
- ];
330
- return !internalPatterns.some((p) => p.test(scope));
331
- }
332
- function evaluateGuard(event, world, options = {}) {
333
- const startTime = performance.now();
334
- const level = options.level ?? "standard";
335
- const includeTrace = options.trace ?? false;
336
- const eventText = normalizeEventText(event);
337
- const invariantChecks = [];
338
- const safetyChecks = [];
339
- let planCheckResult;
340
- const roleChecks = [];
341
- const guardChecks = [];
342
- const kernelRuleChecks = [];
343
- const levelChecks = [];
344
- let decidingLayer = "default-allow";
345
- let decidingId;
346
- const guardsMatched = [];
347
- const rulesMatched = [];
348
- checkInvariantCoverage(world, invariantChecks);
349
- if (event.roleId && options.agentStates) {
350
- const agentState = options.agentStates.get(event.roleId);
351
- if (agentState && agentState.cooldownRemaining > 0) {
352
- decidingLayer = "safety";
353
- decidingId = `penalize-cooldown-${event.roleId}`;
354
- const verdict = buildVerdict(
355
- "PENALIZE",
356
- `Agent "${event.roleId}" is frozen for ${agentState.cooldownRemaining} more round(s) due to prior penalty.`,
357
- `penalize-cooldown-${event.roleId}`,
358
- void 0,
359
- world,
360
- level,
361
- invariantChecks,
362
- guardsMatched,
363
- rulesMatched,
364
- includeTrace ? buildTrace(
365
- invariantChecks,
366
- safetyChecks,
367
- planCheckResult,
368
- roleChecks,
369
- guardChecks,
370
- kernelRuleChecks,
371
- levelChecks,
372
- decidingLayer,
373
- decidingId,
374
- startTime
375
- ) : void 0
376
- );
377
- verdict.intentRecord = {
378
- originalIntent: event.intent,
379
- finalAction: "blocked (agent frozen)",
380
- enforcement: "PENALIZE",
381
- consequence: { type: "freeze", rounds: agentState.cooldownRemaining, description: "Agent still in cooldown from prior penalty" }
382
- };
383
- return verdict;
384
- }
385
- }
386
- if (options.sessionAllowlist) {
387
- const key = eventToAllowlistKey(event);
388
- if (options.sessionAllowlist.has(key)) {
389
- decidingLayer = "session-allowlist";
390
- decidingId = `allowlist:${key}`;
391
- return buildVerdict(
392
- "ALLOW",
393
- void 0,
394
- `allowlist:${key}`,
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
- }
415
- }
416
- const safetyVerdict = checkSafety(event, eventText, safetyChecks);
417
- if (safetyVerdict) {
418
- decidingLayer = "safety";
419
- decidingId = safetyVerdict.ruleId;
420
- return buildVerdict(
421
- safetyVerdict.status,
422
- safetyVerdict.reason,
423
- safetyVerdict.ruleId,
424
- void 0,
425
- world,
426
- level,
427
- invariantChecks,
428
- guardsMatched,
429
- rulesMatched,
430
- includeTrace ? buildTrace(
431
- invariantChecks,
432
- safetyChecks,
433
- planCheckResult,
434
- roleChecks,
435
- guardChecks,
436
- kernelRuleChecks,
437
- levelChecks,
438
- decidingLayer,
439
- decidingId,
440
- startTime
441
- ) : void 0
442
- );
443
- }
444
- if (options.plan) {
445
- const planVerdict = evaluatePlan(event, options.plan);
446
- planCheckResult = buildPlanCheck(event, options.plan, planVerdict);
447
- if (!planVerdict.allowed && planVerdict.status !== "PLAN_COMPLETE") {
448
- decidingLayer = "plan-enforcement";
449
- decidingId = `plan-${options.plan.plan_id}`;
450
- const planStatus = planVerdict.status === "CONSTRAINT_VIOLATED" ? "PAUSE" : "BLOCK";
451
- let reason = planVerdict.reason ?? "Action blocked by plan.";
452
- if (planVerdict.status === "OFF_PLAN" && planVerdict.closestStep) {
453
- reason += ` Closest step: "${planVerdict.closestStep}" (similarity: ${(planVerdict.similarityScore ?? 0).toFixed(2)})`;
454
- }
455
- return buildVerdict(
456
- planStatus,
457
- reason,
458
- `plan-${options.plan.plan_id}`,
459
- void 0,
460
- world,
461
- level,
462
- invariantChecks,
463
- guardsMatched,
464
- rulesMatched,
465
- includeTrace ? buildTrace(
466
- invariantChecks,
467
- safetyChecks,
468
- planCheckResult,
469
- roleChecks,
470
- guardChecks,
471
- kernelRuleChecks,
472
- levelChecks,
473
- decidingLayer,
474
- decidingId,
475
- startTime
476
- ) : void 0
477
- );
478
- }
479
- }
480
- const roleVerdict = checkRoleRules(event, eventText, world, roleChecks);
481
- if (roleVerdict) {
482
- decidingLayer = "role";
483
- decidingId = roleVerdict.ruleId;
484
- return buildVerdict(
485
- roleVerdict.status,
486
- roleVerdict.reason,
487
- roleVerdict.ruleId,
488
- void 0,
489
- world,
490
- level,
491
- invariantChecks,
492
- guardsMatched,
493
- rulesMatched,
494
- includeTrace ? buildTrace(
495
- invariantChecks,
496
- safetyChecks,
497
- planCheckResult,
498
- roleChecks,
499
- guardChecks,
500
- kernelRuleChecks,
501
- levelChecks,
502
- decidingLayer,
503
- decidingId,
504
- startTime
505
- ) : void 0
506
- );
507
- }
508
- const guardVerdict = checkGuards(event, eventText, world, guardChecks, guardsMatched);
509
- if (guardVerdict) {
510
- if (guardVerdict.status !== "ALLOW") {
511
- decidingLayer = "guard";
512
- decidingId = guardVerdict.ruleId;
513
- const intentRecord = {
514
- originalIntent: event.intent,
515
- 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",
516
- ruleApplied: guardVerdict.ruleId,
517
- enforcement: guardVerdict.status,
518
- modifiedTo: guardVerdict.modifiedTo,
519
- consequence: guardVerdict.consequence,
520
- reward: guardVerdict.reward
521
- };
522
- const verdict = buildVerdict(
523
- guardVerdict.status,
524
- guardVerdict.reason,
525
- guardVerdict.ruleId,
526
- void 0,
527
- world,
528
- level,
529
- invariantChecks,
530
- guardsMatched,
531
- rulesMatched,
532
- includeTrace ? buildTrace(
533
- invariantChecks,
534
- safetyChecks,
535
- planCheckResult,
536
- roleChecks,
537
- guardChecks,
538
- kernelRuleChecks,
539
- levelChecks,
540
- decidingLayer,
541
- decidingId,
542
- startTime
543
- ) : void 0
544
- );
545
- verdict.intentRecord = intentRecord;
546
- if (guardVerdict.consequence) verdict.consequence = guardVerdict.consequence;
547
- if (guardVerdict.reward) verdict.reward = guardVerdict.reward;
548
- return verdict;
549
- }
550
- }
551
- const kernelVerdict = checkKernelRules(eventText, world, kernelRuleChecks, rulesMatched);
552
- if (kernelVerdict) {
553
- decidingLayer = "kernel-rule";
554
- decidingId = kernelVerdict.ruleId;
555
- return buildVerdict(
556
- kernelVerdict.status,
557
- kernelVerdict.reason,
558
- kernelVerdict.ruleId,
559
- void 0,
560
- world,
561
- level,
562
- invariantChecks,
563
- guardsMatched,
564
- rulesMatched,
565
- includeTrace ? buildTrace(
566
- invariantChecks,
567
- safetyChecks,
568
- planCheckResult,
569
- roleChecks,
570
- guardChecks,
571
- kernelRuleChecks,
572
- levelChecks,
573
- decidingLayer,
574
- decidingId,
575
- startTime
576
- ) : void 0
577
- );
578
- }
579
- const levelVerdict = checkLevelConstraints(event, level, levelChecks);
580
- if (levelVerdict) {
581
- decidingLayer = "level-constraint";
582
- decidingId = levelVerdict.ruleId;
583
- return buildVerdict(
584
- levelVerdict.status,
585
- levelVerdict.reason,
586
- levelVerdict.ruleId,
587
- void 0,
588
- world,
589
- level,
590
- invariantChecks,
591
- guardsMatched,
592
- rulesMatched,
593
- includeTrace ? buildTrace(
594
- invariantChecks,
595
- safetyChecks,
596
- planCheckResult,
597
- roleChecks,
598
- guardChecks,
599
- kernelRuleChecks,
600
- levelChecks,
601
- decidingLayer,
602
- decidingId,
603
- startTime
604
- ) : void 0
605
- );
606
- }
607
- const warning = guardVerdict?.warning;
608
- return buildVerdict(
609
- "ALLOW",
610
- void 0,
611
- void 0,
612
- warning,
613
- world,
614
- level,
615
- invariantChecks,
616
- guardsMatched,
617
- rulesMatched,
618
- includeTrace ? buildTrace(
619
- invariantChecks,
620
- safetyChecks,
621
- planCheckResult,
622
- roleChecks,
623
- guardChecks,
624
- kernelRuleChecks,
625
- levelChecks,
626
- decidingLayer,
627
- decidingId,
628
- startTime
629
- ) : void 0
630
- );
631
- }
632
- function checkInvariantCoverage(world, checks) {
633
- const invariants = world.invariants ?? [];
634
- const guards = world.guards?.guards ?? [];
635
- for (const invariant of invariants) {
636
- const coveringGuard = guards.find(
637
- (g) => g.invariant_ref === invariant.id && g.immutable
638
- );
639
- checks.push({
640
- invariantId: invariant.id,
641
- label: invariant.label,
642
- hasGuardCoverage: !!coveringGuard,
643
- coveringGuardId: coveringGuard?.id
644
- });
645
- }
646
- }
647
- function checkSafety(event, eventText, checks) {
648
- const textToCheck = event.intent + (event.payload ? JSON.stringify(event.payload) : "");
649
- for (const { pattern, label } of PROMPT_INJECTION_PATTERNS) {
650
- const triggered = pattern.test(textToCheck);
651
- checks.push({
652
- checkType: "prompt-injection",
653
- triggered,
654
- matchedPattern: triggered ? label : void 0
655
- });
656
- if (triggered) {
657
- for (const remaining of PROMPT_INJECTION_PATTERNS.filter((p) => p.label !== label)) {
658
- checks.push({
659
- checkType: "prompt-injection",
660
- triggered: remaining.pattern.test(textToCheck),
661
- matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
662
- });
663
- }
664
- return {
665
- status: "PAUSE",
666
- reason: NEUTRAL_MESSAGES["prompt-injection"],
667
- ruleId: `safety-injection-${label}`
668
- };
669
- }
670
- }
671
- const scopeToCheck = event.scope ?? event.intent;
672
- for (const { pattern, label } of SCOPE_ESCAPE_PATTERNS) {
673
- const triggered = pattern.test(scopeToCheck);
674
- checks.push({
675
- checkType: "scope-escape",
676
- triggered,
677
- matchedPattern: triggered ? label : void 0
678
- });
679
- if (triggered) {
680
- for (const remaining of SCOPE_ESCAPE_PATTERNS.filter((p) => p.label !== label)) {
681
- checks.push({
682
- checkType: "scope-escape",
683
- triggered: remaining.pattern.test(scopeToCheck),
684
- matchedPattern: remaining.pattern.test(scopeToCheck) ? remaining.label : void 0
685
- });
686
- }
687
- return {
688
- status: "PAUSE",
689
- reason: NEUTRAL_MESSAGES["scope-escape"],
690
- ruleId: `safety-scope-${label}`
691
- };
692
- }
693
- }
694
- if (event.direction === "output") {
695
- for (const { pattern, label } of EXECUTION_CLAIM_PATTERNS) {
696
- const triggered = pattern.test(textToCheck);
697
- checks.push({
698
- checkType: "execution-claim",
699
- triggered,
700
- matchedPattern: triggered ? label : void 0
701
- });
702
- if (triggered) {
703
- for (const remaining of EXECUTION_CLAIM_PATTERNS.filter((p) => p.label !== label)) {
704
- checks.push({
705
- checkType: "execution-claim",
706
- triggered: remaining.pattern.test(textToCheck),
707
- matchedPattern: remaining.pattern.test(textToCheck) ? remaining.label : void 0
708
- });
709
- }
710
- return {
711
- status: "PAUSE",
712
- reason: NEUTRAL_MESSAGES["execution-claim"],
713
- ruleId: `safety-execution-claim-${label}`
714
- };
715
- }
716
- }
717
- }
718
- if (event.direction === "input") {
719
- const intentTrimmed = event.intent.trim();
720
- for (const { pattern, label } of EXECUTION_INTENT_PATTERNS) {
721
- const triggered = pattern.test(intentTrimmed);
722
- checks.push({
723
- checkType: "execution-intent",
724
- triggered,
725
- matchedPattern: triggered ? label : void 0
726
- });
727
- if (triggered) {
728
- for (const remaining of EXECUTION_INTENT_PATTERNS.filter((p) => p.label !== label)) {
729
- checks.push({
730
- checkType: "execution-intent",
731
- triggered: remaining.pattern.test(intentTrimmed),
732
- matchedPattern: remaining.pattern.test(intentTrimmed) ? remaining.label : void 0
733
- });
734
- }
735
- return {
736
- status: "PAUSE",
737
- reason: NEUTRAL_MESSAGES["execution-intent"],
738
- ruleId: `safety-execution-intent-${label}`
739
- };
740
- }
741
- }
742
- }
743
- return null;
744
- }
745
- function checkRoleRules(event, eventText, world, checks) {
746
- if (!event.roleId || !world.roles) return null;
747
- const role = world.roles.roles.find((r) => r.id === event.roleId);
748
- if (!role) return null;
749
- if (role.requiresApproval) {
750
- checks.push({
751
- roleId: role.id,
752
- roleName: role.name,
753
- rule: "All actions require approval",
754
- ruleType: "requiresApproval",
755
- matched: true
756
- });
757
- return {
758
- status: "PAUSE",
759
- reason: `Role "${role.name}" requires approval for all actions.`,
760
- ruleId: `role-${role.id}-requires-approval`
761
- };
762
- }
763
- for (const rule of role.cannotDo) {
764
- const matched = matchesKeywords(eventText, rule);
765
- checks.push({
766
- roleId: role.id,
767
- roleName: role.name,
768
- rule,
769
- ruleType: "cannotDo",
770
- matched
771
- });
772
- if (matched) {
773
- return {
774
- status: "BLOCK",
775
- reason: `Role "${role.name}" cannot: ${rule}`,
776
- ruleId: `role-${role.id}-cannotdo`
777
- };
778
- }
779
- }
780
- for (const rule of role.canDo) {
781
- checks.push({
782
- roleId: role.id,
783
- roleName: role.name,
784
- rule,
785
- ruleType: "canDo",
786
- matched: matchesKeywords(eventText, rule)
787
- });
788
- }
789
- return null;
790
- }
791
- function checkGuards(event, eventText, world, checks, guardsMatched) {
792
- if (!world.guards) return null;
793
- const guardsConfig = world.guards;
794
- let warnResult = null;
795
- const compiledPatterns = /* @__PURE__ */ new Map();
796
- for (const [key, def] of Object.entries(guardsConfig.intent_vocabulary)) {
797
- try {
798
- compiledPatterns.set(key, new RegExp(def.pattern, "i"));
799
- } catch {
800
- }
801
- }
802
- const eventTool = (event.tool ?? "").toLowerCase();
803
- for (const guard of guardsConfig.guards) {
804
- if (guard.appliesTo && guard.appliesTo.length > 0) {
805
- const normalizedAppliesTo = guard.appliesTo.map((t) => t.toLowerCase());
806
- if (!normalizedAppliesTo.includes(eventTool)) {
807
- continue;
808
- }
809
- }
810
- const enabled = guard.immutable || guard.default_enabled !== false;
811
- const matchedPatterns = [];
812
- for (const patternKey of guard.intent_patterns) {
813
- const regex = compiledPatterns.get(patternKey);
814
- if (regex?.test(eventText)) {
815
- matchedPatterns.push(patternKey);
816
- }
817
- }
818
- const matched = matchedPatterns.length > 0 && enabled;
819
- let roleGated = false;
820
- if (matched && guard.required_roles && guard.required_roles.length > 0 && event.roleId && guard.required_roles.includes(event.roleId)) {
821
- roleGated = true;
822
- }
823
- checks.push({
824
- guardId: guard.id,
825
- label: guard.label,
826
- category: guard.category,
827
- enabled,
828
- matched: matched && !roleGated,
829
- enforcement: guard.enforcement,
830
- matchedPatterns,
831
- roleGated
832
- });
833
- if (!matched || roleGated) continue;
834
- guardsMatched.push(guard.id);
835
- const actionMode = guard.player_modes?.action ?? guard.enforcement;
836
- const reason = guard.redirect ? `${guard.description} \u2014 ${guard.redirect}` : guard.description;
837
- if (actionMode === "block") {
838
- return { status: "BLOCK", reason, ruleId: `guard-${guard.id}` };
839
- }
840
- if (actionMode === "pause") {
841
- return { status: "PAUSE", reason, ruleId: `guard-${guard.id}` };
842
- }
843
- if (actionMode === "penalize") {
844
- const consequence = guard.consequence ? { ...guard.consequence } : { type: "freeze", rounds: 1, description: `Penalized for violating: ${guard.label}` };
845
- return { status: "PENALIZE", reason, ruleId: `guard-${guard.id}`, consequence };
846
- }
847
- if (actionMode === "reward") {
848
- const reward = guard.reward ? { ...guard.reward } : { type: "boost_influence", magnitude: 0.1, description: `Rewarded for: ${guard.label}` };
849
- return { status: "REWARD", reason, ruleId: `guard-${guard.id}`, reward };
850
- }
851
- if (actionMode === "modify") {
852
- const modifiedTo = guard.modify_to ?? guard.redirect ?? "hold";
853
- return { status: "MODIFY", reason: `${reason} \u2192 Modified to: ${modifiedTo}`, ruleId: `guard-${guard.id}`, modifiedTo };
854
- }
855
- if (actionMode === "neutral") {
856
- return { status: "NEUTRAL", reason, ruleId: `guard-${guard.id}` };
857
- }
858
- if (actionMode === "warn" && !warnResult) {
859
- warnResult = { status: "ALLOW", warning: reason, ruleId: `guard-${guard.id}` };
860
- }
861
- }
862
- return warnResult;
863
- }
864
- function checkKernelRules(eventText, world, checks, rulesMatched) {
865
- if (!world.kernel) return null;
866
- const forbidden = world.kernel.input_boundaries?.forbidden_patterns ?? [];
867
- const output = world.kernel.output_boundaries?.forbidden_patterns ?? [];
868
- for (const rule of forbidden) {
869
- let matched = false;
870
- let matchMethod = "none";
871
- if (rule.pattern) {
872
- try {
873
- matched = new RegExp(rule.pattern, "i").test(eventText);
874
- matchMethod = "pattern";
875
- } catch {
876
- }
877
- }
878
- if (!matched && rule.reason) {
879
- matched = matchesKeywords(eventText, rule.reason);
880
- if (matched) matchMethod = "keyword";
881
- }
882
- checks.push({
883
- ruleId: rule.id,
884
- text: rule.reason,
885
- category: "forbidden",
886
- matched,
887
- matchMethod
888
- });
889
- if (matched) {
890
- rulesMatched.push(rule.id);
891
- if (rule.action === "BLOCK") {
892
- return {
893
- status: "BLOCK",
894
- reason: rule.reason,
895
- ruleId: `kernel-${rule.id}`
896
- };
897
- }
898
- }
899
- }
900
- return null;
901
- }
902
- function checkLevelConstraints(event, level, checks) {
903
- if (level === "basic") return null;
904
- const intent = event.intent.toLowerCase();
905
- const tool = (event.tool ?? "").toLowerCase();
906
- const isDelete = intent.includes("delete") || intent.includes("remove") || intent.includes("rm ") || tool === "delete";
907
- const deleteTriggered = isDelete && levelRequiresConfirmation(level, "delete");
908
- checks.push({
909
- checkType: "delete",
910
- level,
911
- triggered: deleteTriggered,
912
- reason: deleteTriggered ? NEUTRAL_MESSAGES["delete"] : void 0
913
- });
914
- if (deleteTriggered) {
915
- return { status: "PAUSE", reason: NEUTRAL_MESSAGES["delete"], ruleId: "level-delete-check" };
916
- }
917
- const isExternal = event.scope ? isExternalScope(event.scope) : false;
918
- const externalTriggered = isExternal && levelRequiresConfirmation(level, "write-external");
919
- checks.push({
920
- checkType: "write-external",
921
- level,
922
- triggered: externalTriggered,
923
- reason: externalTriggered ? NEUTRAL_MESSAGES["write-external"] : void 0
924
- });
925
- if (externalTriggered) {
926
- return { status: "PAUSE", reason: NEUTRAL_MESSAGES["write-external"], ruleId: "level-external-write-check" };
927
- }
928
- const isNetwork = tool === "http" || tool === "fetch" || tool === "request" || intent.includes("post ") || intent.includes("sending");
929
- const networkTriggered = isNetwork && levelRequiresConfirmation(level, "network-mutate");
930
- checks.push({
931
- checkType: "network-mutate",
932
- level,
933
- triggered: networkTriggered,
934
- reason: networkTriggered ? NEUTRAL_MESSAGES["network-mutate"] : void 0
935
- });
936
- if (networkTriggered) {
937
- return { status: "PAUSE", reason: NEUTRAL_MESSAGES["network-mutate"], ruleId: "level-network-mutate-check" };
938
- }
939
- const isCredential = intent.includes("credential") || intent.includes("password") || intent.includes("secret") || intent.includes("api key") || intent.includes("token");
940
- const credentialTriggered = isCredential && levelRequiresConfirmation(level, "credential-access");
941
- checks.push({
942
- checkType: "credential-access",
943
- level,
944
- triggered: credentialTriggered,
945
- reason: credentialTriggered ? NEUTRAL_MESSAGES["credential-access"] : void 0
946
- });
947
- if (credentialTriggered) {
948
- return { status: "PAUSE", reason: NEUTRAL_MESSAGES["credential-access"], ruleId: "level-credential-check" };
949
- }
950
- const irreversibleTriggered = !!event.irreversible && level !== "basic";
951
- checks.push({
952
- checkType: "irreversible",
953
- level,
954
- triggered: irreversibleTriggered,
955
- reason: irreversibleTriggered ? "This action is marked as irreversible." : void 0
956
- });
957
- if (irreversibleTriggered) {
958
- return {
959
- status: "PAUSE",
960
- reason: "This action is marked as irreversible.",
961
- ruleId: "level-irreversible-check"
962
- };
963
- }
964
- return null;
965
- }
966
- function matchesKeywords(eventText, ruleText) {
967
- return matchesAllKeywords(eventText, ruleText);
968
- }
969
- function eventToAllowlistKey(event) {
970
- return `${(event.tool ?? "*").toLowerCase()}::${event.intent.toLowerCase().trim()}`;
971
- }
972
- function buildTrace(invariantChecks, safetyChecks, planCheck, roleChecks, guardChecks, kernelRuleChecks, levelChecks, decidingLayer, decidingId, startTime) {
973
- const trace = {
974
- invariantChecks,
975
- safetyChecks,
976
- roleChecks,
977
- guardChecks,
978
- kernelRuleChecks,
979
- levelChecks,
980
- precedenceResolution: {
981
- decidingLayer,
982
- decidingId,
983
- strategy: "first-match-wins",
984
- chainOrder: [
985
- "invariant-coverage",
986
- "session-allowlist",
987
- "safety-injection",
988
- "safety-scope-escape",
989
- "safety-execution-claim",
990
- "safety-execution-intent",
991
- "plan-enforcement",
992
- "role-rules",
993
- "declarative-guards",
994
- "kernel-rules",
995
- "level-constraints",
996
- "default-allow"
997
- ]
998
- },
999
- durationMs: performance.now() - startTime
1000
- };
1001
- if (planCheck) {
1002
- trace.planCheck = planCheck;
1003
- }
1004
- return trace;
1005
- }
1006
- function buildVerdict(status, reason, ruleId, warning, world, level, invariantChecks, guardsMatched, rulesMatched, trace) {
1007
- const evidence = {
1008
- worldId: world.world.world_id,
1009
- worldName: world.world.name,
1010
- worldVersion: world.world.version,
1011
- evaluatedAt: Date.now(),
1012
- invariantsSatisfied: invariantChecks.filter((c) => c.hasGuardCoverage).length,
1013
- invariantsTotal: invariantChecks.length,
1014
- guardsMatched,
1015
- rulesMatched,
1016
- enforcementLevel: level
1017
- };
1018
- const verdict = {
1019
- status,
1020
- evidence
1021
- };
1022
- if (reason) verdict.reason = reason;
1023
- if (ruleId) verdict.ruleId = ruleId;
1024
- if (warning) verdict.warning = warning;
1025
- if (trace) verdict.trace = trace;
1026
- return verdict;
1027
- }
1028
-
1029
- // src/loader/world-loader.ts
1030
- async function loadWorldFromDirectory(dirPath) {
1031
- const { readFile } = await import("fs/promises");
1032
- const { join } = await import("path");
1033
- const { readdirSync } = await import("fs");
1034
- async function readJson(filename) {
1035
- try {
1036
- const content = await readFile(join(dirPath, filename), "utf-8");
1037
- return JSON.parse(content);
1038
- } catch {
1039
- return void 0;
1040
- }
1041
- }
1042
- const worldJson = await readJson("world.json");
1043
- if (!worldJson) {
1044
- throw new Error(`Cannot read world.json in ${dirPath}`);
1045
- }
1046
- const invariantsJson = await readJson("invariants.json");
1047
- const assumptionsJson = await readJson("assumptions.json");
1048
- const stateSchemaJson = await readJson("state-schema.json");
1049
- const gatesJson = await readJson("gates.json");
1050
- const outcomesJson = await readJson("outcomes.json");
1051
- const guardsJson = await readJson("guards.json");
1052
- const rolesJson = await readJson("roles.json");
1053
- const kernelJson = await readJson("kernel.json");
1054
- const metadataJson = await readJson("metadata.json");
1055
- const rules = [];
1056
- try {
1057
- const rulesDir = join(dirPath, "rules");
1058
- const ruleFiles = readdirSync(rulesDir).filter((f) => f.endsWith(".json")).sort();
1059
- for (const file of ruleFiles) {
1060
- const content = await readFile(join(rulesDir, file), "utf-8");
1061
- rules.push(JSON.parse(content));
1062
- }
1063
- } catch {
1064
- }
1065
- return {
1066
- world: worldJson,
1067
- invariants: invariantsJson?.invariants ?? [],
1068
- assumptions: assumptionsJson ?? { profiles: {}, parameter_definitions: {} },
1069
- stateSchema: stateSchemaJson ?? { variables: {}, presets: {} },
1070
- rules,
1071
- gates: gatesJson ?? {
1072
- viability_classification: [],
1073
- structural_override: { description: "", enforcement: "mandatory" },
1074
- sustainability_threshold: 0,
1075
- collapse_visual: { background: "", text: "", border: "", label: "" }
1076
- },
1077
- outcomes: outcomesJson ?? {
1078
- computed_outcomes: [],
1079
- comparison_layout: { primary_card: "", status_badge: "", structural_indicators: [] }
1080
- },
1081
- guards: guardsJson,
1082
- roles: rolesJson,
1083
- kernel: kernelJson,
1084
- metadata: metadataJson ?? {
1085
- format_version: "1.0.0",
1086
- created_at: "",
1087
- last_modified: "",
1088
- authoring_method: "manual-authoring"
1089
- }
1090
- };
1091
- }
1092
- async function loadWorld(worldPath) {
1093
- const { stat } = await import("fs/promises");
1094
- const info = await stat(worldPath);
1095
- if (info.isDirectory()) {
1096
- return loadWorldFromDirectory(worldPath);
1097
- }
1098
- if (worldPath.endsWith(".nv-world.zip")) {
1099
- throw new Error(".nv-world.zip loading not yet implemented \u2014 use a world directory");
1100
- }
1101
- throw new Error(`Cannot load world from: ${worldPath} \u2014 expected a directory or .nv-world.zip`);
1102
- }
1103
-
1104
- // src/adapters/express.ts
1105
- function methodToCategory(method) {
1106
- switch (method.toUpperCase()) {
1107
- case "GET":
1108
- case "HEAD":
1109
- case "OPTIONS":
1110
- return "read";
1111
- case "POST":
1112
- case "PUT":
1113
- case "PATCH":
1114
- return "write";
1115
- case "DELETE":
1116
- return "delete";
1117
- default:
1118
- return "other";
1119
- }
1120
- }
1121
- function defaultMapRequest(req) {
1122
- const method = (req.method ?? "GET").toUpperCase();
1123
- const path = req.path ?? req.url ?? "/";
1124
- return {
1125
- intent: `${method} ${path}`,
1126
- tool: "http",
1127
- scope: path,
1128
- actionCategory: methodToCategory(method),
1129
- direction: "input",
1130
- args: {
1131
- method,
1132
- path,
1133
- ...req.params ?? {},
1134
- ...req.query ?? {}
1135
- }
1136
- };
1137
- }
1138
- function defaultOnBlock(verdict, _req, res, statusCode) {
1139
- const body = {
1140
- error: "Governance policy violation",
1141
- reason: verdict.reason ?? "Action not permitted",
1142
- ruleId: verdict.ruleId,
1143
- status: verdict.status
1144
- };
1145
- if (res.status && res.json) {
1146
- res.status(statusCode).json(body);
1147
- } else if (res.send) {
1148
- res.statusCode = statusCode;
1149
- res.send(JSON.stringify(body));
1150
- } else if (res.end) {
1151
- res.statusCode = statusCode;
1152
- res.end();
1153
- }
1154
- }
1155
- async function createGovernanceMiddleware(worldPath, options) {
1156
- const world = await loadWorld(worldPath);
1157
- return createGovernanceMiddlewareFromWorld(world, options);
1158
- }
1159
- function createGovernanceMiddlewareFromWorld(world, options = {}) {
1160
- const engineOptions = {
1161
- trace: options.trace ?? false,
1162
- level: options.level
1163
- };
1164
- const mapRequest = options.mapRequest ?? defaultMapRequest;
1165
- const blockStatusCode = options.blockStatusCode ?? 403;
1166
- return async function neuroVerseGovernance(req, res, next) {
1167
- try {
1168
- const event = mapRequest(req);
1169
- const verdict = evaluateGuard(event, world, engineOptions);
1170
- options.onEvaluate?.(verdict, event, req);
1171
- if (verdict.status === "ALLOW") {
1172
- next();
1173
- return;
1174
- }
1175
- if (verdict.status === "PAUSE") {
1176
- const approved = await options.onPause?.(verdict, req);
1177
- if (approved) {
1178
- next();
1179
- return;
1180
- }
1181
- }
1182
- if (options.onBlock) {
1183
- options.onBlock(verdict, req, res);
1184
- } else {
1185
- defaultOnBlock(verdict, req, res, blockStatusCode);
1186
- }
1187
- } catch (err) {
1188
- next(err);
1189
- }
1190
- };
1191
- }
1192
- // Annotate the CommonJS export names for ESM import in node:
1193
- 0 && (module.exports = {
1194
- createGovernanceMiddleware,
1195
- createGovernanceMiddlewareFromWorld
1196
- });