@neuroverseos/governance 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/.well-known/ai-plugin.json +34 -9
  2. package/AGENTS.md +72 -24
  3. package/README.md +343 -248
  4. package/dist/adapters/autoresearch.cjs +1345 -0
  5. package/dist/adapters/autoresearch.d.cts +111 -0
  6. package/dist/adapters/autoresearch.d.ts +111 -0
  7. package/dist/adapters/autoresearch.js +12 -0
  8. package/dist/adapters/deep-agents.cjs +1528 -0
  9. package/dist/adapters/deep-agents.d.cts +181 -0
  10. package/dist/adapters/deep-agents.d.ts +181 -0
  11. package/dist/adapters/deep-agents.js +17 -0
  12. package/dist/adapters/express.cjs +1253 -0
  13. package/dist/adapters/express.d.cts +66 -0
  14. package/dist/adapters/express.d.ts +66 -0
  15. package/dist/adapters/express.js +12 -0
  16. package/dist/adapters/index.cjs +2112 -0
  17. package/dist/adapters/index.d.cts +8 -0
  18. package/dist/adapters/index.d.ts +8 -0
  19. package/dist/adapters/index.js +68 -0
  20. package/dist/adapters/langchain.cjs +1315 -0
  21. package/dist/adapters/langchain.d.cts +89 -0
  22. package/dist/adapters/langchain.d.ts +89 -0
  23. package/dist/adapters/langchain.js +17 -0
  24. package/dist/adapters/openai.cjs +1345 -0
  25. package/dist/adapters/openai.d.cts +99 -0
  26. package/dist/adapters/openai.d.ts +99 -0
  27. package/dist/adapters/openai.js +17 -0
  28. package/dist/adapters/openclaw.cjs +1337 -0
  29. package/dist/adapters/openclaw.d.cts +99 -0
  30. package/dist/adapters/openclaw.d.ts +99 -0
  31. package/dist/adapters/openclaw.js +17 -0
  32. package/dist/add-ROOZLU62.js +314 -0
  33. package/dist/behavioral-MJO34S6Q.js +118 -0
  34. package/dist/bootstrap-CQRZVOXK.js +116 -0
  35. package/dist/bootstrap-emitter-Q7UIJZ2O.js +7 -0
  36. package/dist/bootstrap-parser-EEF36XDU.js +7 -0
  37. package/dist/browser.global.js +941 -0
  38. package/dist/build-QKOBBC23.js +341 -0
  39. package/dist/chunk-3WQLXYTP.js +91 -0
  40. package/dist/chunk-4FLICVVA.js +119 -0
  41. package/dist/chunk-4NGDRRQH.js +10 -0
  42. package/dist/chunk-5TPFNWRU.js +215 -0
  43. package/dist/chunk-5U2MQO5P.js +57 -0
  44. package/dist/chunk-6CZSKEY5.js +164 -0
  45. package/dist/chunk-6S5CFQXY.js +624 -0
  46. package/dist/chunk-7P3S7MAY.js +1090 -0
  47. package/dist/chunk-A5W4GNQO.js +130 -0
  48. package/dist/chunk-A7GKPPU7.js +226 -0
  49. package/dist/chunk-AKW5YVCE.js +96 -0
  50. package/dist/chunk-B6OXJLJ5.js +622 -0
  51. package/dist/chunk-BNKJPUPQ.js +113 -0
  52. package/dist/chunk-BQZMOEML.js +43 -0
  53. package/dist/chunk-CNSO6XW5.js +207 -0
  54. package/dist/chunk-CTZHONLA.js +135 -0
  55. package/dist/chunk-D2UCV5AK.js +326 -0
  56. package/dist/chunk-EMQDLDAF.js +458 -0
  57. package/dist/chunk-F66BVUYB.js +340 -0
  58. package/dist/chunk-G7DJ6VOD.js +101 -0
  59. package/dist/chunk-I3RRAYK2.js +11 -0
  60. package/dist/chunk-IS4WUH6Y.js +363 -0
  61. package/dist/chunk-MH7BT4VH.js +15 -0
  62. package/dist/chunk-O5ABKEA7.js +304 -0
  63. package/dist/chunk-OT6PXH54.js +61 -0
  64. package/dist/chunk-PVTQQS3Y.js +186 -0
  65. package/dist/chunk-Q6O7ZLO2.js +62 -0
  66. package/dist/chunk-QLPTHTVB.js +253 -0
  67. package/dist/chunk-QWGCMQQD.js +16 -0
  68. package/dist/chunk-QXBFT7NI.js +201 -0
  69. package/dist/chunk-TG6SEF24.js +246 -0
  70. package/dist/chunk-U6U7EJZL.js +177 -0
  71. package/dist/chunk-W7LLXRGY.js +830 -0
  72. package/dist/chunk-ZJTDUCC2.js +194 -0
  73. package/dist/chunk-ZWI3NIXK.js +314 -0
  74. package/dist/cli/neuroverse.cjs +14191 -0
  75. package/dist/cli/neuroverse.d.cts +1 -0
  76. package/dist/cli/neuroverse.d.ts +1 -0
  77. package/dist/cli/neuroverse.js +227 -0
  78. package/dist/cli/plan.cjs +2439 -0
  79. package/dist/cli/plan.d.cts +20 -0
  80. package/dist/cli/plan.d.ts +20 -0
  81. package/dist/cli/plan.js +353 -0
  82. package/dist/cli/run.cjs +2001 -0
  83. package/dist/cli/run.d.cts +20 -0
  84. package/dist/cli/run.d.ts +20 -0
  85. package/dist/cli/run.js +143 -0
  86. package/dist/configure-ai-6TZ3MCSI.js +132 -0
  87. package/dist/decision-flow-M63D47LO.js +61 -0
  88. package/dist/demo-G43RLCPK.js +469 -0
  89. package/dist/derive-FJZVIPUZ.js +153 -0
  90. package/dist/doctor-6BC6X2VO.js +173 -0
  91. package/dist/equity-penalties-SG5IZQ7I.js +244 -0
  92. package/dist/explain-RHBU2GBR.js +51 -0
  93. package/dist/guard-AJCCGZMF.js +92 -0
  94. package/dist/guard-contract-DqFcTScd.d.cts +821 -0
  95. package/dist/guard-contract-DqFcTScd.d.ts +821 -0
  96. package/dist/guard-engine-PNR6MHCM.js +10 -0
  97. package/dist/impact-3XVDSCBU.js +59 -0
  98. package/dist/improve-TQP4ECSY.js +66 -0
  99. package/dist/index.cjs +7591 -0
  100. package/dist/index.d.cts +2195 -0
  101. package/dist/index.d.ts +2195 -0
  102. package/dist/index.js +472 -0
  103. package/dist/infer-world-IFXCACJ5.js +543 -0
  104. package/dist/init-FYPV4SST.js +144 -0
  105. package/dist/init-world-TI7ARHBT.js +223 -0
  106. package/dist/mcp-server-5Y3ZM7TV.js +13 -0
  107. package/dist/model-adapter-VXEKB4LS.js +11 -0
  108. package/dist/playground-VZBNPPBO.js +560 -0
  109. package/dist/redteam-MZPZD3EF.js +357 -0
  110. package/dist/session-JYOARW54.js +15 -0
  111. package/dist/shared-7RLUHNMU.js +16 -0
  112. package/dist/shared-B8dvUUD8.d.cts +60 -0
  113. package/dist/shared-Dr5Wiay8.d.ts +60 -0
  114. package/dist/simulate-LJXYBC6M.js +83 -0
  115. package/dist/test-BOOR4A5F.js +217 -0
  116. package/dist/trace-PKV4KX56.js +166 -0
  117. package/dist/validate-RALX7CZS.js +81 -0
  118. package/dist/validate-engine-7ZXFVGF2.js +7 -0
  119. package/dist/viz/assets/index-B8SaeJZZ.js +23 -0
  120. package/dist/viz/index.html +23 -0
  121. package/dist/world-BIP4GZBZ.js +376 -0
  122. package/dist/world-loader-Y6HMQH2D.js +13 -0
  123. package/dist/worlds/autoresearch.nv-world.md +230 -0
  124. package/dist/worlds/coding-agent.nv-world.md +211 -0
  125. package/dist/worlds/derivation-world.nv-world.md +278 -0
  126. package/dist/worlds/research-agent.nv-world.md +169 -0
  127. package/dist/worlds/social-media.nv-world.md +198 -0
  128. package/dist/worlds/trading-agent.nv-world.md +218 -0
  129. package/examples/social-media-sim/bridge.py +209 -0
  130. package/examples/social-media-sim/simulation.py +927 -0
  131. package/package.json +16 -3
  132. package/simulate.html +4 -336
@@ -0,0 +1,341 @@
1
+ import {
2
+ DeriveInputError,
3
+ DeriveProviderError,
4
+ deriveWorld
5
+ } from "./chunk-6S5CFQXY.js";
6
+ import {
7
+ DERIVE_EXIT_CODES
8
+ } from "./chunk-Q6O7ZLO2.js";
9
+ import "./chunk-OT6PXH54.js";
10
+ import "./chunk-7P3S7MAY.js";
11
+ import {
12
+ parseWorldMarkdown
13
+ } from "./chunk-EMQDLDAF.js";
14
+ import {
15
+ emitWorldDefinition
16
+ } from "./chunk-PVTQQS3Y.js";
17
+ import "./chunk-QWGCMQQD.js";
18
+
19
+ // src/cli/build.ts
20
+ var FINDING_LABELS = {
21
+ "guard-coverage": "STORY INSIGHT",
22
+ "semantic-tension": "DRAMATIC TENSION",
23
+ "orphan": "MISSING CAUSALITY",
24
+ "contradiction": "CONFLICT DETECTED",
25
+ "referential-integrity": "BROKEN REFERENCE",
26
+ "completeness": "INCOMPLETE STRUCTURE",
27
+ "schema-violation": "VALUE OUT OF RANGE"
28
+ };
29
+ function humanLabel(section) {
30
+ const category = section.replace(/^Validate:/, "");
31
+ return FINDING_LABELS[category] ?? section.toUpperCase();
32
+ }
33
+ function traceCausalChains(rules) {
34
+ const effectToTrigger = /* @__PURE__ */ new Map();
35
+ for (const rule of rules) {
36
+ for (const trigger of rule.triggers) {
37
+ if (!effectToTrigger.has(trigger.field)) effectToTrigger.set(trigger.field, /* @__PURE__ */ new Set());
38
+ effectToTrigger.get(trigger.field).add(rule.id);
39
+ }
40
+ }
41
+ const chains = [];
42
+ const ruleById = new Map(rules.map((r) => [r.id, r]));
43
+ for (const startRule of rules) {
44
+ const chain = [startRule.effects[0]?.target].filter(Boolean);
45
+ if (chain.length === 0) continue;
46
+ const visited = /* @__PURE__ */ new Set([startRule.id]);
47
+ let current = startRule;
48
+ for (let depth = 0; depth < 5; depth++) {
49
+ const lastTarget = current.effects[0]?.target;
50
+ if (!lastTarget) break;
51
+ const triggeredRuleIds = effectToTrigger.get(lastTarget);
52
+ if (!triggeredRuleIds) break;
53
+ let nextRule;
54
+ for (const id of triggeredRuleIds) {
55
+ if (!visited.has(id)) {
56
+ nextRule = ruleById.get(id);
57
+ break;
58
+ }
59
+ }
60
+ if (!nextRule) break;
61
+ visited.add(nextRule.id);
62
+ const nextTarget = nextRule.effects[0]?.target;
63
+ if (nextTarget && !chain.includes(nextTarget)) {
64
+ chain.push(nextTarget);
65
+ }
66
+ current = nextRule;
67
+ }
68
+ if (chain.length >= 2) {
69
+ chains.push(chain);
70
+ }
71
+ }
72
+ const sorted = chains.sort((a, b) => b.length - a.length);
73
+ const unique = [];
74
+ for (const chain of sorted) {
75
+ const key = chain.join(" -> ");
76
+ const isSubset = unique.some((existing) => existing.join(" -> ").includes(key));
77
+ if (!isSubset) unique.push(chain);
78
+ }
79
+ return unique.slice(0, 3);
80
+ }
81
+ function renderGovernanceHealth(health) {
82
+ const lines = [];
83
+ lines.push("GOVERNANCE HEALTH");
84
+ lines.push(` Coverage: ${health.surfacesCovered} / ${health.surfacesTotal} action surfaces governed`);
85
+ lines.push(` Invariants enforced: ${health.invariantsEnforced} / ${health.invariantsTotal}`);
86
+ if (health.shadowedGuards > 0) {
87
+ lines.push(` Shadowed guards: ${health.shadowedGuards}`);
88
+ }
89
+ if (health.unenforcedInvariants > 0) {
90
+ lines.push(` Unenforced invariants: ${health.unenforcedInvariants}`);
91
+ }
92
+ if (health.unreachableRules > 0) {
93
+ lines.push(` Unreachable rules: ${health.unreachableRules}`);
94
+ }
95
+ if (health.incompleteStateCoverage > 0) {
96
+ lines.push(` Incomplete state coverage: ${health.incompleteStateCoverage}`);
97
+ }
98
+ if (health.surfaces.length > 0) {
99
+ lines.push("");
100
+ lines.push(" Surfaces:");
101
+ for (const s of health.surfaces) {
102
+ lines.push(` ${s.name}: ${s.governed ? "governed" : "unguarded"}`);
103
+ }
104
+ }
105
+ lines.push("");
106
+ lines.push(` Risk level: ${health.riskLevel.charAt(0).toUpperCase() + health.riskLevel.slice(1)}`);
107
+ return lines.join("\n");
108
+ }
109
+ function parseArgs(argv) {
110
+ let inputPath = "";
111
+ let outputDir;
112
+ let validate = true;
113
+ let dryRun = false;
114
+ let provider;
115
+ let model;
116
+ let endpoint;
117
+ for (let i = 0; i < argv.length; i++) {
118
+ const arg = argv[i];
119
+ if (arg === "--output" && i + 1 < argv.length) {
120
+ outputDir = argv[++i];
121
+ } else if (arg === "--no-validate") {
122
+ validate = false;
123
+ } else if (arg === "--dry-run") {
124
+ dryRun = true;
125
+ } else if (arg === "--provider" && i + 1 < argv.length) {
126
+ provider = argv[++i];
127
+ } else if (arg === "--model" && i + 1 < argv.length) {
128
+ model = argv[++i];
129
+ } else if (arg === "--endpoint" && i + 1 < argv.length) {
130
+ endpoint = argv[++i];
131
+ } else if (!arg.startsWith("--") && !inputPath) {
132
+ inputPath = arg;
133
+ }
134
+ }
135
+ if (!inputPath) throw new DeriveInputError("Usage: neuroverse build <input.md> [--output <dir>]");
136
+ return { inputPath, outputDir, validate, dryRun, provider, model, endpoint };
137
+ }
138
+ async function writeWorldFiles(outputDir, world) {
139
+ const { writeFile, mkdir } = await import("fs/promises");
140
+ const { join } = await import("path");
141
+ await mkdir(outputDir, { recursive: true });
142
+ await writeFile(join(outputDir, "world.json"), JSON.stringify(world.world, null, 2));
143
+ await writeFile(join(outputDir, "invariants.json"), JSON.stringify({ invariants: world.invariants }, null, 2));
144
+ await writeFile(join(outputDir, "assumptions.json"), JSON.stringify(world.assumptions, null, 2));
145
+ await writeFile(join(outputDir, "state-schema.json"), JSON.stringify(world.stateSchema, null, 2));
146
+ await writeFile(join(outputDir, "gates.json"), JSON.stringify(world.gates, null, 2));
147
+ await writeFile(join(outputDir, "outcomes.json"), JSON.stringify(world.outcomes, null, 2));
148
+ await writeFile(join(outputDir, "metadata.json"), JSON.stringify(world.metadata, null, 2));
149
+ const rulesDir = join(outputDir, "rules");
150
+ await mkdir(rulesDir, { recursive: true });
151
+ const sortedRules = [...world.rules].sort((a, b) => a.order - b.order);
152
+ for (let i = 0; i < sortedRules.length; i++) {
153
+ const ruleNum = String(i + 1).padStart(3, "0");
154
+ await writeFile(join(rulesDir, `rule-${ruleNum}.json`), JSON.stringify(sortedRules[i], null, 2));
155
+ }
156
+ }
157
+ function write(msg) {
158
+ process.stderr.write(msg);
159
+ }
160
+ function printInsight(finding) {
161
+ const label = humanLabel(finding.section);
162
+ const icon = finding.severity === "error" ? "x" : "!";
163
+ write(` ${icon} ${label}: ${finding.message}
164
+ `);
165
+ }
166
+ async function main(argv = process.argv.slice(2)) {
167
+ try {
168
+ const args = parseArgs(argv);
169
+ const { basename } = await import("path");
170
+ write(`
171
+ NeuroVerse World Builder
172
+ `);
173
+ write(`
174
+ Analyzing: ${basename(args.inputPath)}
175
+ `);
176
+ const derivedPath = args.outputDir ? `${args.outputDir}/source.nv-world.md` : ".neuroverse/build-output.nv-world.md";
177
+ const { mkdir } = await import("fs/promises");
178
+ const { dirname } = await import("path");
179
+ await mkdir(dirname(derivedPath), { recursive: true });
180
+ const { result, exitCode, dryRunOutput } = await deriveWorld({
181
+ inputPath: args.inputPath,
182
+ outputPath: derivedPath,
183
+ validate: args.validate,
184
+ dryRun: args.dryRun,
185
+ providerOverride: args.provider || args.model || args.endpoint ? { provider: args.provider, model: args.model, endpoint: args.endpoint } : void 0
186
+ });
187
+ if (dryRunOutput) {
188
+ process.stdout.write(JSON.stringify({
189
+ dryRun: true,
190
+ systemPrompt: dryRunOutput.systemPrompt,
191
+ userPrompt: dryRunOutput.userPrompt,
192
+ durationMs: result.durationMs
193
+ }, null, 2) + "\n");
194
+ process.exit(DERIVE_EXIT_CODES.SUCCESS);
195
+ return;
196
+ }
197
+ const { readFile } = await import("fs/promises");
198
+ const derivedContent = await readFile(derivedPath, "utf-8");
199
+ const parseResult = parseWorldMarkdown(derivedContent);
200
+ const worldName = parseResult.world?.frontmatter?.name ?? "Derived World";
201
+ const worldId = parseResult.world?.frontmatter?.world_id ?? "derived_world";
202
+ const thesis = parseResult.world?.thesis ?? "";
203
+ write(`
204
+ World: ${worldName}
205
+ `);
206
+ if (thesis) {
207
+ const firstLine = thesis.split(/[.\n]/)[0].trim();
208
+ if (firstLine) write(`Theme: ${firstLine}
209
+ `);
210
+ }
211
+ const parsedRules = parseResult.world?.rules ?? [];
212
+ if (parsedRules.length > 0) {
213
+ const chains = traceCausalChains(parsedRules);
214
+ if (chains.length > 0) {
215
+ write(`
216
+ Core dynamics:
217
+ `);
218
+ for (const chain of chains) {
219
+ write(` ${chain.join(" -> ")}
220
+ `);
221
+ }
222
+ }
223
+ }
224
+ write(`
225
+ Structure:
226
+ `);
227
+ const sectionLabels = {
228
+ frontmatter: "Metadata",
229
+ thesis: "Thesis",
230
+ invariants: "Invariants",
231
+ state: "State variables",
232
+ assumptions: "Assumptions",
233
+ rules: "Rules",
234
+ gates: "Gates",
235
+ outcomes: "Outcomes"
236
+ };
237
+ for (const [key, label] of Object.entries(sectionLabels)) {
238
+ if (result.sectionsDetected.includes(key)) {
239
+ const val = parseResult.world?.[key];
240
+ const count = Array.isArray(val) ? ` (${val.length})` : "";
241
+ write(` + ${label}${count}
242
+ `);
243
+ }
244
+ }
245
+ if (result.normalization) {
246
+ const n = result.normalization;
247
+ write(`
248
+ Auto-corrections: ${n.fixCount}
249
+ `);
250
+ if (n.invariantIds > 0) write(` - ${n.invariantIds} invariant ID(s) formatted
251
+ `);
252
+ if (n.gateThresholds > 0) write(` - ${n.gateThresholds} gate threshold(s) converted
253
+ `);
254
+ if (n.triggerTags > 0) write(` - ${n.triggerTags} trigger(s) tagged
255
+ `);
256
+ }
257
+ const errors = result.findings.filter((f) => f.severity === "error");
258
+ const warnings = result.findings.filter((f) => f.severity === "warning");
259
+ if (warnings.length > 0) {
260
+ write(`
261
+ Insights:
262
+ `);
263
+ for (const w of warnings) printInsight(w);
264
+ }
265
+ if (errors.length > 0) {
266
+ write(`
267
+ Issues:
268
+ `);
269
+ for (const e of errors) printInsight(e);
270
+ }
271
+ if (exitCode !== 0 || !parseResult.world) {
272
+ write(`
273
+ World source written to: ${derivedPath}
274
+ `);
275
+ write(`Fix the issues above, then run: neuroverse build ${derivedPath}
276
+
277
+ `);
278
+ process.exit(exitCode);
279
+ return;
280
+ }
281
+ const outputDir = args.outputDir ?? `.neuroverse/worlds/${worldId}`;
282
+ try {
283
+ const emitResult = emitWorldDefinition(parseResult.world);
284
+ await writeWorldFiles(outputDir, emitResult.world);
285
+ const { join } = await import("path");
286
+ const { copyFile } = await import("fs/promises");
287
+ const sourceDest = join(outputDir, "source.nv-world.md");
288
+ if (derivedPath !== sourceDest) {
289
+ await copyFile(derivedPath, sourceDest);
290
+ }
291
+ write(`
292
+ World ready: ${worldId}
293
+ `);
294
+ write(` ${outputDir}/
295
+ `);
296
+ } catch (bootstrapError) {
297
+ write(`
298
+ World source written to: ${derivedPath}
299
+ `);
300
+ write(`Bootstrap failed: ${bootstrapError instanceof Error ? bootstrapError.message : String(bootstrapError)}
301
+ `);
302
+ write(`Run: neuroverse bootstrap --input ${derivedPath} --output <dir>
303
+
304
+ `);
305
+ process.exit(1);
306
+ return;
307
+ }
308
+ write(`
309
+ Next steps:
310
+ `);
311
+ write(` Explore neuroverse explain ${worldId}
312
+ `);
313
+ write(` Simulate neuroverse simulate ${worldId}
314
+ `);
315
+ write(` Improve neuroverse improve ${worldId}
316
+ `);
317
+ write(`
318
+ `);
319
+ process.exit(0);
320
+ } catch (e) {
321
+ if (e instanceof DeriveInputError) {
322
+ process.stderr.write(`Input error: ${e.message}
323
+ `);
324
+ process.exit(DERIVE_EXIT_CODES.INPUT_ERROR);
325
+ } else if (e instanceof DeriveProviderError) {
326
+ process.stderr.write(`Provider error: ${e.message}
327
+ `);
328
+ process.exit(DERIVE_EXIT_CODES.PROVIDER_ERROR);
329
+ } else {
330
+ process.stderr.write(`Build failed: ${e instanceof Error ? e.message : String(e)}
331
+ `);
332
+ process.exit(DERIVE_EXIT_CODES.PROVIDER_ERROR);
333
+ }
334
+ }
335
+ }
336
+ export {
337
+ humanLabel,
338
+ main,
339
+ renderGovernanceHealth,
340
+ traceCausalChains
341
+ };
@@ -0,0 +1,91 @@
1
+ import {
2
+ GovernanceBlockedError,
3
+ buildEngineOptions,
4
+ extractScope,
5
+ trackPlanProgress
6
+ } from "./chunk-5U2MQO5P.js";
7
+ import {
8
+ evaluateGuard
9
+ } from "./chunk-W7LLXRGY.js";
10
+ import {
11
+ loadWorld
12
+ } from "./chunk-CTZHONLA.js";
13
+
14
+ // src/adapters/langchain.ts
15
+ var GovernanceBlockedError2 = class extends GovernanceBlockedError {
16
+ event;
17
+ constructor(verdict, event) {
18
+ super(verdict);
19
+ this.event = event;
20
+ }
21
+ };
22
+ function defaultMapToolCall(toolName, toolInput) {
23
+ return {
24
+ intent: toolName,
25
+ tool: toolName,
26
+ scope: extractScope(toolInput),
27
+ args: toolInput,
28
+ direction: "input"
29
+ };
30
+ }
31
+ var NeuroVerseCallbackHandler = class {
32
+ name = "NeuroVerseGovernance";
33
+ world;
34
+ options;
35
+ engineOptions;
36
+ mapToolCall;
37
+ activePlan;
38
+ constructor(world, options = {}) {
39
+ this.world = world;
40
+ this.options = options;
41
+ this.activePlan = options.plan;
42
+ this.engineOptions = buildEngineOptions(options, this.activePlan);
43
+ this.mapToolCall = options.mapToolCall ?? defaultMapToolCall;
44
+ }
45
+ /**
46
+ * Called before a tool is executed.
47
+ * Evaluates the tool call against the governance world.
48
+ *
49
+ * @throws GovernanceBlockedError if the action is BLOCKED
50
+ * @throws GovernanceBlockedError if the action is PAUSED and onPause returns false
51
+ */
52
+ async handleToolStart(tool, input) {
53
+ let parsedInput;
54
+ try {
55
+ parsedInput = typeof input === "string" ? JSON.parse(input) : input;
56
+ } catch {
57
+ parsedInput = { raw: input };
58
+ }
59
+ const event = this.mapToolCall(tool.name, parsedInput);
60
+ this.engineOptions.plan = this.activePlan;
61
+ const verdict = evaluateGuard(event, this.world, this.engineOptions);
62
+ this.options.onEvaluate?.(verdict, event);
63
+ if (verdict.status === "BLOCK") {
64
+ this.options.onBlock?.(verdict, event);
65
+ throw new GovernanceBlockedError2(verdict, event);
66
+ }
67
+ if (verdict.status === "PAUSE") {
68
+ const approved = await this.options.onPause?.(verdict, event);
69
+ if (!approved) {
70
+ throw new GovernanceBlockedError2(verdict, event);
71
+ }
72
+ }
73
+ if (verdict.status === "ALLOW") {
74
+ trackPlanProgress(event, this, this.options);
75
+ }
76
+ }
77
+ };
78
+ async function createNeuroVerseCallbackHandler(worldPath, options) {
79
+ const world = await loadWorld(worldPath);
80
+ return new NeuroVerseCallbackHandler(world, options);
81
+ }
82
+ function createNeuroVerseCallbackHandlerFromWorld(world, options) {
83
+ return new NeuroVerseCallbackHandler(world, options);
84
+ }
85
+
86
+ export {
87
+ GovernanceBlockedError2 as GovernanceBlockedError,
88
+ NeuroVerseCallbackHandler,
89
+ createNeuroVerseCallbackHandler,
90
+ createNeuroVerseCallbackHandlerFromWorld
91
+ };
@@ -0,0 +1,119 @@
1
+ import {
2
+ GovernanceBlockedError,
3
+ buildEngineOptions,
4
+ defaultBlockMessage,
5
+ extractScope,
6
+ trackPlanProgress
7
+ } from "./chunk-5U2MQO5P.js";
8
+ import {
9
+ evaluateGuard
10
+ } from "./chunk-W7LLXRGY.js";
11
+ import {
12
+ loadWorld
13
+ } from "./chunk-CTZHONLA.js";
14
+
15
+ // src/adapters/openai.ts
16
+ var GovernanceBlockedError2 = class extends GovernanceBlockedError {
17
+ toolCallId;
18
+ constructor(verdict, toolCallId) {
19
+ super(verdict);
20
+ this.toolCallId = toolCallId;
21
+ }
22
+ };
23
+ function defaultMapFunctionCall(name, args) {
24
+ return {
25
+ intent: name,
26
+ tool: name,
27
+ scope: extractScope(args),
28
+ args,
29
+ direction: "input"
30
+ };
31
+ }
32
+ var GovernedToolExecutor = class {
33
+ world;
34
+ options;
35
+ engineOptions;
36
+ mapFn;
37
+ blockMsg;
38
+ activePlan;
39
+ constructor(world, options = {}) {
40
+ this.world = world;
41
+ this.options = options;
42
+ this.activePlan = options.plan;
43
+ this.engineOptions = buildEngineOptions(options, this.activePlan);
44
+ this.mapFn = options.mapFunctionCall ?? defaultMapFunctionCall;
45
+ this.blockMsg = options.blockMessage ?? defaultBlockMessage;
46
+ }
47
+ /**
48
+ * Evaluate a single tool call against governance rules.
49
+ * Returns the verdict without executing the tool.
50
+ */
51
+ evaluate(toolCall) {
52
+ let args;
53
+ try {
54
+ args = JSON.parse(toolCall.function.arguments);
55
+ } catch {
56
+ args = { raw: toolCall.function.arguments };
57
+ }
58
+ const event = this.mapFn(toolCall.function.name, args);
59
+ this.engineOptions.plan = this.activePlan;
60
+ const verdict = evaluateGuard(event, this.world, this.engineOptions);
61
+ this.options.onEvaluate?.(verdict, event);
62
+ if (verdict.status === "ALLOW") {
63
+ trackPlanProgress(event, this, this.options);
64
+ }
65
+ return verdict;
66
+ }
67
+ /**
68
+ * Execute a tool call with governance enforcement.
69
+ *
70
+ * If ALLOW: runs the tool and returns the result.
71
+ * If BLOCK: returns a blocked message (no execution).
72
+ * If PAUSE: throws — caller must handle approval flow.
73
+ *
74
+ * @param toolCall - The OpenAI tool call to evaluate
75
+ * @param runner - The actual tool execution function
76
+ * @returns A tool result message ready for the OpenAI API
77
+ */
78
+ async execute(toolCall, runner) {
79
+ const verdict = this.evaluate(toolCall);
80
+ if (verdict.status === "BLOCK") {
81
+ return {
82
+ tool_call_id: toolCall.id,
83
+ role: "tool",
84
+ content: this.blockMsg(verdict),
85
+ _verdict: verdict
86
+ };
87
+ }
88
+ if (verdict.status === "PAUSE") {
89
+ throw new GovernanceBlockedError2(verdict, toolCall.id);
90
+ }
91
+ let args;
92
+ try {
93
+ args = JSON.parse(toolCall.function.arguments);
94
+ } catch {
95
+ args = { raw: toolCall.function.arguments };
96
+ }
97
+ const content = await runner(toolCall.function.name, args);
98
+ return {
99
+ tool_call_id: toolCall.id,
100
+ role: "tool",
101
+ content,
102
+ _verdict: verdict
103
+ };
104
+ }
105
+ };
106
+ async function createGovernedToolExecutor(worldPath, options) {
107
+ const world = await loadWorld(worldPath);
108
+ return new GovernedToolExecutor(world, options);
109
+ }
110
+ function createGovernedToolExecutorFromWorld(world, options) {
111
+ return new GovernedToolExecutor(world, options);
112
+ }
113
+
114
+ export {
115
+ GovernanceBlockedError2 as GovernanceBlockedError,
116
+ GovernedToolExecutor,
117
+ createGovernedToolExecutor,
118
+ createGovernedToolExecutorFromWorld
119
+ };
@@ -0,0 +1,10 @@
1
+ // src/contracts/bootstrap-contract.ts
2
+ var BOOTSTRAP_EXIT_CODES = {
3
+ SUCCESS: 0,
4
+ FAIL: 1,
5
+ ERROR: 3
6
+ };
7
+
8
+ export {
9
+ BOOTSTRAP_EXIT_CODES
10
+ };