buildproof 0.0.0-stage → 3.8.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.
- package/LICENSE.txt +2 -0
- package/README.md +25 -2
- package/bin/buildproof.js +267 -0
- package/core/ai.js +193 -0
- package/core/av-scan.js +66 -0
- package/core/bug-hunter/engine.js +4 -0
- package/core/certs.js +194 -0
- package/core/ci.js +254 -0
- package/core/claims.js +53 -0
- package/core/config.js +57 -0
- package/core/deps-guard.js +49 -0
- package/core/edits.js +97 -0
- package/core/fixlib.js +93 -0
- package/core/flow.js +852 -0
- package/core/gate.js +193 -0
- package/core/hooks.js +120 -0
- package/core/integrity.js +321 -0
- package/core/keep-corrupt.js +17 -0
- package/core/license.js +165 -0
- package/core/machine-proof.js +27 -0
- package/core/project-lock.js +31 -0
- package/core/projects.js +180 -0
- package/core/proof-score.js +11 -0
- package/core/runner.js +46 -0
- package/core/scope.js +153 -0
- package/core/security-scan.js +68 -0
- package/core/server.js +322 -0
- package/core/service.js +89 -0
- package/core/snapshot.js +90 -0
- package/core/store.js +45 -0
- package/core/vault.js +77 -0
- package/license/.keep +0 -0
- package/license/vendor.pub.pem +3 -0
- package/package.json +39 -4
- package/scripts/guard.js +311 -0
- package/web/apple-touch-icon.png +0 -0
- package/web/favicon-32.png +0 -0
- package/web/html2canvas.min.js +20 -0
- package/web/icon-192.png +0 -0
- package/web/icon-512.png +0 -0
- package/web/index.html +834 -0
- package/web/manifest.webmanifest +12 -0
package/core/edits.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Safe edits: every write is backed up OUTSIDE the project (data/backups/<taskId>/...),
|
|
4
|
+
* so rollback is always possible and backups never pollute the project.
|
|
5
|
+
*/
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { DATA_DIR } = require('./config');
|
|
9
|
+
|
|
10
|
+
function isInside(root, abs) {
|
|
11
|
+
const rel = path.relative(path.resolve(root), path.resolve(abs));
|
|
12
|
+
return !!rel && !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function backupDir(taskId) { return path.join(DATA_DIR, 'backups', taskId); }
|
|
16
|
+
|
|
17
|
+
/** Back up a project file once per task (first version wins = pre-task state). */
|
|
18
|
+
function backupFile(root, taskId, rel) {
|
|
19
|
+
const abs = path.join(root, rel);
|
|
20
|
+
const dst = path.join(backupDir(taskId), 'files', rel);
|
|
21
|
+
const manifestPath = path.join(backupDir(taskId), 'manifest.json');
|
|
22
|
+
let manifest = {};
|
|
23
|
+
try { manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); } catch { manifest = {}; }
|
|
24
|
+
if (manifest[rel]) return manifest[rel];
|
|
25
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
26
|
+
const existed = fs.existsSync(abs);
|
|
27
|
+
if (existed) fs.copyFileSync(abs, dst);
|
|
28
|
+
manifest[rel] = { existed };
|
|
29
|
+
fs.mkdirSync(backupDir(taskId), { recursive: true });
|
|
30
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
|
|
31
|
+
return manifest[rel];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Write new full content for files. Rejects anything outside root or outside allowed list. */
|
|
35
|
+
function applyEdits(root, taskId, edits, allowed) {
|
|
36
|
+
const allowedSet = new Set((allowed || []).map((f) => f.replace(/\\/g, '/')));
|
|
37
|
+
const applied = [];
|
|
38
|
+
const rejected = [];
|
|
39
|
+
for (const e of edits || []) {
|
|
40
|
+
const rel = String(e.path || '').replace(/\\/g, '/').replace(/^\.\//, '');
|
|
41
|
+
const abs = path.resolve(root, rel);
|
|
42
|
+
if (!rel || !isInside(root, abs)) { rejected.push({ file: rel, reason: 'outside_project' }); continue; }
|
|
43
|
+
if (allowed && !allowedSet.has(rel)) { rejected.push({ file: rel, reason: 'outside_approved_scope' }); continue; }
|
|
44
|
+
const before = fs.existsSync(abs) ? fs.readFileSync(abs, 'utf8') : null;
|
|
45
|
+
backupFile(root, taskId, rel);
|
|
46
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
47
|
+
fs.writeFileSync(abs, String(e.content == null ? '' : e.content), 'utf8');
|
|
48
|
+
applied.push({ file: rel, diff: unifiedDiff(before || '', String(e.content || ''), rel) });
|
|
49
|
+
}
|
|
50
|
+
return { applied, rejected };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Restore given files (or all backed-up files) to pre-task state. */
|
|
54
|
+
function restore(root, taskId, files) {
|
|
55
|
+
const manifestPath = path.join(backupDir(taskId), 'manifest.json');
|
|
56
|
+
let manifest = {};
|
|
57
|
+
try { manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); } catch { return { restored: [], failed: [] }; }
|
|
58
|
+
const targets = files || Object.keys(manifest);
|
|
59
|
+
const restored = [];
|
|
60
|
+
const failed = [];
|
|
61
|
+
for (const rel of targets) {
|
|
62
|
+
const m = manifest[rel];
|
|
63
|
+
if (!m) { failed.push({ file: rel, reason: 'no_backup' }); continue; }
|
|
64
|
+
const abs = path.join(root, rel);
|
|
65
|
+
try {
|
|
66
|
+
if (m.existed) {
|
|
67
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
68
|
+
fs.copyFileSync(path.join(backupDir(taskId), 'files', rel), abs);
|
|
69
|
+
const now = new Date(); try { fs.utimesSync(abs, now, now); } catch { /* keep going */ } // Windows keeps the old date: build tools would reuse a stale build
|
|
70
|
+
} else if (fs.existsSync(abs)) fs.unlinkSync(abs);
|
|
71
|
+
restored.push(rel);
|
|
72
|
+
} catch (e) { failed.push({ file: rel, reason: e.message }); }
|
|
73
|
+
}
|
|
74
|
+
return { restored, failed };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Compact line diff for reports (LCS-based, fine for files up to a few thousand lines). */
|
|
78
|
+
function unifiedDiff(a, b, name) {
|
|
79
|
+
const A = a.split(/\r?\n/);
|
|
80
|
+
const B = b.split(/\r?\n/);
|
|
81
|
+
if (A.length * B.length > 4e6) return `--- a/${name}\n+++ b/${name}\n(diff too large: ${A.length} -> ${B.length} lines)`;
|
|
82
|
+
const n = A.length; const m = B.length;
|
|
83
|
+
const dp = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
|
|
84
|
+
for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) {
|
|
85
|
+
dp[i][j] = A[i] === B[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
86
|
+
}
|
|
87
|
+
const lines = [`--- a/${name}`, `+++ b/${name}`];
|
|
88
|
+
let i = 0; let j = 0;
|
|
89
|
+
while (i < n || j < m) {
|
|
90
|
+
if (i < n && j < m && A[i] === B[j]) { i++; j++; continue; }
|
|
91
|
+
if (j < m && (i >= n || dp[i][j + 1] >= dp[i + 1][j])) { lines.push(`+${B[j]} (line ${j + 1})`); j++; }
|
|
92
|
+
else { lines.push(`-${A[i]} (line ${i + 1})`); i++; }
|
|
93
|
+
}
|
|
94
|
+
return lines.join('\n');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = { applyEdits, restore, backupFile, unifiedDiff, isInside, backupDir };
|
package/core/fixlib.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Fix library (no AI):
|
|
4
|
+
* - seed rules: error pattern -> deterministic action
|
|
5
|
+
* - learned fixes: exact error signature + exact file state -> recorded verified edit
|
|
6
|
+
*/
|
|
7
|
+
const crypto = require('crypto');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const store = require('./store');
|
|
11
|
+
const { hashFile } = require('./snapshot');
|
|
12
|
+
|
|
13
|
+
const SEED_RULES = [
|
|
14
|
+
{
|
|
15
|
+
id: 'deps_missing',
|
|
16
|
+
match: (err, project) => project.needsInstall || /cannot find module|module not found|err_module_not_found|can't resolve/i.test(err),
|
|
17
|
+
when: (err, project) => project.needsInstall || (project.type !== 'python' && /cannot find module ['"](?![a-zA-Z]:)[^.'"/\\][^'"]*['"]/i.test(err)),
|
|
18
|
+
action: 'npm_install',
|
|
19
|
+
title: 'Dependencies are missing - install them',
|
|
20
|
+
explain: 'The project cannot find its packages. Fix: npm install (installs the dependencies listed in package.json).',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'native_rebuild',
|
|
24
|
+
match: (err) => /was compiled against a different node\.js version|node_module_version|npm rebuild/i.test(err),
|
|
25
|
+
when: () => true,
|
|
26
|
+
action: 'npm_rebuild',
|
|
27
|
+
title: 'A native module was built for another Node version - rebuild',
|
|
28
|
+
explain: 'A package was compiled for a different Node version. Fix: npm rebuild.',
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
function matchSeed(errText, project) {
|
|
33
|
+
for (const r of SEED_RULES) {
|
|
34
|
+
if (r.match(errText || '', project) && r.when(errText || '', project)) return r;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Stable signature of an error: strip paths, numbers, quotes noise. */
|
|
40
|
+
function signature(errText) {
|
|
41
|
+
const lines = String(errText || '')
|
|
42
|
+
.split(/\r?\n/)
|
|
43
|
+
.filter((l) => /error|fail|cannot|expected|undefined|not a function|is not defined|assert/i.test(l))
|
|
44
|
+
.slice(0, 6)
|
|
45
|
+
.map((l) => l.replace(/[A-Za-z]:\\[^\s:)]+|\/[^\s:)]+\/[^\s:)]+/g, '<path>')
|
|
46
|
+
.replace(/\b\d+(\.\d+)?(ms|s)?\b/g, '<n>')
|
|
47
|
+
.replace(/\s+/g, ' ')
|
|
48
|
+
.trim());
|
|
49
|
+
if (!lines.length) return null;
|
|
50
|
+
return crypto.createHash('sha256').update(lines.join('\n')).digest('hex').slice(0, 20);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A learned fix is replayed only when the files are byte-identical to before AND
|
|
55
|
+
* it fixed the very tests this task is about (or, for old entries, the exact same failure).
|
|
56
|
+
*/
|
|
57
|
+
function findLearned(errText, project, targetNames) {
|
|
58
|
+
const sig = signature(errText);
|
|
59
|
+
if (!sig) return null;
|
|
60
|
+
const targets = targetNames || [];
|
|
61
|
+
const entries = store.list('fixes', (f) => f.projectType === project.type && (
|
|
62
|
+
targets.length ? Array.isArray(f.fixedTests) && f.fixedTests.length > 0 && targets.every((n) => f.fixedTests.includes(n)) : f.signature === sig));
|
|
63
|
+
for (const entry of entries.reverse()) {
|
|
64
|
+
const ok = entry.edits.every((e) => hashFile(path.join(project.root, e.path)) === e.beforeHash);
|
|
65
|
+
if (ok) return entry;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function learn({ errText, project, edits, beforeHashes, taskId, instruction, fixedTests }) {
|
|
71
|
+
const sig = signature(errText);
|
|
72
|
+
if (!sig || !edits.length) return null;
|
|
73
|
+
const entry = {
|
|
74
|
+
id: 'fx_' + crypto.randomBytes(5).toString('hex'),
|
|
75
|
+
signature: sig,
|
|
76
|
+
projectType: project.type,
|
|
77
|
+
projectId: project.id,
|
|
78
|
+
instruction: instruction || '',
|
|
79
|
+
sourceTaskId: taskId,
|
|
80
|
+
fixedTests: fixedTests || [],
|
|
81
|
+
edits: edits.map((e) => ({ path: e.path, beforeHash: beforeHashes[e.path] || null, content: e.content })),
|
|
82
|
+
uses: 0,
|
|
83
|
+
createdAt: new Date().toISOString(),
|
|
84
|
+
};
|
|
85
|
+
store.insert('fixes', entry);
|
|
86
|
+
return entry;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function readContent(root, rel) {
|
|
90
|
+
try { return fs.readFileSync(path.join(root, rel), 'utf8'); } catch { return null; }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { SEED_RULES, matchSeed, signature, findLearned, learn, readContent };
|