@vibecheckai/cli 3.2.0 → 3.2.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/bin/runners/lib/agent-firewall/change-packet/builder.js +214 -0
- package/bin/runners/lib/agent-firewall/change-packet/schema.json +228 -0
- package/bin/runners/lib/agent-firewall/change-packet/store.js +200 -0
- package/bin/runners/lib/agent-firewall/claims/claim-types.js +21 -0
- package/bin/runners/lib/agent-firewall/claims/extractor.js +214 -0
- package/bin/runners/lib/agent-firewall/claims/patterns.js +24 -0
- package/bin/runners/lib/agent-firewall/evidence/auth-evidence.js +88 -0
- package/bin/runners/lib/agent-firewall/evidence/contract-evidence.js +75 -0
- package/bin/runners/lib/agent-firewall/evidence/env-evidence.js +118 -0
- package/bin/runners/lib/agent-firewall/evidence/resolver.js +102 -0
- package/bin/runners/lib/agent-firewall/evidence/route-evidence.js +142 -0
- package/bin/runners/lib/agent-firewall/evidence/side-effect-evidence.js +145 -0
- package/bin/runners/lib/agent-firewall/fs-hook/daemon.js +19 -0
- package/bin/runners/lib/agent-firewall/fs-hook/installer.js +87 -0
- package/bin/runners/lib/agent-firewall/fs-hook/watcher.js +184 -0
- package/bin/runners/lib/agent-firewall/git-hook/pre-commit.js +163 -0
- package/bin/runners/lib/agent-firewall/ide-extension/cursor.js +107 -0
- package/bin/runners/lib/agent-firewall/ide-extension/vscode.js +68 -0
- package/bin/runners/lib/agent-firewall/ide-extension/windsurf.js +66 -0
- package/bin/runners/lib/agent-firewall/interceptor/base.js +304 -0
- package/bin/runners/lib/agent-firewall/interceptor/cursor.js +35 -0
- package/bin/runners/lib/agent-firewall/interceptor/vscode.js +35 -0
- package/bin/runners/lib/agent-firewall/interceptor/windsurf.js +34 -0
- package/bin/runners/lib/agent-firewall/policy/default-policy.json +84 -0
- package/bin/runners/lib/agent-firewall/policy/engine.js +72 -0
- package/bin/runners/lib/agent-firewall/policy/loader.js +143 -0
- package/bin/runners/lib/agent-firewall/policy/rules/auth-drift.js +50 -0
- package/bin/runners/lib/agent-firewall/policy/rules/contract-drift.js +50 -0
- package/bin/runners/lib/agent-firewall/policy/rules/fake-success.js +61 -0
- package/bin/runners/lib/agent-firewall/policy/rules/ghost-env.js +50 -0
- package/bin/runners/lib/agent-firewall/policy/rules/ghost-route.js +50 -0
- package/bin/runners/lib/agent-firewall/policy/rules/scope.js +93 -0
- package/bin/runners/lib/agent-firewall/policy/rules/unsafe-side-effect.js +57 -0
- package/bin/runners/lib/agent-firewall/policy/schema.json +183 -0
- package/bin/runners/lib/agent-firewall/policy/verdict.js +54 -0
- package/bin/runners/lib/agent-firewall/truthpack/index.js +67 -0
- package/bin/runners/lib/agent-firewall/truthpack/loader.js +116 -0
- package/bin/runners/lib/agent-firewall/unblock/planner.js +337 -0
- package/bin/runners/lib/analysis-core.js +198 -180
- package/bin/runners/lib/analyzers.js +1119 -536
- package/bin/runners/lib/cli-output.js +236 -210
- package/bin/runners/lib/detectors-v2.js +547 -785
- package/bin/runners/lib/fingerprint.js +377 -0
- package/bin/runners/lib/route-truth.js +1167 -322
- package/bin/runners/lib/scan-output.js +144 -738
- package/bin/runners/lib/ship-output-enterprise.js +239 -0
- package/bin/runners/lib/terminal-ui.js +188 -770
- package/bin/runners/lib/truth.js +1004 -321
- package/bin/runners/lib/unified-output.js +162 -158
- package/bin/runners/runAgent.js +161 -0
- package/bin/runners/runFirewall.js +134 -0
- package/bin/runners/runFirewallHook.js +56 -0
- package/bin/runners/runScan.js +113 -10
- package/bin/runners/runShip.js +7 -8
- package/bin/runners/runTruth.js +89 -0
- package/mcp-server/agent-firewall-interceptor.js +164 -0
- package/mcp-server/index.js +347 -313
- package/mcp-server/truth-context.js +131 -90
- package/mcp-server/truth-firewall-tools.js +1412 -1045
- package/package.json +1 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Evidence Resolver
|
|
3
|
+
*
|
|
4
|
+
* Resolves route claims against truthpack.routes.json
|
|
5
|
+
* Checks for ghost routes (UI references route not registered).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
const { getRoutes } = require("../truthpack");
|
|
11
|
+
const { canonicalizePath } = require("../../route-truth");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve route claim evidence
|
|
15
|
+
* @param {string} projectRoot - Project root directory
|
|
16
|
+
* @param {object} claim - Route claim
|
|
17
|
+
* @returns {object} Evidence result
|
|
18
|
+
*/
|
|
19
|
+
function resolve(projectRoot, claim) {
|
|
20
|
+
const routes = getRoutes(projectRoot);
|
|
21
|
+
|
|
22
|
+
// Normalize route path from claim
|
|
23
|
+
const routePath = canonicalizePath(claim.value);
|
|
24
|
+
|
|
25
|
+
// Check if route exists in truthpack
|
|
26
|
+
if (routes && routes.length > 0) {
|
|
27
|
+
// First, check for exact match
|
|
28
|
+
const exactMatch = routes.find(route => {
|
|
29
|
+
const routePathNormalized = canonicalizePath(route.path || route);
|
|
30
|
+
return routePathNormalized === routePath;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
if (exactMatch) {
|
|
34
|
+
return {
|
|
35
|
+
result: "PROVEN",
|
|
36
|
+
sources: [{
|
|
37
|
+
type: "truthpack.routes",
|
|
38
|
+
pointer: claim.pointer,
|
|
39
|
+
confidence: 0.9
|
|
40
|
+
}],
|
|
41
|
+
reason: `Route ${routePath} found in truthpack (exact match)`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Then check parameterized routes - but be conservative
|
|
46
|
+
// Only match if it looks like a parameter value (numeric/UUID-like)
|
|
47
|
+
const paramMatch = routes.find(route => {
|
|
48
|
+
const routePathNormalized = canonicalizePath(route.path || route);
|
|
49
|
+
|
|
50
|
+
if (!isParameterizedPath(routePathNormalized)) return false;
|
|
51
|
+
|
|
52
|
+
const routeParts = routePathNormalized.split("/").filter(Boolean);
|
|
53
|
+
const claimParts = routePath.split("/").filter(Boolean);
|
|
54
|
+
|
|
55
|
+
if (routeParts.length !== claimParts.length) return false;
|
|
56
|
+
|
|
57
|
+
// Check if all non-parameter segments match exactly
|
|
58
|
+
for (let i = 0; i < routeParts.length; i++) {
|
|
59
|
+
const rSeg = routeParts[i];
|
|
60
|
+
const cSeg = claimParts[i];
|
|
61
|
+
|
|
62
|
+
// If it's a parameter, check if the concrete value looks like a parameter
|
|
63
|
+
if (rSeg.startsWith(":")) {
|
|
64
|
+
// Only match if it looks like an ID (numeric or UUID-like)
|
|
65
|
+
// Don't match literal words like "create", "update", etc.
|
|
66
|
+
const looksLikeId = /^\d+$/.test(cSeg) || /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(cSeg);
|
|
67
|
+
if (!looksLikeId) return false;
|
|
68
|
+
} else if (rSeg !== cSeg) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return true;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (paramMatch) {
|
|
77
|
+
return {
|
|
78
|
+
result: "PROVEN",
|
|
79
|
+
sources: [{
|
|
80
|
+
type: "truthpack.routes",
|
|
81
|
+
pointer: claim.pointer,
|
|
82
|
+
confidence: 0.7
|
|
83
|
+
}],
|
|
84
|
+
reason: `Route ${routePath} matches parameterized route in truthpack`
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// No match found - ghost route
|
|
89
|
+
return {
|
|
90
|
+
result: "UNPROVEN",
|
|
91
|
+
sources: [],
|
|
92
|
+
reason: `Route ${routePath} not found in truthpack (ghost route)`
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
// No routes in truthpack - cannot prove
|
|
96
|
+
return {
|
|
97
|
+
result: "UNPROVEN",
|
|
98
|
+
sources: [],
|
|
99
|
+
reason: "No routes found in truthpack"
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isParameterizedPath(p) {
|
|
105
|
+
const s = String(p || "").trim();
|
|
106
|
+
return s.includes(":") || s.includes("*");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function matchPath(pattern, concrete) {
|
|
110
|
+
const pat = String(pattern || "").trim();
|
|
111
|
+
const con = String(concrete || "").trim();
|
|
112
|
+
|
|
113
|
+
if (pat === con) return true;
|
|
114
|
+
|
|
115
|
+
// Simple parameterized matching - only match if segments align
|
|
116
|
+
const patParts = pat.split("/").filter(Boolean);
|
|
117
|
+
const conParts = con.split("/").filter(Boolean);
|
|
118
|
+
|
|
119
|
+
if (patParts.length !== conParts.length) return false;
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < patParts.length; i++) {
|
|
122
|
+
const pSeg = patParts[i];
|
|
123
|
+
const cSeg = conParts[i];
|
|
124
|
+
|
|
125
|
+
// Parameter placeholder (:id, :slug) matches any segment
|
|
126
|
+
if (pSeg.startsWith(":")) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
// Wildcard (*slug) matches remainder
|
|
130
|
+
if (pSeg.startsWith("*")) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
// Exact match required for literal segments
|
|
134
|
+
if (pSeg !== cSeg) return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
resolve
|
|
142
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side Effect Evidence Resolver
|
|
3
|
+
*
|
|
4
|
+
* Detects unverified side effects (DB writes, email, payments).
|
|
5
|
+
* Checks for test coverage or reality proof.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
const fs = require("fs");
|
|
11
|
+
const path = require("path");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve side effect claim evidence
|
|
15
|
+
* @param {string} projectRoot - Project root directory
|
|
16
|
+
* @param {object} claim - Side effect claim
|
|
17
|
+
* @returns {object} Evidence result
|
|
18
|
+
*/
|
|
19
|
+
function resolve(projectRoot, claim) {
|
|
20
|
+
const claimFile = claim.file || "";
|
|
21
|
+
const claimValue = claim.value.toLowerCase();
|
|
22
|
+
|
|
23
|
+
// Detect side effect types
|
|
24
|
+
const hasDbWrite = /\b(create|update|delete|insert|save|write)\b/i.test(claimValue) ||
|
|
25
|
+
/\b(prisma|sequelize|mongoose|db\.|database\.)\b/i.test(claimValue);
|
|
26
|
+
|
|
27
|
+
const hasEmail = /\b(email|sendMail|nodemailer|sendgrid|mailgun)\b/i.test(claimValue);
|
|
28
|
+
|
|
29
|
+
const hasPayment = /\b(stripe|payment|charge|checkout|billing)\b/i.test(claimValue);
|
|
30
|
+
|
|
31
|
+
if (!hasDbWrite && !hasEmail && !hasPayment) {
|
|
32
|
+
// Not a side effect
|
|
33
|
+
return {
|
|
34
|
+
result: "PROVEN",
|
|
35
|
+
sources: [],
|
|
36
|
+
reason: "No side effect detected"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check for test coverage
|
|
41
|
+
const testFile = findTestFile(projectRoot, claimFile);
|
|
42
|
+
if (testFile && fs.existsSync(testFile)) {
|
|
43
|
+
const testContent = fs.readFileSync(testFile, "utf8");
|
|
44
|
+
// Check if test covers the side effect
|
|
45
|
+
if (testContent.includes(claimValue.slice(0, 20)) ||
|
|
46
|
+
testContent.includes("mock") ||
|
|
47
|
+
testContent.includes("test")) {
|
|
48
|
+
return {
|
|
49
|
+
result: "PROVEN",
|
|
50
|
+
sources: [{
|
|
51
|
+
type: "repo.search",
|
|
52
|
+
pointer: testFile,
|
|
53
|
+
confidence: 0.7
|
|
54
|
+
}],
|
|
55
|
+
reason: "Test file found for side effect"
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Check for reality proof (reality report)
|
|
61
|
+
const realityReport = findRealityReport(projectRoot, claimFile);
|
|
62
|
+
if (realityReport) {
|
|
63
|
+
return {
|
|
64
|
+
result: "PROVEN",
|
|
65
|
+
sources: [{
|
|
66
|
+
type: "repo.search",
|
|
67
|
+
pointer: realityReport,
|
|
68
|
+
confidence: 0.8
|
|
69
|
+
}],
|
|
70
|
+
reason: "Reality proof found for side effect"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Side effect detected but no verification found
|
|
75
|
+
return {
|
|
76
|
+
result: "UNPROVEN",
|
|
77
|
+
sources: [],
|
|
78
|
+
reason: `Side effect detected (${hasDbWrite ? 'DB' : ''}${hasEmail ? 'Email' : ''}${hasPayment ? 'Payment' : ''}) but no test coverage or reality proof found`
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Find test file for a source file
|
|
84
|
+
* @param {string} projectRoot - Project root directory
|
|
85
|
+
* @param {string} sourceFile - Source file path
|
|
86
|
+
* @returns {string|null} Test file path or null
|
|
87
|
+
*/
|
|
88
|
+
function findTestFile(projectRoot, sourceFile) {
|
|
89
|
+
if (!sourceFile) return null;
|
|
90
|
+
|
|
91
|
+
// Try common test file patterns
|
|
92
|
+
const baseName = path.basename(sourceFile, path.extname(sourceFile));
|
|
93
|
+
const dir = path.dirname(sourceFile);
|
|
94
|
+
|
|
95
|
+
const patterns = [
|
|
96
|
+
`${dir}/${baseName}.test.ts`,
|
|
97
|
+
`${dir}/${baseName}.test.tsx`,
|
|
98
|
+
`${dir}/${baseName}.test.js`,
|
|
99
|
+
`${dir}/__tests__/${baseName}.test.ts`,
|
|
100
|
+
`${dir}/__tests__/${baseName}.test.tsx`,
|
|
101
|
+
`${dir.replace(/\/src\//, '/tests/')}/${baseName}.test.ts`
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
for (const pattern of patterns) {
|
|
105
|
+
const testPath = path.join(projectRoot, pattern);
|
|
106
|
+
if (fs.existsSync(testPath)) {
|
|
107
|
+
return pattern;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Find reality report for a file
|
|
116
|
+
* @param {string} projectRoot - Project root directory
|
|
117
|
+
* @param {string} sourceFile - Source file path
|
|
118
|
+
* @returns {string|null} Reality report path or null
|
|
119
|
+
*/
|
|
120
|
+
function findRealityReport(projectRoot, sourceFile) {
|
|
121
|
+
const realityDir = path.join(projectRoot, ".vibecheck", "reality");
|
|
122
|
+
|
|
123
|
+
if (!fs.existsSync(realityDir)) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Look for recent reality reports
|
|
128
|
+
const reports = fs.readdirSync(realityDir)
|
|
129
|
+
.filter(file => file.endsWith('.json'))
|
|
130
|
+
.map(file => path.join(realityDir, file))
|
|
131
|
+
.filter(file => {
|
|
132
|
+
try {
|
|
133
|
+
const report = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
134
|
+
return report.files && report.files.includes(sourceFile);
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return reports.length > 0 ? reports[0] : null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports = {
|
|
144
|
+
resolve
|
|
145
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* File System Hook Daemon
|
|
4
|
+
*
|
|
5
|
+
* Runs as a background process to intercept file writes.
|
|
6
|
+
* Start with: node bin/runners/lib/agent-firewall/fs-hook/daemon.js
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const path = require("path");
|
|
10
|
+
const { startFileSystemHook } = require("./installer");
|
|
11
|
+
|
|
12
|
+
const projectRoot = process.cwd();
|
|
13
|
+
|
|
14
|
+
console.log("🛡️ Starting Agent Firewall File System Hook...\n");
|
|
15
|
+
console.log(` Project: ${projectRoot}\n`);
|
|
16
|
+
|
|
17
|
+
startFileSystemHook(projectRoot);
|
|
18
|
+
|
|
19
|
+
console.log("✅ File System Hook running (press Ctrl+C to stop)\n");
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File System Hook Installer
|
|
3
|
+
*
|
|
4
|
+
* Installs and manages the file system hook daemon.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
const fs = require("fs");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
const { FileSystemHook } = require("./watcher");
|
|
12
|
+
|
|
13
|
+
let globalHook = null;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Install file system hook
|
|
17
|
+
* @param {string} projectRoot - Project root directory
|
|
18
|
+
* @returns {object} Installation result
|
|
19
|
+
*/
|
|
20
|
+
function installFileSystemHook(projectRoot) {
|
|
21
|
+
const hookScript = path.join(__dirname, "daemon.js");
|
|
22
|
+
const packageJson = path.join(projectRoot, "package.json");
|
|
23
|
+
|
|
24
|
+
// Add script to package.json
|
|
25
|
+
if (fs.existsSync(packageJson)) {
|
|
26
|
+
const pkg = JSON.parse(fs.readFileSync(packageJson, "utf8"));
|
|
27
|
+
if (!pkg.scripts) {
|
|
28
|
+
pkg.scripts = {};
|
|
29
|
+
}
|
|
30
|
+
pkg.scripts["firewall:fs-hook"] = `node ${path.relative(projectRoot, hookScript)}`;
|
|
31
|
+
fs.writeFileSync(packageJson, JSON.stringify(pkg, null, 2));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Create marker file
|
|
35
|
+
const markerFile = path.join(projectRoot, ".vibecheck", "fs-hook-enabled");
|
|
36
|
+
fs.writeFileSync(markerFile, JSON.stringify({
|
|
37
|
+
enabled: true,
|
|
38
|
+
installedAt: new Date().toISOString()
|
|
39
|
+
}, null, 2));
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
success: true,
|
|
43
|
+
message: "File system hook installed. Run 'npm run firewall:fs-hook' to start."
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Start file system hook daemon
|
|
49
|
+
* @param {string} projectRoot - Project root directory
|
|
50
|
+
*/
|
|
51
|
+
function startFileSystemHook(projectRoot) {
|
|
52
|
+
if (globalHook) {
|
|
53
|
+
console.log("⚠️ File system hook already running");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
globalHook = new FileSystemHook(projectRoot);
|
|
58
|
+
globalHook.start().catch(console.error);
|
|
59
|
+
|
|
60
|
+
// Keep process alive
|
|
61
|
+
process.on("SIGINT", () => {
|
|
62
|
+
if (globalHook) {
|
|
63
|
+
globalHook.stop();
|
|
64
|
+
}
|
|
65
|
+
process.exit(0);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Keep alive
|
|
69
|
+
setInterval(() => {}, 1000);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Stop file system hook
|
|
74
|
+
*/
|
|
75
|
+
function stopFileSystemHook() {
|
|
76
|
+
if (globalHook) {
|
|
77
|
+
globalHook.stop();
|
|
78
|
+
globalHook = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = {
|
|
83
|
+
installFileSystemHook,
|
|
84
|
+
startFileSystemHook,
|
|
85
|
+
stopFileSystemHook,
|
|
86
|
+
FileSystemHook
|
|
87
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File System Hook - Intercepts ALL file writes at OS level
|
|
3
|
+
*
|
|
4
|
+
* Uses chokidar to watch for file changes and intercepts them
|
|
5
|
+
* before they're written to disk.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
const chokidar = require("chokidar");
|
|
11
|
+
const fs = require("fs");
|
|
12
|
+
const path = require("path");
|
|
13
|
+
const { interceptFileWrite } = require("../interceptor/base");
|
|
14
|
+
const { loadPolicy } = require("../policy/loader");
|
|
15
|
+
|
|
16
|
+
class FileSystemHook {
|
|
17
|
+
constructor(projectRoot) {
|
|
18
|
+
this.projectRoot = projectRoot;
|
|
19
|
+
this.watcher = null;
|
|
20
|
+
this.pendingWrites = new Map(); // Track writes in progress
|
|
21
|
+
this.isEnabled = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Start watching for file writes
|
|
26
|
+
*/
|
|
27
|
+
async start() {
|
|
28
|
+
if (this.watcher) {
|
|
29
|
+
return; // Already watching
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const policy = loadPolicy(this.projectRoot);
|
|
33
|
+
if (policy.mode !== "enforce") {
|
|
34
|
+
console.log("⚠️ File system hook only active in enforce mode");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.isEnabled = true;
|
|
39
|
+
|
|
40
|
+
// Watch for file changes
|
|
41
|
+
this.watcher = chokidar.watch([
|
|
42
|
+
"**/*.ts",
|
|
43
|
+
"**/*.tsx",
|
|
44
|
+
"**/*.js",
|
|
45
|
+
"**/*.jsx",
|
|
46
|
+
"**/*.py",
|
|
47
|
+
"**/*.go",
|
|
48
|
+
"**/*.rs"
|
|
49
|
+
], {
|
|
50
|
+
cwd: this.projectRoot,
|
|
51
|
+
ignored: [
|
|
52
|
+
"**/node_modules/**",
|
|
53
|
+
"**/dist/**",
|
|
54
|
+
"**/.next/**",
|
|
55
|
+
"**/.vibecheck/**",
|
|
56
|
+
"**/.git/**",
|
|
57
|
+
"**/build/**"
|
|
58
|
+
],
|
|
59
|
+
persistent: true,
|
|
60
|
+
ignoreInitial: true,
|
|
61
|
+
awaitWriteFinish: {
|
|
62
|
+
stabilityThreshold: 100,
|
|
63
|
+
pollInterval: 50
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Intercept file writes
|
|
68
|
+
this.watcher.on("add", (filePath) => this.handleFileWrite(filePath, "create"));
|
|
69
|
+
this.watcher.on("change", (filePath) => this.handleFileWrite(filePath, "modify"));
|
|
70
|
+
|
|
71
|
+
console.log("🛡️ File System Hook ACTIVE - intercepting all file writes");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Handle file write event
|
|
76
|
+
*/
|
|
77
|
+
async handleFileWrite(filePath, operation) {
|
|
78
|
+
// Skip if we're already processing this file
|
|
79
|
+
if (this.pendingWrites.has(filePath)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.pendingWrites.set(filePath, true);
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
// Read the new content
|
|
87
|
+
const fileAbs = path.join(this.projectRoot, filePath);
|
|
88
|
+
if (!fs.existsSync(fileAbs)) {
|
|
89
|
+
this.pendingWrites.delete(filePath);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const newContent = fs.readFileSync(fileAbs, "utf8");
|
|
94
|
+
|
|
95
|
+
// Try to get old content from git or backup
|
|
96
|
+
let oldContent = null;
|
|
97
|
+
try {
|
|
98
|
+
// Check if file existed before (for modifications)
|
|
99
|
+
if (operation === "modify") {
|
|
100
|
+
// Try to read from git index
|
|
101
|
+
const { execSync } = require("child_process");
|
|
102
|
+
try {
|
|
103
|
+
oldContent = execSync(
|
|
104
|
+
`git show :${filePath}`,
|
|
105
|
+
{ cwd: this.projectRoot, encoding: "utf8", stdio: "pipe" }
|
|
106
|
+
);
|
|
107
|
+
} catch {
|
|
108
|
+
// File not in git, that's okay
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
// Couldn't get old content, that's okay
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Intercept the write
|
|
116
|
+
const result = await interceptFileWrite({
|
|
117
|
+
projectRoot: this.projectRoot,
|
|
118
|
+
agentId: "filesystem-hook",
|
|
119
|
+
intent: `File ${operation} via filesystem`,
|
|
120
|
+
filePath: filePath,
|
|
121
|
+
content: newContent,
|
|
122
|
+
oldContent: oldContent
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const policy = loadPolicy(this.projectRoot);
|
|
126
|
+
|
|
127
|
+
// If blocked and in enforce mode, revert the file
|
|
128
|
+
if (!result.allowed && policy.mode === "enforce") {
|
|
129
|
+
console.error(`\n❌ BLOCKED: ${filePath}`);
|
|
130
|
+
console.error(` ${result.message}`);
|
|
131
|
+
|
|
132
|
+
if (result.violations) {
|
|
133
|
+
result.violations.forEach(v => {
|
|
134
|
+
console.error(` - ${v.rule}: ${v.message}`);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Revert the file
|
|
139
|
+
if (oldContent !== null) {
|
|
140
|
+
fs.writeFileSync(fileAbs, oldContent, "utf8");
|
|
141
|
+
console.error(` ✅ File reverted to previous version\n`);
|
|
142
|
+
} else {
|
|
143
|
+
// Delete the file if it was newly created
|
|
144
|
+
fs.unlinkSync(fileAbs);
|
|
145
|
+
console.error(` ✅ File deleted (was newly created)\n`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Show unblock plan
|
|
149
|
+
if (result.unblockPlan && result.unblockPlan.steps.length > 0) {
|
|
150
|
+
console.error(" To fix:");
|
|
151
|
+
result.unblockPlan.steps.forEach((step, i) => {
|
|
152
|
+
console.error(` ${i + 1}. ${step.action}: ${step.description}`);
|
|
153
|
+
});
|
|
154
|
+
console.error("");
|
|
155
|
+
}
|
|
156
|
+
} else if (result.allowed || policy.mode === "observe") {
|
|
157
|
+
// Log in observe mode
|
|
158
|
+
if (policy.mode === "observe" && result.violations && result.violations.length > 0) {
|
|
159
|
+
console.log(`📊 OBSERVE: ${filePath} - violations logged (not blocked)`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.error(`Error intercepting file write: ${error.message}`);
|
|
164
|
+
} finally {
|
|
165
|
+
this.pendingWrites.delete(filePath);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Stop watching
|
|
171
|
+
*/
|
|
172
|
+
stop() {
|
|
173
|
+
if (this.watcher) {
|
|
174
|
+
this.watcher.close();
|
|
175
|
+
this.watcher = null;
|
|
176
|
+
this.isEnabled = false;
|
|
177
|
+
console.log("🛡️ File System Hook stopped");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports = {
|
|
183
|
+
FileSystemHook
|
|
184
|
+
};
|