Spaces:
Sleeping
Sleeping
Upgrade to codehygiene v0.6.1, Bonsai 1.7B WebGPU + ZeroGPU refiner, and Data Retention page
Browse files- web/src/bonsai-guards.js +105 -62
web/src/bonsai-guards.js
CHANGED
|
@@ -115,67 +115,129 @@ function shiftIndent(text, delta) {
|
|
| 115 |
}).join("\n");
|
| 116 |
}
|
| 117 |
|
| 118 |
-
function
|
| 119 |
-
const srcLines = source.split("\n");
|
| 120 |
-
const needle = search.split("\n").map((ln) => ln.trimEnd());
|
| 121 |
const span = needle.length;
|
| 122 |
-
if (span === 0 || span > srcLines.length) return
|
| 123 |
-
|
| 124 |
let foundPos = -1;
|
| 125 |
for (let idx = 0; idx <= srcLines.length - span; idx += 1) {
|
| 126 |
-
let
|
| 127 |
for (let k = 0; k < span; k += 1) {
|
| 128 |
-
if (srcLines[idx + k]
|
| 129 |
-
|
| 130 |
break;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
-
if (
|
| 134 |
-
if (foundPos !== -1) return
|
| 135 |
foundPos = idx;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
|
| 143 |
function replaceStrippedUnique(source, search, replace) {
|
| 144 |
const srcLines = source.split("\n");
|
| 145 |
const searchLines = search.split("\n");
|
| 146 |
const needle = searchLines.map((ln) => ln.trim());
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
}
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
}
|
| 180 |
|
| 181 |
export function applyChanges(content, responseText) {
|
|
@@ -203,6 +265,9 @@ export function applyChanges(content, responseText) {
|
|
| 203 |
if (fallback === null) {
|
| 204 |
fallback = replaceStrippedUnique(working, search, replace);
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
| 206 |
if (fallback !== null) {
|
| 207 |
working = fallback;
|
| 208 |
applied += 1;
|
|
@@ -309,33 +374,11 @@ function checkPythonStructure(code) {
|
|
| 309 |
return openTries.length === 0;
|
| 310 |
}
|
| 311 |
|
| 312 |
-
const JS_IMPORT_RE = new RegExp(
|
| 313 |
-
"^\\s*import\\s+[\\s\\S]*?from\\s+[\"'][^\"']+[\"'];?",
|
| 314 |
-
"gm",
|
| 315 |
-
);
|
| 316 |
-
const JS_EXPORT_DEFAULT_RE = /^\s*export\s+default\s+/gm;
|
| 317 |
-
const JS_EXPORT_DECL_RE = new RegExp(
|
| 318 |
-
"^\\s*export\\s+(?=(?:async\\s+)?(?:function|class|const|let|var)\\b)",
|
| 319 |
-
"gm",
|
| 320 |
-
);
|
| 321 |
-
|
| 322 |
function checkBasicSyntax(code, filename) {
|
| 323 |
if (!code.trim()) return false;
|
| 324 |
const lang = inferLang(filename);
|
| 325 |
if (lang === "python") return checkPythonStructure(code);
|
| 326 |
-
if (lang === "javascript") {
|
| 327 |
-
try {
|
| 328 |
-
let moduleBody = code.replace(JS_IMPORT_RE, "");
|
| 329 |
-
moduleBody = moduleBody.replace(JS_EXPORT_DEFAULT_RE, "const __default = ");
|
| 330 |
-
moduleBody = moduleBody.replace(JS_EXPORT_DECL_RE, "");
|
| 331 |
-
// Syntax-parse only; Function constructor is never invoked.
|
| 332 |
-
new Function(moduleBody);
|
| 333 |
-
return true;
|
| 334 |
-
} catch {
|
| 335 |
-
return false;
|
| 336 |
-
}
|
| 337 |
-
}
|
| 338 |
-
if (lang === "typescript" || lang === "rust" || lang === "go") {
|
| 339 |
return hasBalancedDelimiters(code, "//");
|
| 340 |
}
|
| 341 |
return true;
|
|
|
|
| 115 |
}).join("\n");
|
| 116 |
}
|
| 117 |
|
| 118 |
+
function findUniqueSpan(srcLines, needle, normFn) {
|
|
|
|
|
|
|
| 119 |
const span = needle.length;
|
| 120 |
+
if (span === 0 || span > srcLines.length) return -1;
|
|
|
|
| 121 |
let foundPos = -1;
|
| 122 |
for (let idx = 0; idx <= srcLines.length - span; idx += 1) {
|
| 123 |
+
let matches = true;
|
| 124 |
for (let k = 0; k < span; k += 1) {
|
| 125 |
+
if (normFn(srcLines[idx + k]) !== needle[k]) {
|
| 126 |
+
matches = false;
|
| 127 |
break;
|
| 128 |
}
|
| 129 |
}
|
| 130 |
+
if (matches) {
|
| 131 |
+
if (foundPos !== -1) return -1;
|
| 132 |
foundPos = idx;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
+
return foundPos;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
function spliceMatchedSpan(srcLines, searchLines, pos, replace, alignIndent) {
|
| 139 |
+
const span = searchLines.length;
|
| 140 |
+
let delta = 0;
|
| 141 |
+
if (alignIndent) {
|
| 142 |
+
for (let k = 0; k < span; k += 1) {
|
| 143 |
+
const sLine = srcLines[pos + k];
|
| 144 |
+
const qLine = searchLines[k];
|
| 145 |
+
if (sLine.trim() && qLine.trim() && !sLine.includes("\t") && !qLine.includes("\t")) {
|
| 146 |
+
delta = leadingSpaces(sLine) - leadingSpaces(qLine);
|
| 147 |
+
break;
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
const adjusted = alignIndent ? shiftIndent(replace, delta) : replace;
|
| 152 |
+
const replLines = adjusted ? adjusted.split("\n") : [];
|
| 153 |
+
return [...srcLines.slice(0, pos), ...replLines, ...srcLines.slice(pos + span)].join("\n");
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
function replaceRstripLines(source, search, replace) {
|
| 157 |
+
const srcLines = source.split("\n");
|
| 158 |
+
const searchLines = search.split("\n");
|
| 159 |
+
const pos = findUniqueSpan(srcLines, searchLines.map((ln) => ln.trimEnd()), (ln) => ln.trimEnd());
|
| 160 |
+
return pos === -1 ? null : spliceMatchedSpan(srcLines, searchLines, pos, replace, false);
|
| 161 |
}
|
| 162 |
|
| 163 |
function replaceStrippedUnique(source, search, replace) {
|
| 164 |
const srcLines = source.split("\n");
|
| 165 |
const searchLines = search.split("\n");
|
| 166 |
const needle = searchLines.map((ln) => ln.trim());
|
| 167 |
+
if (!needle.some(Boolean) || (needle.length === 1 && needle[0].length < 14)) return null;
|
| 168 |
+
const pos = findUniqueSpan(srcLines, needle, (ln) => ln.trim());
|
| 169 |
+
return pos === -1 ? null : spliceMatchedSpan(srcLines, searchLines, pos, replace, true);
|
| 170 |
+
}
|
| 171 |
|
| 172 |
+
function similarityRatio(left, right) {
|
| 173 |
+
if (left === right) return 1.0;
|
| 174 |
+
const total = left.length + right.length;
|
| 175 |
+
if (total === 0) return 1.0;
|
| 176 |
+
const n = left.length;
|
| 177 |
+
const m = right.length;
|
| 178 |
+
let prev = new Uint16Array(m + 1);
|
| 179 |
+
let curr = new Uint16Array(m + 1);
|
| 180 |
+
for (let i = 1; i <= n; i += 1) {
|
| 181 |
+
const ch = left.charCodeAt(i - 1);
|
| 182 |
+
for (let j = 1; j <= m; j += 1) {
|
| 183 |
+
curr[j] = ch === right.charCodeAt(j - 1) ? prev[j - 1] + 1 : Math.max(prev[j], curr[j - 1]);
|
| 184 |
}
|
| 185 |
+
const swap = prev;
|
| 186 |
+
prev = curr;
|
| 187 |
+
curr = swap;
|
| 188 |
+
curr.fill(0);
|
| 189 |
}
|
| 190 |
+
return (2.0 * prev[m]) / total;
|
| 191 |
+
}
|
| 192 |
|
| 193 |
+
function endpointsMatch(firstSearch, lastSearch, nonEmpty) {
|
| 194 |
+
if (nonEmpty.length === 0) return false;
|
| 195 |
+
const firstRatio = similarityRatio(firstSearch, nonEmpty[0]);
|
| 196 |
+
if (firstRatio < 0.78) return false;
|
| 197 |
+
return similarityRatio(lastSearch, nonEmpty[nonEmpty.length - 1]) >= 0.78;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
function scoreFuzzyCandidates(srcNorm, searchNonempty, needleText, span) {
|
| 201 |
+
let bestPos = -1;
|
| 202 |
+
let bestRatio = 0.0;
|
| 203 |
+
let runnerUp = 0.0;
|
| 204 |
+
const minGap = Math.max(2, Math.floor(span / 2));
|
| 205 |
+
const firstSearch = searchNonempty[0];
|
| 206 |
+
const lastSearch = searchNonempty[searchNonempty.length - 1];
|
| 207 |
+
|
| 208 |
+
for (let pos = 0; pos <= srcNorm.length - span; pos += 1) {
|
| 209 |
+
const win = srcNorm.slice(pos, pos + span);
|
| 210 |
+
if (!endpointsMatch(firstSearch, lastSearch, win.filter(Boolean))) continue;
|
| 211 |
+
const ratio = similarityRatio(needleText, win.join("\n"));
|
| 212 |
+
if (ratio > bestRatio) {
|
| 213 |
+
if (bestPos >= 0 && Math.abs(pos - bestPos) >= minGap) {
|
| 214 |
+
runnerUp = Math.max(runnerUp, bestRatio);
|
| 215 |
+
}
|
| 216 |
+
bestRatio = ratio;
|
| 217 |
+
bestPos = pos;
|
| 218 |
+
} else if (Math.abs(pos - bestPos) >= minGap) {
|
| 219 |
+
runnerUp = Math.max(runnerUp, ratio);
|
| 220 |
}
|
| 221 |
}
|
| 222 |
+
return { bestPos, bestRatio, runnerUp };
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
function replaceFuzzyUnique(source, search, replace) {
|
| 226 |
+
const srcLines = source.split("\n");
|
| 227 |
+
const searchLines = search.split("\n");
|
| 228 |
+
const searchNorm = searchLines.map((ln) => ln.trim());
|
| 229 |
+
const searchNonempty = searchNorm.filter(Boolean);
|
| 230 |
+
const span = searchNorm.length;
|
| 231 |
+
if (span === 0 || span > srcLines.length || searchNonempty.length === 0) return null;
|
| 232 |
+
|
| 233 |
+
const needleText = searchNorm.join("\n");
|
| 234 |
+
if (needleText.length < (span >= 2 ? 12 : 24) || needleText.length > 4000) return null;
|
| 235 |
+
|
| 236 |
+
const srcNorm = srcLines.map((ln) => ln.trim());
|
| 237 |
+
const { bestPos, bestRatio, runnerUp } = scoreFuzzyCandidates(srcNorm, searchNonempty, needleText, span);
|
| 238 |
+
const minRatio = span >= 3 ? 0.9 : 0.93;
|
| 239 |
+
if (bestPos < 0 || bestRatio < minRatio || bestRatio - runnerUp < 0.05) return null;
|
| 240 |
+
return spliceMatchedSpan(srcLines, searchLines, bestPos, replace, true);
|
| 241 |
}
|
| 242 |
|
| 243 |
export function applyChanges(content, responseText) {
|
|
|
|
| 265 |
if (fallback === null) {
|
| 266 |
fallback = replaceStrippedUnique(working, search, replace);
|
| 267 |
}
|
| 268 |
+
if (fallback === null) {
|
| 269 |
+
fallback = replaceFuzzyUnique(working, search, replace);
|
| 270 |
+
}
|
| 271 |
if (fallback !== null) {
|
| 272 |
working = fallback;
|
| 273 |
applied += 1;
|
|
|
|
| 374 |
return openTries.length === 0;
|
| 375 |
}
|
| 376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
function checkBasicSyntax(code, filename) {
|
| 378 |
if (!code.trim()) return false;
|
| 379 |
const lang = inferLang(filename);
|
| 380 |
if (lang === "python") return checkPythonStructure(code);
|
| 381 |
+
if (lang === "javascript" || lang === "typescript" || lang === "rust" || lang === "go") {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
return hasBalancedDelimiters(code, "//");
|
| 383 |
}
|
| 384 |
return true;
|