@neuroverseos/governance 0.1.0

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.
@@ -0,0 +1,228 @@
1
+ "use strict";
2
+ /**
3
+ * Drift Monitor — Background service tracking governance health
4
+ *
5
+ * Tracks:
6
+ * - Block frequency (are we blocking too much? too little?)
7
+ * - Manual overrides (humans overriding PAUSE decisions)
8
+ * - Tool friction (which tools cause the most governance friction?)
9
+ * - Rule friction (which rules fire most often?)
10
+ *
11
+ * Can also read from audit.jsonl for historical analysis.
12
+ * Stored in .neuroverseos/state.json — separate from MEMORY.md.
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DriftMonitor = void 0;
16
+ const fs_1 = require("fs");
17
+ // ────────────────────────────────────────────────────────────────────────
18
+ // Monitor
19
+ // ────────────────────────────────────────────────────────────────────────
20
+ class DriftMonitor {
21
+ constructor(config) {
22
+ this.saveInterval = null;
23
+ this.config = config;
24
+ this.state = this.loadState();
25
+ }
26
+ start() {
27
+ this.saveInterval = setInterval(() => this.saveState(), 30000);
28
+ }
29
+ stop() {
30
+ if (this.saveInterval) {
31
+ clearInterval(this.saveInterval);
32
+ this.saveInterval = null;
33
+ }
34
+ this.saveState();
35
+ }
36
+ /**
37
+ * Record an action and its governance verdict.
38
+ */
39
+ recordAction(tool, verdict, ruleId) {
40
+ this.state.totalActions++;
41
+ switch (verdict) {
42
+ case 'ALLOW':
43
+ this.state.allowCount++;
44
+ break;
45
+ case 'PAUSE':
46
+ this.state.pauseCount++;
47
+ break;
48
+ case 'BLOCK':
49
+ this.state.blockCount++;
50
+ break;
51
+ }
52
+ // Tool friction
53
+ if (!this.state.toolFriction[tool]) {
54
+ this.state.toolFriction[tool] = { blocks: 0, pauses: 0, allows: 0 };
55
+ }
56
+ const tf = this.state.toolFriction[tool];
57
+ if (verdict === 'BLOCK')
58
+ tf.blocks++;
59
+ else if (verdict === 'PAUSE')
60
+ tf.pauses++;
61
+ else
62
+ tf.allows++;
63
+ // Rule friction
64
+ if (ruleId) {
65
+ this.state.ruleFriction[ruleId] = (this.state.ruleFriction[ruleId] ?? 0) + 1;
66
+ }
67
+ this.state.lastUpdated = Date.now();
68
+ }
69
+ /**
70
+ * Record when a human overrides a PAUSE decision.
71
+ */
72
+ recordOverride() {
73
+ this.state.overrideCount++;
74
+ }
75
+ /**
76
+ * Get current drift statistics.
77
+ */
78
+ getStats() {
79
+ const total = this.state.totalActions || 1;
80
+ const blockRate = this.state.blockCount / total;
81
+ const pauseRate = this.state.pauseCount / total;
82
+ const overrideRate = this.state.pauseCount > 0
83
+ ? this.state.overrideCount / this.state.pauseCount
84
+ : 0;
85
+ const driftSignals = [];
86
+ if (blockRate > 0.3) {
87
+ driftSignals.push(`High block rate (${(blockRate * 100).toFixed(1)}%) — governance may be too strict`);
88
+ }
89
+ if (overrideRate > 0.5 && this.state.pauseCount >= 5) {
90
+ driftSignals.push(`High override rate (${(overrideRate * 100).toFixed(1)}%) — world rules may need amendment`);
91
+ }
92
+ for (const [tool, friction] of Object.entries(this.state.toolFriction)) {
93
+ const toolTotal = friction.blocks + friction.pauses + friction.allows;
94
+ const toolBlockRate = friction.blocks / Math.max(toolTotal, 1);
95
+ if (toolBlockRate > 0.5 && toolTotal >= 3) {
96
+ driftSignals.push(`Tool "${tool}" blocked ${(toolBlockRate * 100).toFixed(0)}% of the time`);
97
+ }
98
+ }
99
+ if (total > 20 && blockRate === 0 && pauseRate === 0) {
100
+ driftSignals.push('Zero blocks or pauses — governance may be too permissive');
101
+ }
102
+ return {
103
+ totalActions: this.state.totalActions,
104
+ allowCount: this.state.allowCount,
105
+ pauseCount: this.state.pauseCount,
106
+ blockCount: this.state.blockCount,
107
+ blockRate,
108
+ pauseRate,
109
+ overrideRate,
110
+ driftSignals,
111
+ };
112
+ }
113
+ /**
114
+ * Generate amendment proposals based on drift patterns.
115
+ */
116
+ generateProposals() {
117
+ const proposals = [];
118
+ const stats = this.getStats();
119
+ // High override rate → suggest loosening specific guards
120
+ if (stats.overrideRate > 0.5 && this.state.pauseCount >= 5) {
121
+ const topRule = Object.entries(this.state.ruleFriction)
122
+ .sort(([, a], [, b]) => b - a)[0];
123
+ if (topRule) {
124
+ proposals.push({
125
+ type: 'modify_threshold',
126
+ reason: `Rule "${topRule[0]}" triggered ${topRule[1]} times with ${(stats.overrideRate * 100).toFixed(0)}% override rate`,
127
+ suggestion: topRule[0],
128
+ evidence: `${(stats.overrideRate * 100).toFixed(0)}% of pauses were manually overridden`,
129
+ });
130
+ }
131
+ }
132
+ // Tool constantly blocked → suggest more nuanced guard
133
+ for (const [tool, friction] of Object.entries(this.state.toolFriction)) {
134
+ const total = friction.blocks + friction.pauses + friction.allows;
135
+ if (friction.blocks > 5 && friction.blocks / total > 0.7) {
136
+ proposals.push({
137
+ type: 'add_guard',
138
+ reason: `Tool "${tool}" is blocked ${friction.blocks} out of ${total} times`,
139
+ suggestion: `Add granular guard for "${tool}" that allows safe patterns and blocks dangerous ones`,
140
+ evidence: `${tool}: ${friction.blocks} blocks, ${friction.pauses} pauses, ${friction.allows} allows`,
141
+ });
142
+ }
143
+ }
144
+ // Zero governance firing → suggest adding guards
145
+ if (this.state.totalActions > 20 && stats.blockRate === 0 && stats.pauseRate === 0) {
146
+ proposals.push({
147
+ type: 'add_guard',
148
+ reason: 'No governance constraints have fired in over 20 actions',
149
+ suggestion: 'Consider adding guards for destructive operations, external communication, or production access',
150
+ evidence: `${this.state.totalActions} actions with 0 blocks and 0 pauses`,
151
+ });
152
+ }
153
+ return proposals;
154
+ }
155
+ /**
156
+ * Rebuild stats from the audit log (for historical analysis).
157
+ */
158
+ rebuildFromAudit() {
159
+ if (!(0, fs_1.existsSync)(this.config.auditPath))
160
+ return;
161
+ try {
162
+ const raw = (0, fs_1.readFileSync)(this.config.auditPath, 'utf-8');
163
+ const lines = raw.split('\n').filter(l => l.trim());
164
+ // Reset state
165
+ this.state = this.freshState();
166
+ let decisionCount = 0;
167
+ for (const line of lines) {
168
+ try {
169
+ const entry = JSON.parse(line);
170
+ // Decision entries
171
+ if (entry.type === 'decision') {
172
+ const dec = entry;
173
+ if (dec.decision === 'allow-once' || dec.decision === 'allow-always') {
174
+ decisionCount++;
175
+ }
176
+ continue;
177
+ }
178
+ // Verdict entries
179
+ const verdict = entry;
180
+ this.recordAction(verdict.tool, verdict.status, verdict.ruleId ?? undefined);
181
+ }
182
+ catch {
183
+ continue;
184
+ }
185
+ }
186
+ this.state.overrideCount = decisionCount;
187
+ this.saveState();
188
+ }
189
+ catch {
190
+ // Silent failure
191
+ }
192
+ }
193
+ // ── Persistence ────────────────────────────────────────────────
194
+ loadState() {
195
+ if ((0, fs_1.existsSync)(this.config.statePath)) {
196
+ try {
197
+ return JSON.parse((0, fs_1.readFileSync)(this.config.statePath, 'utf-8'));
198
+ }
199
+ catch {
200
+ // Corrupted state — start fresh
201
+ }
202
+ }
203
+ return this.freshState();
204
+ }
205
+ freshState() {
206
+ return {
207
+ totalActions: 0,
208
+ allowCount: 0,
209
+ pauseCount: 0,
210
+ blockCount: 0,
211
+ overrideCount: 0,
212
+ toolFriction: {},
213
+ ruleFriction: {},
214
+ lastUpdated: Date.now(),
215
+ sessionStart: Date.now(),
216
+ };
217
+ }
218
+ saveState() {
219
+ try {
220
+ (0, fs_1.writeFileSync)(this.config.statePath, JSON.stringify(this.state, null, 2));
221
+ }
222
+ catch {
223
+ // Silent failure — don't crash on state save
224
+ }
225
+ }
226
+ }
227
+ exports.DriftMonitor = DriftMonitor;
228
+ //# sourceMappingURL=drift-monitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drift-monitor.js","sourceRoot":"","sources":["../drift-monitor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAEH,2BAA6D;AA4C7D,2EAA2E;AAC3E,UAAU;AACV,2EAA2E;AAE3E,MAAa,YAAY;IAKvB,YAAY,MAA0B;QAF9B,iBAAY,GAA0C,IAAI,CAAC;QAGjE,IAAI,CAAC,MA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SACzB,CAAC;IACJ,CAAC;IAEO,SAAS;QACf,IAAI,CAAC;YACH,IAAA,kBAAa,EAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QAC5E,CAAC;QAAC,MAAM,CAAC;YACP,6CAA6C;QAC/C,CAAC;IACH,CAAC;CACF;AA1OD,oCA0OC"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Governance Engine — Deterministic decision pipeline
3
+ *
4
+ * Evaluates tool calls against world rules in strict order:
5
+ * 1. Session allowlist → ALLOW silently if guard already approved
6
+ * 2. Invariants → any hit = BLOCK (immediate, no override)
7
+ * 3. Guards → condition engine + tool filter → PAUSE or BLOCK
8
+ * 4. Rules → trigger evaluation → verdict override
9
+ * 5. Role constraints → cannotDo = BLOCK, requiresApproval = PAUSE
10
+ * 6. Default → ALLOW
11
+ *
12
+ * Same world + same event = same verdict. Always.
13
+ * No AI. No network calls. Sub-millisecond per evaluation.
14
+ */
15
+ import type { GovernanceWorld, GovernanceVerdict, ActiveWorldRecord, RoleBinding, BindingChangeSeverity, ToolCallEvent, EngineConfig, EngineStatus } from './types';
16
+ export declare class GovernanceEngine {
17
+ private config;
18
+ private world;
19
+ private worldMtimeMs;
20
+ private cachedWorldHash;
21
+ private metaRecord;
22
+ private surfacedAlerts;
23
+ private workspaceDir;
24
+ constructor(config: EngineConfig);
25
+ /** Set the workspace directory for md drift detection */
26
+ setWorkspaceDir(dir: string): void;
27
+ /** Clear surfaced alerts (e.g. on session reset) */
28
+ clearAlertHistory(): void;
29
+ loadWorld(): void;
30
+ getWorld(): GovernanceWorld | null;
31
+ setWorld(world: GovernanceWorld): void;
32
+ saveWorld(): void;
33
+ getStatus(): EngineStatus;
34
+ /** Load or create world.meta.json for integrity tracking */
35
+ loadMeta(): void;
36
+ saveMeta(): void;
37
+ getMetaRecord(): ActiveWorldRecord | null;
38
+ setMetaRecord(record: ActiveWorldRecord): void;
39
+ /**
40
+ * Get all role bindings from the meta record.
41
+ * Returns empty array if no bindings exist.
42
+ */
43
+ getRoleBindings(): RoleBinding[];
44
+ /**
45
+ * Hydrate a Map<agentId, roleId> from stored bindings.
46
+ * Called at startup and after any binding change.
47
+ */
48
+ hydrateRoleMap(roleMap: Map<string, string>): void;
49
+ /**
50
+ * Add or update a role binding. Returns the severity of the change.
51
+ * Does NOT auto-save — caller must call saveMeta().
52
+ */
53
+ setRoleBinding(agentId: string, roleId: string, boundBy?: RoleBinding['boundBy']): BindingChangeSeverity;
54
+ /**
55
+ * Remove a role binding. Returns 'removal' severity.
56
+ * Does NOT auto-save — caller must call saveMeta().
57
+ */
58
+ removeRoleBinding(agentId: string): boolean;
59
+ /**
60
+ * Classify the severity of changing from one role to another.
61
+ * Uses the role's requiresApproval and cannotDo as proxy for privilege level.
62
+ */
63
+ private classifyBindingChange;
64
+ /**
65
+ * Estimate privilege level of a role (higher = more permissive).
66
+ * Simple heuristic: canDo breadth - cannotDo restrictions - approval overhead.
67
+ */
68
+ private estimatePrivilege;
69
+ private getMetaPath;
70
+ /** Canonical SHA-256 hash of a world object (sorted keys, no whitespace) */
71
+ computeWorldHash(world: GovernanceWorld): string;
72
+ /**
73
+ * System-level protection: intercept modifications to governance source files.
74
+ *
75
+ * This runs BEFORE all user-defined rules and cannot be removed by
76
+ * modifying world.json. It is the engine's self-protection layer.
77
+ *
78
+ * - write_file / edit_file targeting .neuroverseos/ → BLOCK
79
+ * - write_file / edit_file targeting .md files → PAUSE
80
+ * - shell commands referencing .neuroverseos/ → BLOCK
81
+ *
82
+ * The PAUSE verdict flows through the existing interactive prompt in the
83
+ * before_tool_call hook. Session overrides ("always") are handled there.
84
+ */
85
+ private checkGovernanceSourceProtection;
86
+ /**
87
+ * Run all integrity checks before the governance pipeline.
88
+ * Returns accumulated alerts. Critical alerts override the verdict.
89
+ */
90
+ private checkIntegrity;
91
+ /**
92
+ * Verify world.json hash against stored hash.
93
+ * Uses mtime cache to avoid re-reading on every call.
94
+ */
95
+ private verifyWorldHash;
96
+ /**
97
+ * Evaluate a tool call event against the loaded world.
98
+ * Returns a deterministic verdict: ALLOW, PAUSE, or BLOCK.
99
+ * Now includes runtime integrity checks (spec §8).
100
+ */
101
+ evaluate(event: ToolCallEvent): GovernanceVerdict;
102
+ /**
103
+ * The actual ordered pipeline. This is the core of governance.
104
+ */
105
+ private runPipeline;
106
+ /**
107
+ * Step 2: Invariants. Non-toggleable. Non-overridable. Hard BLOCK.
108
+ */
109
+ private checkInvariants;
110
+ /**
111
+ * Step 3: Guards. Scoped + tool-filtered. Can PAUSE or BLOCK.
112
+ */
113
+ private checkGuards;
114
+ /**
115
+ * Step 4: Rules. Contextual triggers with verdict effects.
116
+ */
117
+ private checkRules;
118
+ /**
119
+ * Step 5: Role constraints. cannotDo = BLOCK, requiresApproval = PAUSE.
120
+ */
121
+ private checkRoles;
122
+ /**
123
+ * Apply an approved amendment to the world.
124
+ */
125
+ applyAmendment(proposal: {
126
+ type: 'add_guard' | 'modify_threshold' | 'add_role' | 'add_invariant';
127
+ suggestion: string;
128
+ reason: string;
129
+ }): void;
130
+ }
131
+ //# sourceMappingURL=governance-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"governance-engine.d.ts","sourceRoot":"","sources":["../governance-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAOH,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EAEjB,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,aAAa,EACb,YAAY,EACZ,YAAY,EACb,MAAM,SAAS,CAAC;AA0BjB,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,KAAK,CAAgC;IAG7C,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,UAAU,CAAkC;IACpD,OAAO,CAAC,cAAc,CAA0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