@neuroverseos/governance 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +190 -0
- package/README.md +472 -115
- package/dist/adapters/express.cjs +874 -0
- package/dist/adapters/express.d.cts +66 -0
- package/dist/adapters/express.d.ts +66 -0
- package/dist/adapters/express.js +10 -0
- package/dist/adapters/index.cjs +1166 -0
- package/dist/adapters/index.d.cts +5 -0
- package/dist/adapters/index.d.ts +5 -0
- package/dist/adapters/index.js +40 -0
- package/dist/adapters/langchain.cjs +861 -0
- package/dist/adapters/langchain.d.cts +82 -0
- package/dist/adapters/langchain.d.ts +82 -0
- package/dist/adapters/langchain.js +14 -0
- package/dist/adapters/openai.cjs +891 -0
- package/dist/adapters/openai.d.cts +92 -0
- package/dist/adapters/openai.d.ts +92 -0
- package/dist/adapters/openai.js +14 -0
- package/dist/adapters/openclaw.cjs +883 -0
- package/dist/adapters/openclaw.d.cts +92 -0
- package/dist/adapters/openclaw.d.ts +92 -0
- package/dist/adapters/openclaw.js +14 -0
- package/dist/bootstrap-H4HHKQ5G.js +113 -0
- package/dist/build-73KAVHEY.js +308 -0
- package/dist/chunk-4NGDRRQH.js +10 -0
- package/dist/chunk-B4NF3OLW.js +673 -0
- package/dist/chunk-BUWWN2NX.js +192 -0
- package/dist/chunk-CROPZ75A.js +107 -0
- package/dist/chunk-D7BGWV2J.js +620 -0
- package/dist/chunk-EIUHJXBB.js +340 -0
- package/dist/chunk-EQXFOKH2.js +85 -0
- package/dist/chunk-FYPYZFV5.js +187 -0
- package/dist/chunk-FYS2CBUW.js +304 -0
- package/dist/chunk-FZQCRGUU.js +115 -0
- package/dist/chunk-I3RRAYK2.js +11 -0
- package/dist/chunk-ITJ3LCPG.js +225 -0
- package/dist/chunk-M3TZFGHO.js +79 -0
- package/dist/chunk-MWDQ4MJB.js +11 -0
- package/dist/chunk-O5OMJMIE.js +590 -0
- package/dist/chunk-T4X42QXC.js +121 -0
- package/dist/chunk-XPDMYECO.js +642 -0
- package/dist/chunk-Z2S2HIV5.js +100 -0
- package/dist/cli/neuroverse.cjs +5864 -0
- package/dist/cli/neuroverse.d.cts +1 -0
- package/dist/cli/neuroverse.d.ts +1 -0
- package/dist/cli/neuroverse.js +128 -0
- package/dist/configure-ai-46JVG56I.js +129 -0
- package/dist/derive-6NAEWLM5.js +150 -0
- package/dist/explain-3B3VB6TL.js +69 -0
- package/dist/guard-67Y66P3I.js +82 -0
- package/dist/guard-contract-D_RQz9kt.d.cts +529 -0
- package/dist/guard-contract-D_RQz9kt.d.ts +529 -0
- package/dist/impact-CHERK3O6.js +57 -0
- package/dist/improve-YG6I6ERG.js +84 -0
- package/dist/index.cjs +4227 -0
- package/dist/index.d.cts +1147 -0
- package/dist/index.d.ts +1139 -25
- package/dist/index.js +318 -1159
- package/dist/init-Z66T6TDI.js +142 -0
- package/dist/simulate-ETHHINZ4.js +107 -0
- package/dist/trace-3YODSSIP.js +164 -0
- package/dist/validate-UVE6GKQU.js +72 -0
- package/dist/validate-engine-657D75OG.js +6 -0
- package/dist/world-WLNHL5XC.js +298 -0
- package/dist/worlds/derivation-world.nv-world.md +278 -0
- package/package.json +74 -26
- package/LICENSE +0 -21
- package/dist/audit-logger.d.ts +0 -38
- package/dist/audit-logger.d.ts.map +0 -1
- package/dist/audit-logger.js +0 -100
- package/dist/audit-logger.js.map +0 -1
- package/dist/condition-engine.d.ts +0 -16
- package/dist/condition-engine.d.ts.map +0 -1
- package/dist/condition-engine.js +0 -186
- package/dist/condition-engine.js.map +0 -1
- package/dist/drift-monitor.d.ts +0 -81
- package/dist/drift-monitor.d.ts.map +0 -1
- package/dist/drift-monitor.js +0 -228
- package/dist/drift-monitor.js.map +0 -1
- package/dist/governance-engine.d.ts +0 -131
- package/dist/governance-engine.d.ts.map +0 -1
- package/dist/governance-engine.js +0 -660
- package/dist/governance-engine.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/types.d.ts +0 -196
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -9
- package/dist/types.js.map +0 -1
- package/dist/world-bootstrap.d.ts +0 -31
- package/dist/world-bootstrap.d.ts.map +0 -1
- package/dist/world-bootstrap.js +0 -415
- package/dist/world-bootstrap.js.map +0 -1
- package/openclaw.plugin.json +0 -59
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,1147 @@
|
|
|
1
|
+
import { a as GuardEvent, W as WorldDefinition, b as GuardEngineOptions, G as GuardVerdict, V as ViabilityStatus } from './guard-contract-D_RQz9kt.js';
|
|
2
|
+
export { E as EvaluationTrace, c as GUARD_EXIT_CODES, d as GuardCheck, e as GuardExitCode, f as GuardStatus, I as InvariantCheck, K as KernelRuleCheck, L as LevelCheck, P as PrecedenceResolution, R as RoleCheck, S as SafetyCheck, g as VerdictEvidence } from './guard-contract-D_RQz9kt.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* Guard Engine — Deterministic Governance Evaluator
|
|
3
6
|
*
|
|
4
|
-
*
|
|
5
|
-
* ALLOW / PAUSE / BLOCK verdicts against structured world rules.
|
|
7
|
+
* Pure function: (event, world, options) → verdict
|
|
6
8
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
+
* Evaluates a GuardEvent against a loaded WorldDefinition and produces
|
|
10
|
+
* a GuardVerdict with evidence and optional evaluation trace.
|
|
9
11
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
+
* Evaluation chain (first-match-wins on BLOCK/PAUSE):
|
|
13
|
+
* 1. Safety checks (prompt injection, scope escape) → PAUSE
|
|
14
|
+
* 2. Role-specific rules (cannotDo, requiresApproval) → BLOCK/PAUSE
|
|
15
|
+
* 3. Declarative guards (guards.json) → BLOCK/PAUSE/WARN
|
|
16
|
+
* 4. Kernel rules (kernel.json forbidden patterns) → BLOCK
|
|
17
|
+
* 5. Level constraints (basic/standard/strict) → PAUSE
|
|
18
|
+
* 6. Default → ALLOW
|
|
12
19
|
*
|
|
13
|
-
*
|
|
20
|
+
* Invariant checks run unconditionally and are recorded in evidence
|
|
21
|
+
* but do not produce verdicts — they measure world health.
|
|
14
22
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
23
|
+
* INVARIANTS:
|
|
24
|
+
* - Deterministic: same event + same world → same verdict.
|
|
25
|
+
* - Zero network calls. Zero LLM calls. Zero async.
|
|
26
|
+
* - Every check is recorded in the trace, not just the decider.
|
|
27
|
+
* - No hidden logic. Everything is in the world file or declared here.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Evaluate a guard event against a world definition.
|
|
32
|
+
*
|
|
33
|
+
* This is the entire guard engine. One function. Deterministic.
|
|
34
|
+
* No class instantiation, no state, no side effects.
|
|
35
|
+
*/
|
|
36
|
+
declare function evaluateGuard(event: GuardEvent, world: WorldDefinition, options?: GuardEngineOptions): GuardVerdict;
|
|
37
|
+
/**
|
|
38
|
+
* Build a normalized allowlist key from a GuardEvent.
|
|
39
|
+
*
|
|
40
|
+
* Format: `tool::intent` (both lowercased, intent trimmed).
|
|
41
|
+
* Tool defaults to '*' when absent.
|
|
42
|
+
*
|
|
43
|
+
* Callers use this to:
|
|
44
|
+
* 1. Add keys to the allowlist set (on user "allow-always" decision)
|
|
45
|
+
* 2. The engine uses it to check membership before evaluation
|
|
46
|
+
*
|
|
47
|
+
* The key is opaque — callers should always use this function
|
|
48
|
+
* rather than constructing keys manually.
|
|
49
|
+
*/
|
|
50
|
+
declare function eventToAllowlistKey(event: GuardEvent): string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Audit Logger — Runtime Governance Telemetry
|
|
54
|
+
*
|
|
55
|
+
* Records every action evaluated by the governance engine.
|
|
56
|
+
* Provides the governance paper trail: what happened, why, and when.
|
|
57
|
+
*
|
|
58
|
+
* Architecture:
|
|
59
|
+
* - AuditEvent is the atomic record (one per evaluateGuard call)
|
|
60
|
+
* - AuditLogger is the pluggable interface (file, database, stream)
|
|
61
|
+
* - FileAuditLogger is the built-in file-based implementation
|
|
62
|
+
* - createGovernanceEngine wraps evaluateGuard with automatic logging
|
|
63
|
+
*
|
|
64
|
+
* INVARIANTS:
|
|
65
|
+
* - Logging never blocks or fails the governance decision
|
|
66
|
+
* - Every AuditEvent includes the full verdict + evidence
|
|
67
|
+
* - Events are append-only (immutable log)
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A single governance evaluation record.
|
|
72
|
+
* One of these is produced for every call to evaluateGuard.
|
|
73
|
+
*/
|
|
74
|
+
interface AuditEvent {
|
|
75
|
+
/** ISO 8601 timestamp */
|
|
76
|
+
timestamp: string;
|
|
77
|
+
/** World identity */
|
|
78
|
+
worldId: string;
|
|
79
|
+
worldName: string;
|
|
80
|
+
worldVersion: string;
|
|
81
|
+
/** The action that was evaluated */
|
|
82
|
+
intent: string;
|
|
83
|
+
tool?: string;
|
|
84
|
+
scope?: string;
|
|
85
|
+
actor?: string;
|
|
86
|
+
direction?: 'input' | 'output';
|
|
87
|
+
/** The governance decision */
|
|
88
|
+
decision: 'ALLOW' | 'BLOCK' | 'PAUSE';
|
|
89
|
+
reason?: string;
|
|
90
|
+
ruleId?: string;
|
|
91
|
+
warning?: string;
|
|
92
|
+
/** Which rules/guards matched */
|
|
93
|
+
guardsMatched: string[];
|
|
94
|
+
rulesMatched: string[];
|
|
95
|
+
/** Invariant health */
|
|
96
|
+
invariantsSatisfied: number;
|
|
97
|
+
invariantsTotal: number;
|
|
98
|
+
/** Enforcement level used */
|
|
99
|
+
enforcementLevel: string;
|
|
100
|
+
/** Evaluation duration in milliseconds (if trace was enabled) */
|
|
101
|
+
durationMs?: number;
|
|
102
|
+
/** Full event args (optional, for detailed audit) */
|
|
103
|
+
args?: Record<string, unknown>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Summary of governance activity over a set of audit events.
|
|
107
|
+
*/
|
|
108
|
+
interface AuditSummary {
|
|
109
|
+
totalActions: number;
|
|
110
|
+
allowed: number;
|
|
111
|
+
blocked: number;
|
|
112
|
+
paused: number;
|
|
113
|
+
/** Unique actors seen */
|
|
114
|
+
actors: string[];
|
|
115
|
+
/** Actions grouped by intent */
|
|
116
|
+
topIntents: {
|
|
117
|
+
intent: string;
|
|
118
|
+
count: number;
|
|
119
|
+
blocked: number;
|
|
120
|
+
paused: number;
|
|
121
|
+
}[];
|
|
122
|
+
/** Most frequently triggered rules */
|
|
123
|
+
topRules: {
|
|
124
|
+
ruleId: string;
|
|
125
|
+
count: number;
|
|
126
|
+
}[];
|
|
127
|
+
/** Time range */
|
|
128
|
+
firstEvent: string;
|
|
129
|
+
lastEvent: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Pluggable audit logger interface.
|
|
133
|
+
* Implement this to send audit events to any destination.
|
|
134
|
+
*/
|
|
135
|
+
interface AuditLogger {
|
|
136
|
+
/** Append an audit event. Must not throw. */
|
|
137
|
+
log(event: AuditEvent): void | Promise<void>;
|
|
138
|
+
/** Flush any buffered events. */
|
|
139
|
+
flush?(): void | Promise<void>;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Append-only file logger using newline-delimited JSON (NDJSON).
|
|
143
|
+
* Each line is a self-contained JSON audit event.
|
|
144
|
+
*
|
|
145
|
+
* Log file location defaults to .neuroverse/audit.ndjson
|
|
146
|
+
*/
|
|
147
|
+
declare class FileAuditLogger implements AuditLogger {
|
|
148
|
+
private logPath;
|
|
149
|
+
private buffer;
|
|
150
|
+
private flushTimer;
|
|
151
|
+
private flushIntervalMs;
|
|
152
|
+
constructor(logPath: string, options?: {
|
|
153
|
+
flushIntervalMs?: number;
|
|
154
|
+
});
|
|
155
|
+
log(event: AuditEvent): void;
|
|
156
|
+
flush(): Promise<void>;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Simple logger that writes to stderr. Useful for development.
|
|
160
|
+
*/
|
|
161
|
+
declare class ConsoleAuditLogger implements AuditLogger {
|
|
162
|
+
log(event: AuditEvent): void;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Sends events to multiple loggers. Useful for file + console, file + webhook, etc.
|
|
166
|
+
*/
|
|
167
|
+
declare class CompositeAuditLogger implements AuditLogger {
|
|
168
|
+
private loggers;
|
|
169
|
+
constructor(...loggers: AuditLogger[]);
|
|
170
|
+
log(event: AuditEvent): void;
|
|
171
|
+
flush(): Promise<void>;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Convert a GuardEvent + GuardVerdict into an AuditEvent.
|
|
175
|
+
*/
|
|
176
|
+
declare function verdictToAuditEvent(event: GuardEvent, verdict: GuardVerdict): AuditEvent;
|
|
177
|
+
interface GovernanceEngineOptions extends GuardEngineOptions {
|
|
178
|
+
/** Audit logger instance. If provided, every evaluation is logged. */
|
|
179
|
+
auditLogger?: AuditLogger;
|
|
180
|
+
/** Include args in audit events. Default: false (privacy). */
|
|
181
|
+
auditArgs?: boolean;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Create a governed evaluation function that wraps evaluateGuard
|
|
185
|
+
* with automatic audit logging.
|
|
186
|
+
*
|
|
187
|
+
* Usage:
|
|
188
|
+
* const engine = createGovernanceEngine(world, {
|
|
189
|
+
* auditLogger: new FileAuditLogger('.neuroverse/audit.ndjson'),
|
|
190
|
+
* });
|
|
191
|
+
*
|
|
192
|
+
* const verdict = engine.evaluate(event);
|
|
193
|
+
*/
|
|
194
|
+
declare function createGovernanceEngine(world: WorldDefinition, options?: GovernanceEngineOptions): {
|
|
195
|
+
/**
|
|
196
|
+
* Evaluate a governance event and log the result.
|
|
197
|
+
*/
|
|
198
|
+
evaluate(event: GuardEvent): GuardVerdict;
|
|
199
|
+
/** Flush the audit logger. */
|
|
200
|
+
flush(): Promise<void>;
|
|
201
|
+
/** The underlying world definition. */
|
|
202
|
+
world: WorldDefinition;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Read audit events from an NDJSON log file.
|
|
206
|
+
*
|
|
207
|
+
* @param logPath - Path to the audit log file
|
|
208
|
+
* @param filter - Optional filter function
|
|
209
|
+
* @returns Array of matching audit events
|
|
210
|
+
*/
|
|
211
|
+
declare function readAuditLog(logPath: string, filter?: (event: AuditEvent) => boolean): Promise<AuditEvent[]>;
|
|
212
|
+
/**
|
|
213
|
+
* Summarize a set of audit events.
|
|
214
|
+
*/
|
|
215
|
+
declare function summarizeAuditEvents(events: AuditEvent[]): AuditSummary;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Verdict Formatter — Human-Readable Verdict Output
|
|
219
|
+
*
|
|
220
|
+
* Converts a GuardVerdict into a consistent, human-readable string.
|
|
221
|
+
* Used by adapters, CLIs, and UIs to display governance decisions.
|
|
222
|
+
*
|
|
223
|
+
* Design:
|
|
224
|
+
* - One function, one concern: verdict → string
|
|
225
|
+
* - No side effects, no I/O
|
|
226
|
+
* - Adapters call this so every integration has consistent messaging
|
|
227
|
+
*/
|
|
228
|
+
|
|
229
|
+
interface FormatVerdictOptions {
|
|
230
|
+
/** Include rule/guard ID. Default: true. */
|
|
231
|
+
showRuleId?: boolean;
|
|
232
|
+
/** Include evidence summary. Default: false. */
|
|
233
|
+
showEvidence?: boolean;
|
|
234
|
+
/** Include warning if present. Default: true. */
|
|
235
|
+
showWarning?: boolean;
|
|
236
|
+
/** Use ANSI colors in output. Default: false. */
|
|
237
|
+
color?: boolean;
|
|
238
|
+
/** Compact single-line format. Default: false. */
|
|
239
|
+
compact?: boolean;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Format a GuardVerdict as a human-readable string.
|
|
243
|
+
*
|
|
244
|
+
* Examples:
|
|
245
|
+
* formatVerdict(verdict)
|
|
246
|
+
* // "BLOCKED\n Rule: margin_floor\n Reason: margin ratio below 10%"
|
|
247
|
+
*
|
|
248
|
+
* formatVerdict(verdict, { compact: true })
|
|
249
|
+
* // "BLOCKED — margin_floor: margin ratio below 10%"
|
|
250
|
+
*
|
|
251
|
+
* formatVerdict(verdict, { color: true })
|
|
252
|
+
* // Same but with ANSI colors
|
|
253
|
+
*/
|
|
254
|
+
declare function formatVerdict(verdict: GuardVerdict, options?: FormatVerdictOptions): string;
|
|
255
|
+
/**
|
|
256
|
+
* Format a verdict as a short one-line status message.
|
|
257
|
+
* Useful for logging, status bars, and notifications.
|
|
258
|
+
*
|
|
259
|
+
* Example: "BLOCKED: margin_floor — margin ratio below 10%"
|
|
260
|
+
*/
|
|
261
|
+
declare function formatVerdictOneLine(verdict: GuardVerdict): string;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Governance Impact Report — Counterfactual Audit
|
|
265
|
+
*
|
|
266
|
+
* Answers: "What would have happened if governance did not exist?"
|
|
267
|
+
*
|
|
268
|
+
* This is the killer enterprise feature. It doesn't just log what happened —
|
|
269
|
+
* it proves the value of governance by showing what it prevented.
|
|
270
|
+
*
|
|
271
|
+
* Architecture:
|
|
272
|
+
* - Reads audit events (from FileAuditLogger NDJSON)
|
|
273
|
+
* - Classifies blocked/paused actions by risk category
|
|
274
|
+
* - Produces an ImpactReport with aggregated prevention metrics
|
|
275
|
+
* - CLI renders as human-readable or JSON
|
|
276
|
+
*
|
|
277
|
+
* INVARIANTS:
|
|
278
|
+
* - No speculation — only reports on actual evaluated actions
|
|
279
|
+
* - Risk categories derived from world rules, not invented
|
|
280
|
+
* - All numbers are exact counts from the audit log
|
|
281
|
+
*/
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* A counterfactual governance impact report.
|
|
285
|
+
*/
|
|
286
|
+
interface ImpactReport {
|
|
287
|
+
/** Report metadata */
|
|
288
|
+
generatedAt: string;
|
|
289
|
+
periodStart: string;
|
|
290
|
+
periodEnd: string;
|
|
291
|
+
worldName: string;
|
|
292
|
+
/** High-level stats */
|
|
293
|
+
totalEvaluations: number;
|
|
294
|
+
totalBlocked: number;
|
|
295
|
+
totalPaused: number;
|
|
296
|
+
totalAllowed: number;
|
|
297
|
+
/** Prevention rate: (blocked + paused) / total */
|
|
298
|
+
preventionRate: number;
|
|
299
|
+
/** Blocked actions grouped by category */
|
|
300
|
+
preventedByCategory: PreventionCategory[];
|
|
301
|
+
/** Top prevented intents */
|
|
302
|
+
topPreventedIntents: {
|
|
303
|
+
intent: string;
|
|
304
|
+
count: number;
|
|
305
|
+
topRule: string;
|
|
306
|
+
}[];
|
|
307
|
+
/** Actors with most blocked actions (potential policy violations) */
|
|
308
|
+
hotActors: {
|
|
309
|
+
actor: string;
|
|
310
|
+
blocked: number;
|
|
311
|
+
paused: number;
|
|
312
|
+
total: number;
|
|
313
|
+
}[];
|
|
314
|
+
/** Rules that triggered most often */
|
|
315
|
+
mostActiveRules: {
|
|
316
|
+
ruleId: string;
|
|
317
|
+
blockCount: number;
|
|
318
|
+
pauseCount: number;
|
|
319
|
+
}[];
|
|
320
|
+
/** Hourly activity pattern (governance load) */
|
|
321
|
+
hourlyDistribution: {
|
|
322
|
+
hour: number;
|
|
323
|
+
total: number;
|
|
324
|
+
blocked: number;
|
|
325
|
+
}[];
|
|
326
|
+
/** Repeat offenders: actions attempted more than once after being blocked */
|
|
327
|
+
repeatViolations: {
|
|
328
|
+
intent: string;
|
|
329
|
+
actor: string;
|
|
330
|
+
attempts: number;
|
|
331
|
+
firstSeen: string;
|
|
332
|
+
lastSeen: string;
|
|
333
|
+
}[];
|
|
334
|
+
}
|
|
335
|
+
interface PreventionCategory {
|
|
336
|
+
category: string;
|
|
337
|
+
count: number;
|
|
338
|
+
/** Example intents in this category */
|
|
339
|
+
examples: string[];
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Generate a governance impact report from audit events.
|
|
343
|
+
*/
|
|
344
|
+
declare function generateImpactReport(events: AuditEvent[]): ImpactReport;
|
|
345
|
+
/**
|
|
346
|
+
* Render an impact report as human-readable text.
|
|
347
|
+
*/
|
|
348
|
+
declare function renderImpactReport(report: ImpactReport): string;
|
|
349
|
+
/**
|
|
350
|
+
* Generate an impact report directly from an audit log file.
|
|
351
|
+
*/
|
|
352
|
+
declare function generateImpactReportFromFile(logPath: string): Promise<ImpactReport>;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* World Loader — Shared world file loading for CLI commands
|
|
356
|
+
*
|
|
357
|
+
* Loads a WorldDefinition from either:
|
|
358
|
+
* - A directory containing individual JSON files
|
|
359
|
+
* - (Future) A .nv-world.zip archive
|
|
360
|
+
*
|
|
361
|
+
* Used by: neuroverse guard, neuroverse validate, neuroverse init
|
|
362
|
+
* Not used by: neuroverse bootstrap (which produces world files, not consumes them)
|
|
363
|
+
*/
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Load a WorldDefinition from a directory of JSON files.
|
|
367
|
+
*
|
|
368
|
+
* Reads all standard world files and assembles them into a WorldDefinition.
|
|
369
|
+
* Missing optional files are handled gracefully with defaults.
|
|
370
|
+
* Missing required files (world.json) throw.
|
|
371
|
+
*/
|
|
372
|
+
declare function loadWorldFromDirectory(dirPath: string): Promise<WorldDefinition>;
|
|
373
|
+
/**
|
|
374
|
+
* Load a world from a path — auto-detects directory vs .nv-world.zip.
|
|
375
|
+
*/
|
|
376
|
+
declare function loadWorld(worldPath: string): Promise<WorldDefinition>;
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Condition Engine — Structured operator evaluation
|
|
380
|
+
*
|
|
381
|
+
* Ported from the OpenClaw governance plugin. Provides 12 operators
|
|
382
|
+
* for evaluating conditions against guard events:
|
|
383
|
+
*
|
|
384
|
+
* ==, !=, >, <, >=, <=, in,
|
|
385
|
+
* contains, contains_any, matches_pattern, starts_with, ends_with
|
|
386
|
+
*
|
|
387
|
+
* Supports dot-notation field resolution (e.g. "args.command", "args.file_path")
|
|
388
|
+
* for inspecting tool arguments.
|
|
389
|
+
*
|
|
390
|
+
* Deterministic. No AI. No network. Sub-millisecond per evaluation.
|
|
391
|
+
*/
|
|
392
|
+
|
|
393
|
+
type ConditionOperator = '==' | '!=' | '>' | '<' | '>=' | '<=' | 'in' | 'contains' | 'contains_any' | 'matches_pattern' | 'starts_with' | 'ends_with';
|
|
394
|
+
interface Condition {
|
|
395
|
+
field: string;
|
|
396
|
+
operator: ConditionOperator;
|
|
397
|
+
value: string | string[] | number | boolean;
|
|
398
|
+
}
|
|
399
|
+
interface ConditionResult {
|
|
400
|
+
matched: boolean;
|
|
401
|
+
evidence: string | null;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Evaluate a single condition against a guard event.
|
|
405
|
+
* Returns whether it matched and what evidence was found.
|
|
406
|
+
*/
|
|
407
|
+
declare function evaluateCondition(condition: Condition, event: GuardEvent): ConditionResult;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Validate Contract — CLI World File Static Analysis Types
|
|
411
|
+
*
|
|
412
|
+
* Defines the input/output contract for `neuroverse validate`.
|
|
413
|
+
*
|
|
414
|
+
* WorldDefinition comes in (loaded from .nv-world.zip or directory).
|
|
415
|
+
* ValidateReport goes out (stdout JSON).
|
|
416
|
+
* Exit code encodes health: 0=PASS, 1=FAIL, 2=WARN.
|
|
417
|
+
*
|
|
418
|
+
* The validate engine performs static analysis on world files:
|
|
419
|
+
* - Completeness: Are all required blocks present and non-empty?
|
|
420
|
+
* - Referential integrity: Do rules reference declared state variables?
|
|
421
|
+
* - Guard coverage: Do invariants have backing structural guards?
|
|
422
|
+
* - Contradiction detection: Do rules conflict with each other?
|
|
423
|
+
* - Orphan detection: Are there unreachable rules or unused variables?
|
|
424
|
+
*
|
|
425
|
+
* INVARIANTS:
|
|
426
|
+
* - Deterministic: same world → same report.
|
|
427
|
+
* - Zero network calls.
|
|
428
|
+
* - Every finding includes the source block and a human-readable message.
|
|
429
|
+
*/
|
|
430
|
+
type FindingSeverity = 'error' | 'warning' | 'info';
|
|
431
|
+
type FindingCategory = 'completeness' | 'referential-integrity' | 'guard-coverage' | 'contradiction' | 'orphan' | 'schema-violation' | 'semantic-tension';
|
|
432
|
+
/**
|
|
433
|
+
* A single finding from static analysis.
|
|
434
|
+
*/
|
|
435
|
+
interface ValidateFinding {
|
|
436
|
+
/** Unique ID for this finding type (e.g., "missing-thesis", "orphan-rule-003") */
|
|
437
|
+
id: string;
|
|
438
|
+
/** Human-readable message */
|
|
439
|
+
message: string;
|
|
440
|
+
/** Error, warning, or informational */
|
|
441
|
+
severity: FindingSeverity;
|
|
442
|
+
/** What category of issue this is */
|
|
443
|
+
category: FindingCategory;
|
|
444
|
+
/** Which world file block(s) are affected */
|
|
445
|
+
affectedBlocks: string[];
|
|
446
|
+
/** Specific field or rule ID that triggered this finding */
|
|
447
|
+
source?: string;
|
|
448
|
+
/** Suggested fix (when deterministically derivable) */
|
|
449
|
+
suggestion?: string;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Summary of world health by category.
|
|
453
|
+
*/
|
|
454
|
+
interface ValidateSummary {
|
|
455
|
+
/** Total findings by severity */
|
|
456
|
+
errors: number;
|
|
457
|
+
warnings: number;
|
|
458
|
+
info: number;
|
|
459
|
+
/** Block completeness score (0-100) */
|
|
460
|
+
completenessScore: number;
|
|
461
|
+
/** Invariant coverage percentage (invariants with backing guards) */
|
|
462
|
+
invariantCoverage: number;
|
|
463
|
+
/** Whether the world is valid enough to run */
|
|
464
|
+
canRun: boolean;
|
|
465
|
+
/** Whether the world passes full validation (no errors) */
|
|
466
|
+
isHealthy: boolean;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* The validate report — what goes to stdout.
|
|
470
|
+
*
|
|
471
|
+
* Exit codes:
|
|
472
|
+
* 0 = PASS (no errors, world is healthy)
|
|
473
|
+
* 1 = FAIL (errors found, world cannot run safely)
|
|
474
|
+
* 2 = WARN (warnings found but world can run)
|
|
475
|
+
* 3 = ERROR (invalid input, could not parse world)
|
|
476
|
+
*/
|
|
477
|
+
interface ValidateReport {
|
|
478
|
+
/** World identity */
|
|
479
|
+
worldId: string;
|
|
480
|
+
worldName: string;
|
|
481
|
+
worldVersion: string;
|
|
482
|
+
/** When the validation ran */
|
|
483
|
+
validatedAt: number;
|
|
484
|
+
/** Duration of validation */
|
|
485
|
+
durationMs: number;
|
|
486
|
+
/** High-level summary */
|
|
487
|
+
summary: ValidateSummary;
|
|
488
|
+
/** All findings, ordered by severity (errors first) */
|
|
489
|
+
findings: ValidateFinding[];
|
|
490
|
+
}
|
|
491
|
+
declare const VALIDATE_EXIT_CODES: {
|
|
492
|
+
readonly PASS: 0;
|
|
493
|
+
readonly FAIL: 1;
|
|
494
|
+
readonly WARN: 2;
|
|
495
|
+
readonly ERROR: 3;
|
|
496
|
+
};
|
|
497
|
+
type ValidateExitCode = (typeof VALIDATE_EXIT_CODES)[keyof typeof VALIDATE_EXIT_CODES];
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Validate Engine — World File Static Analysis
|
|
501
|
+
*
|
|
502
|
+
* Pure function: (world) → report
|
|
503
|
+
*
|
|
504
|
+
* Performs comprehensive static analysis on a WorldDefinition:
|
|
505
|
+
* 1. Completeness — are required blocks present and non-empty?
|
|
506
|
+
* 2. Referential integrity — do rules reference declared variables?
|
|
507
|
+
* 3. Guard coverage — do invariants have backing structural guards?
|
|
508
|
+
* 4. Contradiction detection — do rules conflict?
|
|
509
|
+
* 5. Orphan detection — unused variables, unreachable rules
|
|
510
|
+
* 6. Schema validation — values within declared ranges
|
|
511
|
+
*
|
|
512
|
+
* INVARIANTS:
|
|
513
|
+
* - Deterministic: same world → same report, always.
|
|
514
|
+
* - Zero network calls. Zero LLM calls. Zero async.
|
|
515
|
+
* - Every finding is traceable to specific world file blocks.
|
|
516
|
+
*/
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Validate a world definition and produce a report.
|
|
520
|
+
*
|
|
521
|
+
* This is the entire validate engine. One function. Deterministic.
|
|
522
|
+
*/
|
|
523
|
+
declare function validateWorld(world: WorldDefinition): ValidateReport;
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Bootstrap Contract — Markdown → WorldDefinition Compilation Types
|
|
527
|
+
*
|
|
528
|
+
* Defines the input/output contract for `neuroverse bootstrap`.
|
|
529
|
+
*
|
|
530
|
+
* Input: .nv-world.md file (structured markdown)
|
|
531
|
+
* Output: WorldDefinition (passed to validate, guard, or compileWorldToZip)
|
|
532
|
+
*
|
|
533
|
+
* The markdown format is:
|
|
534
|
+
* - YAML frontmatter for world identity and metadata
|
|
535
|
+
* - H1 sections for each block (Thesis, Invariants, State, etc.)
|
|
536
|
+
* - Structured sub-sections within each block
|
|
537
|
+
* - Deterministically parseable — no LLM, no heuristics
|
|
538
|
+
*
|
|
539
|
+
* Exit codes:
|
|
540
|
+
* 0 = SUCCESS (compiled cleanly)
|
|
541
|
+
* 1 = FAIL (parse errors, missing required sections)
|
|
542
|
+
* 3 = ERROR (file not found, invalid input)
|
|
543
|
+
*/
|
|
544
|
+
type ParseSeverity = 'error' | 'warning' | 'info';
|
|
545
|
+
/**
|
|
546
|
+
* A single parse issue found during markdown compilation.
|
|
547
|
+
*/
|
|
548
|
+
interface ParseIssue {
|
|
549
|
+
/** Line number in the source markdown (1-based) */
|
|
550
|
+
line: number;
|
|
551
|
+
/** Which section the issue was found in */
|
|
552
|
+
section: string;
|
|
553
|
+
/** Human-readable message */
|
|
554
|
+
message: string;
|
|
555
|
+
/** Severity */
|
|
556
|
+
severity: ParseSeverity;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* The result of parsing a .nv-world.md file.
|
|
560
|
+
*/
|
|
561
|
+
interface BootstrapResult {
|
|
562
|
+
/** Whether compilation succeeded (no errors) */
|
|
563
|
+
success: boolean;
|
|
564
|
+
/** Source file path */
|
|
565
|
+
sourcePath: string;
|
|
566
|
+
/** All parse issues */
|
|
567
|
+
issues: ParseIssue[];
|
|
568
|
+
/** Parsed sections (for debugging) */
|
|
569
|
+
parsedSections: string[];
|
|
570
|
+
/** Duration */
|
|
571
|
+
durationMs: number;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* YAML frontmatter parsed from the markdown header.
|
|
575
|
+
*/
|
|
576
|
+
interface ParsedFrontmatter {
|
|
577
|
+
world_id: string;
|
|
578
|
+
name: string;
|
|
579
|
+
version?: string;
|
|
580
|
+
runtime_mode?: string;
|
|
581
|
+
default_profile?: string;
|
|
582
|
+
alternative_profile?: string;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* A parsed invariant from the Invariants section.
|
|
586
|
+
*/
|
|
587
|
+
interface ParsedInvariant {
|
|
588
|
+
id: string;
|
|
589
|
+
label: string;
|
|
590
|
+
enforcement: string;
|
|
591
|
+
mutable: boolean;
|
|
592
|
+
line: number;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* A parsed state variable from the State section.
|
|
596
|
+
*/
|
|
597
|
+
interface ParsedStateVariable {
|
|
598
|
+
id: string;
|
|
599
|
+
type: 'number' | 'enum' | 'boolean';
|
|
600
|
+
default: string | number | boolean;
|
|
601
|
+
label: string;
|
|
602
|
+
description: string;
|
|
603
|
+
min?: number;
|
|
604
|
+
max?: number;
|
|
605
|
+
step?: number;
|
|
606
|
+
options?: string[];
|
|
607
|
+
line: number;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* A parsed assumption profile from the Assumptions section.
|
|
611
|
+
*/
|
|
612
|
+
interface ParsedAssumptionProfile {
|
|
613
|
+
id: string;
|
|
614
|
+
name: string;
|
|
615
|
+
description: string;
|
|
616
|
+
parameters: Record<string, string | number | boolean>;
|
|
617
|
+
line: number;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* A parsed trigger from a rule.
|
|
621
|
+
*/
|
|
622
|
+
interface ParsedTrigger {
|
|
623
|
+
field: string;
|
|
624
|
+
operator: string;
|
|
625
|
+
value: string | number | boolean;
|
|
626
|
+
source: 'state' | 'assumption';
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* A parsed effect from a rule.
|
|
630
|
+
*/
|
|
631
|
+
interface ParsedEffect {
|
|
632
|
+
target: string;
|
|
633
|
+
operation: string;
|
|
634
|
+
value: number | boolean | string;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* A parsed rule from the Rules section.
|
|
638
|
+
*/
|
|
639
|
+
interface ParsedRule {
|
|
640
|
+
id: string;
|
|
641
|
+
label: string;
|
|
642
|
+
severity: string;
|
|
643
|
+
description?: string;
|
|
644
|
+
order: number;
|
|
645
|
+
triggers: ParsedTrigger[];
|
|
646
|
+
effects: ParsedEffect[];
|
|
647
|
+
collapse_check?: {
|
|
648
|
+
field: string;
|
|
649
|
+
operator: string;
|
|
650
|
+
value: number;
|
|
651
|
+
};
|
|
652
|
+
causal_translation?: {
|
|
653
|
+
trigger_text: string;
|
|
654
|
+
rule_text: string;
|
|
655
|
+
shift_text: string;
|
|
656
|
+
effect_text: string;
|
|
657
|
+
};
|
|
658
|
+
line: number;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* A parsed gate from the Gates section.
|
|
662
|
+
*/
|
|
663
|
+
interface ParsedGate {
|
|
664
|
+
status: string;
|
|
665
|
+
field: string;
|
|
666
|
+
operator: string;
|
|
667
|
+
value: number;
|
|
668
|
+
line: number;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* A parsed outcome from the Outcomes section.
|
|
672
|
+
*/
|
|
673
|
+
interface ParsedOutcome {
|
|
674
|
+
id: string;
|
|
675
|
+
type: string;
|
|
676
|
+
range?: [number, number];
|
|
677
|
+
display?: string;
|
|
678
|
+
label: string;
|
|
679
|
+
primary?: boolean;
|
|
680
|
+
assignment?: string;
|
|
681
|
+
line: number;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* The full parsed intermediate representation.
|
|
685
|
+
*/
|
|
686
|
+
interface ParsedWorld {
|
|
687
|
+
frontmatter: ParsedFrontmatter;
|
|
688
|
+
thesis: string;
|
|
689
|
+
invariants: ParsedInvariant[];
|
|
690
|
+
stateVariables: ParsedStateVariable[];
|
|
691
|
+
assumptions: ParsedAssumptionProfile[];
|
|
692
|
+
rules: ParsedRule[];
|
|
693
|
+
gates: ParsedGate[];
|
|
694
|
+
outcomes: ParsedOutcome[];
|
|
695
|
+
}
|
|
696
|
+
declare const BOOTSTRAP_EXIT_CODES: {
|
|
697
|
+
readonly SUCCESS: 0;
|
|
698
|
+
readonly FAIL: 1;
|
|
699
|
+
readonly ERROR: 3;
|
|
700
|
+
};
|
|
701
|
+
type BootstrapExitCode = (typeof BOOTSTRAP_EXIT_CODES)[keyof typeof BOOTSTRAP_EXIT_CODES];
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Bootstrap Parser — .nv-world.md → ParsedWorld
|
|
705
|
+
*
|
|
706
|
+
* Deterministic markdown parser for the NeuroVerse world authoring format.
|
|
707
|
+
* No LLM calls. No heuristics. Pattern matching on structured markdown.
|
|
708
|
+
*
|
|
709
|
+
* ## Markdown Format (.nv-world.md)
|
|
710
|
+
*
|
|
711
|
+
* ```markdown
|
|
712
|
+
* ---
|
|
713
|
+
* world_id: my-world
|
|
714
|
+
* name: My World
|
|
715
|
+
* version: 1.0.0
|
|
716
|
+
* default_profile: baseline
|
|
717
|
+
* alternative_profile: alternative
|
|
718
|
+
* ---
|
|
719
|
+
*
|
|
720
|
+
* # Thesis
|
|
721
|
+
* The structural claim this world tests...
|
|
722
|
+
*
|
|
723
|
+
* # Invariants
|
|
724
|
+
* - `invariant_id` — Label text (structural, immutable)
|
|
725
|
+
* - `another_id` — Another label (structural, immutable)
|
|
726
|
+
*
|
|
727
|
+
* # State
|
|
728
|
+
* ## variable_name
|
|
729
|
+
* - type: number
|
|
730
|
+
* - min: 0
|
|
731
|
+
* - max: 100
|
|
732
|
+
* - step: 5
|
|
733
|
+
* - default: 50
|
|
734
|
+
* - label: Human Label
|
|
735
|
+
* - description: What this variable represents
|
|
736
|
+
*
|
|
737
|
+
* ## another_variable
|
|
738
|
+
* - type: enum
|
|
739
|
+
* - options: option_a, option_b, option_c
|
|
740
|
+
* - default: option_a
|
|
741
|
+
* - label: Another Variable
|
|
742
|
+
* - description: Description here
|
|
743
|
+
*
|
|
744
|
+
* # Assumptions
|
|
745
|
+
* ## baseline
|
|
746
|
+
* - name: Baseline Scenario
|
|
747
|
+
* - description: The default conditions
|
|
748
|
+
* - param_key: param_value
|
|
749
|
+
*
|
|
750
|
+
* ## alternative
|
|
751
|
+
* - name: Alternative Scenario
|
|
752
|
+
* - description: What changes
|
|
753
|
+
* - param_key: different_value
|
|
754
|
+
*
|
|
755
|
+
* # Rules
|
|
756
|
+
* ## rule-001: Rule Label (structural)
|
|
757
|
+
* Description of what this rule does.
|
|
758
|
+
*
|
|
759
|
+
* When field == "value" [state] AND other_field > 50 [assumption]
|
|
760
|
+
* Then target *= 0.30, other_target = false
|
|
761
|
+
* Collapse: field < 0.03
|
|
762
|
+
*
|
|
763
|
+
* > trigger: Trigger text here
|
|
764
|
+
* > rule: Rule text here
|
|
765
|
+
* > shift: Shift text here
|
|
766
|
+
* > effect: Effect text here
|
|
767
|
+
*
|
|
768
|
+
* # Gates
|
|
769
|
+
* - THRIVING: effective_margin >= 40
|
|
770
|
+
* - STABLE: effective_margin >= 20
|
|
771
|
+
* - COMPRESSED: effective_margin >= 10
|
|
772
|
+
* - CRITICAL: effective_margin > 3
|
|
773
|
+
* - MODEL_COLLAPSES: effective_margin <= 3
|
|
774
|
+
*
|
|
775
|
+
* # Outcomes
|
|
776
|
+
* ## outcome_id
|
|
777
|
+
* - type: number
|
|
778
|
+
* - range: 0-100
|
|
779
|
+
* - display: percentage
|
|
780
|
+
* - label: Outcome Label
|
|
781
|
+
* - primary: true
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Parse a .nv-world.md string into a ParsedWorld.
|
|
787
|
+
*
|
|
788
|
+
* Returns the parsed world and a list of issues.
|
|
789
|
+
* Callers should check issues for severity === 'error' before proceeding.
|
|
790
|
+
*/
|
|
791
|
+
declare function parseWorldMarkdown(markdown: string): {
|
|
792
|
+
world: ParsedWorld | null;
|
|
793
|
+
issues: ParseIssue[];
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* Bootstrap Emitter — ParsedWorld → WorldDefinition
|
|
798
|
+
*
|
|
799
|
+
* Converts the intermediate representation from the parser
|
|
800
|
+
* into a proper WorldDefinition that the guard engine, validate engine,
|
|
801
|
+
* and compileWorldToZip() all consume.
|
|
802
|
+
*
|
|
803
|
+
* Fills in sensible defaults for optional fields.
|
|
804
|
+
* Reports issues when the parsed data can't cleanly map to the target type.
|
|
805
|
+
*
|
|
806
|
+
* Deterministic. Same parsed input → same WorldDefinition output.
|
|
807
|
+
*/
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Convert a ParsedWorld into a WorldDefinition.
|
|
811
|
+
*
|
|
812
|
+
* Returns the world definition and any issues encountered during emission.
|
|
813
|
+
*/
|
|
814
|
+
declare function emitWorldDefinition(parsed: ParsedWorld): {
|
|
815
|
+
world: WorldDefinition;
|
|
816
|
+
issues: ParseIssue[];
|
|
817
|
+
};
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Derive Contract — AI-Assisted World Synthesis Types
|
|
821
|
+
*
|
|
822
|
+
* Defines the input/output contract for `neuroverse derive`.
|
|
823
|
+
*
|
|
824
|
+
* Input: Arbitrary markdown files/directories
|
|
825
|
+
* Output: .nv-world.md file constrained by DerivationWorld
|
|
826
|
+
*
|
|
827
|
+
* Exit codes:
|
|
828
|
+
* 0 = SUCCESS (valid file written)
|
|
829
|
+
* 1 = VALIDATION_FAIL (output failed parseWorldMarkdown)
|
|
830
|
+
* 2 = INPUT_ERROR (missing input, empty dir, unreadable)
|
|
831
|
+
* 3 = PROVIDER_ERROR (no config, API failure, timeout)
|
|
832
|
+
*/
|
|
833
|
+
declare const DERIVE_EXIT_CODES: {
|
|
834
|
+
readonly SUCCESS: 0;
|
|
835
|
+
readonly VALIDATION_FAIL: 1;
|
|
836
|
+
readonly INPUT_ERROR: 2;
|
|
837
|
+
readonly PROVIDER_ERROR: 3;
|
|
838
|
+
};
|
|
839
|
+
type DeriveExitCode = (typeof DERIVE_EXIT_CODES)[keyof typeof DERIVE_EXIT_CODES];
|
|
840
|
+
declare const CONFIGURE_AI_EXIT_CODES: {
|
|
841
|
+
readonly SUCCESS: 0;
|
|
842
|
+
readonly VALIDATION_FAIL: 1;
|
|
843
|
+
readonly ERROR: 3;
|
|
844
|
+
};
|
|
845
|
+
interface DeriveResult {
|
|
846
|
+
success: boolean;
|
|
847
|
+
outputPath: string;
|
|
848
|
+
sectionsDetected: string[];
|
|
849
|
+
validationErrors: number;
|
|
850
|
+
validationWarnings: number;
|
|
851
|
+
findings: DeriveFinding[];
|
|
852
|
+
gate?: string;
|
|
853
|
+
normalization?: NormalizationSummary;
|
|
854
|
+
durationMs: number;
|
|
855
|
+
}
|
|
856
|
+
interface NormalizationSummary {
|
|
857
|
+
fixCount: number;
|
|
858
|
+
invariantIds: number;
|
|
859
|
+
gateThresholds: number;
|
|
860
|
+
triggerTags: number;
|
|
861
|
+
}
|
|
862
|
+
interface DeriveFinding {
|
|
863
|
+
severity: 'error' | 'warning';
|
|
864
|
+
section: string;
|
|
865
|
+
message: string;
|
|
866
|
+
line?: number;
|
|
867
|
+
}
|
|
868
|
+
interface CollectedSource {
|
|
869
|
+
filename: string;
|
|
870
|
+
content: string;
|
|
871
|
+
}
|
|
872
|
+
interface AIProviderConfig {
|
|
873
|
+
provider: string;
|
|
874
|
+
model: string;
|
|
875
|
+
apiKey: string;
|
|
876
|
+
endpoint: string | null;
|
|
877
|
+
}
|
|
878
|
+
interface AIProvider {
|
|
879
|
+
complete(systemPrompt: string, userPrompt: string): Promise<string>;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Derive Engine — AI-Assisted World Synthesis Pipeline
|
|
884
|
+
*
|
|
885
|
+
* Pipeline:
|
|
886
|
+
* collect → prompt → AI → extract → parseWorldMarkdown → write → JSON
|
|
887
|
+
*
|
|
888
|
+
* DerivationWorld is advisory only:
|
|
889
|
+
* - Gate status is reported but never blocks file writing
|
|
890
|
+
* - parseWorldMarkdown errors ARE blocking (invalid .nv-world.md)
|
|
891
|
+
* - Governance engine does NOT run during derive
|
|
892
|
+
*/
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Extract .nv-world.md content from LLM response.
|
|
896
|
+
*
|
|
897
|
+
* Handles:
|
|
898
|
+
* - Triple backtick fences (```markdown ... ``` or ``` ... ```)
|
|
899
|
+
* - Sentinel markers (BEGIN NV WORLD / END NV WORLD)
|
|
900
|
+
* - Raw content starting with ---
|
|
901
|
+
*
|
|
902
|
+
* Pre-parse safety: output must contain `world_id:` in frontmatter.
|
|
903
|
+
*/
|
|
904
|
+
declare function extractWorldMarkdown(raw: string): string | null;
|
|
905
|
+
interface DeriveOptions {
|
|
906
|
+
inputPath: string;
|
|
907
|
+
outputPath: string;
|
|
908
|
+
validate: boolean;
|
|
909
|
+
dryRun: boolean;
|
|
910
|
+
providerOverride?: Partial<AIProviderConfig>;
|
|
911
|
+
}
|
|
912
|
+
declare function deriveWorld(options: DeriveOptions): Promise<{
|
|
913
|
+
result: DeriveResult;
|
|
914
|
+
exitCode: number;
|
|
915
|
+
dryRunOutput?: {
|
|
916
|
+
systemPrompt: string;
|
|
917
|
+
userPrompt: string;
|
|
918
|
+
};
|
|
919
|
+
}>;
|
|
920
|
+
|
|
921
|
+
/**
|
|
922
|
+
* Derive Normalizer — Post-processing pass for AI output drift
|
|
923
|
+
*
|
|
924
|
+
* Runs AFTER AI generation but BEFORE parsing. Normalizes common
|
|
925
|
+
* deviations from the strict .nv-world.md grammar so the parser
|
|
926
|
+
* can accept the output.
|
|
927
|
+
*
|
|
928
|
+
* Transformations:
|
|
929
|
+
* 1. Invariant lines: adds backtick-wrapped IDs when missing
|
|
930
|
+
* 2. Gate lines: converts symbolic values to numeric thresholds
|
|
931
|
+
* 3. Trigger lines: ensures [state] / [assumption] source tags
|
|
932
|
+
*
|
|
933
|
+
* INVARIANTS:
|
|
934
|
+
* - Deterministic: same input → same output, always.
|
|
935
|
+
* - Zero network calls. Zero LLM calls.
|
|
936
|
+
* - Never removes content — only reformats.
|
|
937
|
+
* - Operates on raw markdown string, not parsed structures.
|
|
938
|
+
*/
|
|
939
|
+
interface NormalizerReport {
|
|
940
|
+
/** Total number of lines fixed */
|
|
941
|
+
fixCount: number;
|
|
942
|
+
/** Number of invariant IDs wrapped in backticks */
|
|
943
|
+
invariantIds: number;
|
|
944
|
+
/** Number of symbolic gate thresholds converted to numeric */
|
|
945
|
+
gateThresholds: number;
|
|
946
|
+
/** Number of triggers tagged with [state] */
|
|
947
|
+
triggerTags: number;
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Normalize AI-generated .nv-world.md content to fix common drift patterns.
|
|
951
|
+
*
|
|
952
|
+
* Returns the normalized markdown string and a detailed fix report.
|
|
953
|
+
*/
|
|
954
|
+
declare function normalizeWorldMarkdown(markdown: string): {
|
|
955
|
+
normalized: string;
|
|
956
|
+
fixCount: number;
|
|
957
|
+
report: NormalizerReport;
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Explain Engine — Generates human-readable narrative summaries from compiled worlds.
|
|
962
|
+
*
|
|
963
|
+
* No AI calls. Pure template-based rendering from WorldDefinition data.
|
|
964
|
+
*
|
|
965
|
+
* Sections:
|
|
966
|
+
* 1. Core thesis and identity
|
|
967
|
+
* 2. Key dynamics (rules with causal_translation)
|
|
968
|
+
* 3. State variables (what can change)
|
|
969
|
+
* 4. Invariants (what cannot change)
|
|
970
|
+
* 5. Viability gates (health thresholds)
|
|
971
|
+
* 6. Dramatic tensions (opposing rule effects)
|
|
972
|
+
* 7. Outcomes (what gets measured)
|
|
973
|
+
*/
|
|
974
|
+
|
|
975
|
+
interface ExplainOutput {
|
|
976
|
+
worldName: string;
|
|
977
|
+
worldId: string;
|
|
978
|
+
thesis: string;
|
|
979
|
+
dynamics: DynamicSummary[];
|
|
980
|
+
stateVariables: VariableSummary[];
|
|
981
|
+
invariants: InvariantSummary[];
|
|
982
|
+
gates: GateSummary[];
|
|
983
|
+
tensions: TensionSummary[];
|
|
984
|
+
outcomes: OutcomeSummary[];
|
|
985
|
+
stats: WorldStats;
|
|
986
|
+
}
|
|
987
|
+
interface DynamicSummary {
|
|
988
|
+
ruleId: string;
|
|
989
|
+
label: string;
|
|
990
|
+
severity: string;
|
|
991
|
+
triggerDescription: string;
|
|
992
|
+
effectDescription: string;
|
|
993
|
+
targets: string[];
|
|
994
|
+
}
|
|
995
|
+
interface VariableSummary {
|
|
996
|
+
name: string;
|
|
997
|
+
label: string;
|
|
998
|
+
type: string;
|
|
999
|
+
defaultValue: string | number | boolean;
|
|
1000
|
+
range?: string;
|
|
1001
|
+
}
|
|
1002
|
+
interface InvariantSummary {
|
|
1003
|
+
id: string;
|
|
1004
|
+
label: string;
|
|
1005
|
+
enforcement: string;
|
|
1006
|
+
}
|
|
1007
|
+
interface GateSummary {
|
|
1008
|
+
status: string;
|
|
1009
|
+
field: string;
|
|
1010
|
+
threshold: string;
|
|
1011
|
+
}
|
|
1012
|
+
interface TensionSummary {
|
|
1013
|
+
variable: string;
|
|
1014
|
+
increasedBy: string[];
|
|
1015
|
+
decreasedBy: string[];
|
|
1016
|
+
}
|
|
1017
|
+
interface OutcomeSummary {
|
|
1018
|
+
id: string;
|
|
1019
|
+
label: string;
|
|
1020
|
+
type: string;
|
|
1021
|
+
primary: boolean;
|
|
1022
|
+
}
|
|
1023
|
+
interface WorldStats {
|
|
1024
|
+
invariants: number;
|
|
1025
|
+
stateVariables: number;
|
|
1026
|
+
rules: number;
|
|
1027
|
+
gates: number;
|
|
1028
|
+
outcomes: number;
|
|
1029
|
+
assumptions: number;
|
|
1030
|
+
}
|
|
1031
|
+
declare function explainWorld(world: WorldDefinition): ExplainOutput;
|
|
1032
|
+
declare function renderExplainText(output: ExplainOutput): string;
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Simulate Engine — Deterministic State Evolution
|
|
1036
|
+
*
|
|
1037
|
+
* Pure function: (world, options) → SimulationResult
|
|
1038
|
+
*
|
|
1039
|
+
* Evaluates all rules against the current state, applies effects,
|
|
1040
|
+
* classifies viability via gates, and produces a step-by-step trace.
|
|
1041
|
+
*
|
|
1042
|
+
* Supports:
|
|
1043
|
+
* - Single-step evaluation (default)
|
|
1044
|
+
* - Multi-step iteration (--steps N)
|
|
1045
|
+
* - State overrides (start from non-default values)
|
|
1046
|
+
* - Assumption profile selection
|
|
1047
|
+
* - Collapse detection (early termination)
|
|
1048
|
+
*
|
|
1049
|
+
* INVARIANTS:
|
|
1050
|
+
* - Deterministic: same world + same options → same result.
|
|
1051
|
+
* - Zero network calls. Zero LLM calls. Zero async.
|
|
1052
|
+
* - Every rule evaluation is recorded in the trace.
|
|
1053
|
+
*/
|
|
1054
|
+
|
|
1055
|
+
interface SimulateOptions {
|
|
1056
|
+
/** Number of simulation steps (default: 1) */
|
|
1057
|
+
steps?: number;
|
|
1058
|
+
/** State variable overrides (start values) */
|
|
1059
|
+
stateOverrides?: Record<string, string | number | boolean>;
|
|
1060
|
+
/** Assumption profile to use (default: world default) */
|
|
1061
|
+
profile?: string;
|
|
1062
|
+
}
|
|
1063
|
+
interface SimulationResult {
|
|
1064
|
+
worldId: string;
|
|
1065
|
+
worldName: string;
|
|
1066
|
+
profile: string;
|
|
1067
|
+
initialState: Record<string, string | number | boolean>;
|
|
1068
|
+
steps: SimulationStep[];
|
|
1069
|
+
finalState: Record<string, string | number | boolean>;
|
|
1070
|
+
finalViability: ViabilityStatus;
|
|
1071
|
+
collapsed: boolean;
|
|
1072
|
+
collapseStep?: number;
|
|
1073
|
+
collapseRule?: string;
|
|
1074
|
+
}
|
|
1075
|
+
interface SimulationStep {
|
|
1076
|
+
step: number;
|
|
1077
|
+
rulesEvaluated: RuleEvaluation[];
|
|
1078
|
+
rulesFired: number;
|
|
1079
|
+
stateAfter: Record<string, string | number | boolean>;
|
|
1080
|
+
viability: ViabilityStatus;
|
|
1081
|
+
collapsed: boolean;
|
|
1082
|
+
}
|
|
1083
|
+
interface RuleEvaluation {
|
|
1084
|
+
ruleId: string;
|
|
1085
|
+
label: string;
|
|
1086
|
+
triggered: boolean;
|
|
1087
|
+
excluded: boolean;
|
|
1088
|
+
effects: AppliedEffect[];
|
|
1089
|
+
collapsed: boolean;
|
|
1090
|
+
collapseField?: string;
|
|
1091
|
+
}
|
|
1092
|
+
interface AppliedEffect {
|
|
1093
|
+
target: string;
|
|
1094
|
+
operation: string;
|
|
1095
|
+
value: number | boolean | string;
|
|
1096
|
+
before: string | number | boolean;
|
|
1097
|
+
after: string | number | boolean;
|
|
1098
|
+
}
|
|
1099
|
+
declare function simulateWorld(world: WorldDefinition, options?: SimulateOptions): SimulationResult;
|
|
1100
|
+
declare function renderSimulateText(result: SimulationResult): string;
|
|
1101
|
+
|
|
1102
|
+
/**
|
|
1103
|
+
* Improve Engine — Actionable Suggestions from World Analysis
|
|
1104
|
+
*
|
|
1105
|
+
* Pure function: (world) → ImprovementReport
|
|
1106
|
+
*
|
|
1107
|
+
* Runs validation, simulation, and structural analysis to produce
|
|
1108
|
+
* prioritized, actionable suggestions for improving a world.
|
|
1109
|
+
*
|
|
1110
|
+
* Categories:
|
|
1111
|
+
* 1. Critical fixes — errors that prevent the world from running
|
|
1112
|
+
* 2. Structural gaps — missing guards, orphan variables, etc.
|
|
1113
|
+
* 3. Balance suggestions — detected from simulation dynamics
|
|
1114
|
+
* 4. Completeness — optional blocks that would strengthen the world
|
|
17
1115
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* /world status — Full governance metrics (drift, audit, friction)
|
|
22
|
-
* /world laws — Display the full constitution
|
|
23
|
-
* /world bootstrap — Generate world.json from your .md files
|
|
24
|
-
* /world bind — Bind agent identity to governance role
|
|
25
|
-
* /world unbind — Remove agent role binding
|
|
26
|
-
* /world bindings — Show all agent-to-role bindings
|
|
27
|
-
* /world propose — Agent recommends governance amendments
|
|
28
|
-
* /world approve <id> — Human approves a proposed amendment
|
|
29
|
-
* /world export — Export for use in other tools
|
|
30
|
-
* /world help — Command reference
|
|
1116
|
+
* INVARIANTS:
|
|
1117
|
+
* - Deterministic: same world → same report.
|
|
1118
|
+
* - Zero network calls. Zero LLM calls. Zero async.
|
|
31
1119
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
1120
|
+
|
|
1121
|
+
type SuggestionPriority = 'critical' | 'high' | 'medium' | 'low';
|
|
1122
|
+
type SuggestionCategory = 'fix' | 'structure' | 'balance' | 'completeness' | 'coverage';
|
|
1123
|
+
interface Suggestion {
|
|
1124
|
+
id: string;
|
|
1125
|
+
priority: SuggestionPriority;
|
|
1126
|
+
category: SuggestionCategory;
|
|
1127
|
+
title: string;
|
|
1128
|
+
description: string;
|
|
1129
|
+
action: string;
|
|
1130
|
+
affectedFiles: string[];
|
|
1131
|
+
}
|
|
1132
|
+
interface ImprovementReport {
|
|
1133
|
+
worldId: string;
|
|
1134
|
+
worldName: string;
|
|
1135
|
+
score: number;
|
|
1136
|
+
suggestions: Suggestion[];
|
|
1137
|
+
stats: {
|
|
1138
|
+
critical: number;
|
|
1139
|
+
high: number;
|
|
1140
|
+
medium: number;
|
|
1141
|
+
low: number;
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
declare function improveWorld(world: WorldDefinition): ImprovementReport;
|
|
1145
|
+
declare function renderImproveText(report: ImprovementReport): string;
|
|
1146
|
+
|
|
1147
|
+
export { type AIProvider, type AIProviderConfig, type AppliedEffect, type AuditEvent, type AuditLogger, type AuditSummary, BOOTSTRAP_EXIT_CODES, type BootstrapExitCode, type BootstrapResult, CONFIGURE_AI_EXIT_CODES, type CollectedSource, CompositeAuditLogger, type Condition, type ConditionOperator, type ConditionResult, ConsoleAuditLogger, DERIVE_EXIT_CODES, type DeriveExitCode, type DeriveFinding, type DeriveResult, type ExplainOutput, FileAuditLogger, type FindingCategory, type FindingSeverity, type FormatVerdictOptions, type GovernanceEngineOptions, GuardEngineOptions, GuardEvent, GuardVerdict, type ImpactReport, type ImprovementReport, type NormalizationSummary, type ParseIssue, type ParsedAssumptionProfile, type ParsedEffect, type ParsedFrontmatter, type ParsedGate, type ParsedInvariant, type ParsedOutcome, type ParsedRule, type ParsedStateVariable, type ParsedTrigger, type ParsedWorld, type PreventionCategory, type RuleEvaluation, type SimulateOptions, type SimulationResult, type SimulationStep, type Suggestion, type SuggestionCategory, type SuggestionPriority, VALIDATE_EXIT_CODES, type ValidateExitCode, type ValidateFinding, type ValidateReport, type ValidateSummary, createGovernanceEngine, deriveWorld, emitWorldDefinition, evaluateCondition, evaluateGuard, eventToAllowlistKey, explainWorld, extractWorldMarkdown, formatVerdict, formatVerdictOneLine, generateImpactReport, generateImpactReportFromFile, improveWorld, loadWorld, loadWorldFromDirectory, normalizeWorldMarkdown, parseWorldMarkdown, readAuditLog, renderExplainText, renderImpactReport, renderImproveText, renderSimulateText, simulateWorld, summarizeAuditEvents, validateWorld, verdictToAuditEvent };
|