brianbellx commited on
Commit
7667cef
·
verified ·
1 Parent(s): a703fff

Add asset cache-busting hashes, Cache-Control headers, and squiggle-windowed long-file cleanup

Browse files
README.md CHANGED
@@ -14,7 +14,7 @@ suggested_hardware: cpu-basic
14
 
15
  Paste a GitHub link. The page finds the hardest-to-read files and a small model rewrites them while you watch.
16
 
17
- The scorer runs in the browser (`codehygiene` v0.6.0). Cleanup runs in the browser via WebGPU (`brianbellx/codehygiene-bonsai-1.7b`) and falls back to Hugging Face ZeroGPU when WebGPU is unavailable. A patch is kept only if it parses, keeps the code, and leaves fewer issues.
18
 
19
  ## Settings
20
 
@@ -27,7 +27,7 @@ The scorer runs in the browser (`codehygiene` v0.6.0). Cleanup runs in the brows
27
  | `REFINER_FILE` | `qwen2.5-coder-1.5b-instruct-q4_k_m.gguf` | Build arg. GGUF file name. |
28
  | `REFINER_MODEL_PATH` | set by the Dockerfile | Path of the GGUF inside the image. |
29
  | `CODEHYGIENE_REF` | pinned commit | Build arg. Version of the scorer and web assets. |
30
- | `MAX_FILE_CHARS` | `12000` | Longer files are not sent to the model. |
31
  | `WARMUP` | on for `llama`, off for `mock` | Fix the examples in the background after start. |
32
  | `MOCK_PROMPT_DELAY` | `1.2` | Seconds before the mock starts streaming. |
33
  | `MOCK_PIECES_PER_SECOND` | `60` | Mock streaming speed. |
 
14
 
15
  Paste a GitHub link. The page finds the hardest-to-read files and a small model rewrites them while you watch.
16
 
17
+ The scorer runs in the browser (`codehygiene` v0.6.1). Cleanup runs in the browser via WebGPU (`brianbellx/codehygiene-bonsai-1.7b`) and falls back to Hugging Face ZeroGPU when WebGPU is unavailable. A patch is kept only if it parses, keeps the code, and leaves fewer issues.
18
 
19
  ## Settings
20
 
 
27
  | `REFINER_FILE` | `qwen2.5-coder-1.5b-instruct-q4_k_m.gguf` | Build arg. GGUF file name. |
28
  | `REFINER_MODEL_PATH` | set by the Dockerfile | Path of the GGUF inside the image. |
29
  | `CODEHYGIENE_REF` | pinned commit | Build arg. Version of the scorer and web assets. |
30
+ | `MAX_FILE_CHARS` | `131072` | Files longer than 128 KiB are skipped; files over 9 KB are cleaned via focused squiggle windows. |
31
  | `WARMUP` | on for `llama`, off for `mock` | Fix the examples in the background after start. |
32
  | `MOCK_PROMPT_DELAY` | `1.2` | Seconds before the mock starts streaming. |
33
  | `MOCK_PIECES_PER_SECOND` | `60` | Mock streaming speed. |
server/app.py CHANGED
@@ -191,6 +191,24 @@ def create_app(
191
  app.state.board = board
192
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  def producer_for(body: FixBody, paced: bool) -> Producer:
195
  request = body.to_request()
196
  cached = None if body.no_cache else service.cached(request, body.source_kind)
 
191
  app.state.board = board
192
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
193
 
194
+ @app.middleware("http")
195
+ async def static_cache_headers(
196
+ request: Request, call_next: Callable[[Request], Awaitable[Any]]
197
+ ) -> Any:
198
+ response = await call_next(request)
199
+ if request.method not in ("GET", "HEAD") or request.url.path.startswith("/api/"):
200
+ return response
201
+ if "cache-control" in response.headers:
202
+ return response
203
+ content_type = response.headers.get("content-type", "")
204
+ if content_type.startswith("text/html"):
205
+ response.headers["Cache-Control"] = "no-cache"
206
+ elif request.url.path.startswith("/fonts/") or "v=" in request.url.query:
207
+ response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
208
+ else:
209
+ response.headers["Cache-Control"] = "no-cache"
210
+ return response
211
+
212
  def producer_for(body: FixBody, paced: bool) -> Producer:
213
  request = body.to_request()
214
  cached = None if body.no_cache else service.cached(request, body.source_kind)
server/config.py CHANGED
@@ -41,7 +41,7 @@ class Settings:
41
  n_ctx: int = 8192
42
  n_threads: int = 2
43
  max_new_tokens: int = 1280
44
- max_file_chars: int = 12_000
45
 
46
  fixtures_path: Path = SPACE_DIR / "server" / "fixtures" / "mock_patches.json"
47
  mock_prompt_delay: float = 1.2
 
41
  n_ctx: int = 8192
42
  n_threads: int = 2
43
  max_new_tokens: int = 1280
44
+ max_file_chars: int = 131_072
45
 
46
  fixtures_path: Path = SPACE_DIR / "server" / "fixtures" / "mock_patches.json"
47
  mock_prompt_delay: float = 1.2
server/fix_loop.py CHANGED
@@ -7,10 +7,11 @@ from dataclasses import dataclass
7
  from typing import Any
8
 
9
  from .backends import Refiner
10
- from .guards import Verdict, verify_patch
11
  from .hygiene import score_dict
12
  from .prompt import build_prompt
13
  from .vendored_pipeline import apply_changes
 
14
 
15
  Emit = Callable[[str, dict[str, Any]], None]
16
 
@@ -61,15 +62,15 @@ def preview_content(base: str, raw: str) -> tuple[str, int] | None:
61
 
62
 
63
  class _Attempt:
64
- def __init__(self, emit: Emit, cancel: threading.Event, base: str) -> None:
65
  self.emit = emit
66
  self.cancel = cancel
67
- self.base = base
68
  self.raw = ""
69
  self.tokens = 0
70
  self.seconds = 0.0
71
  self._last_preview_at = 0.0
72
- self._last_preview = base
73
  self._last_progress_at = -PROGRESS_INTERVAL
74
  self._prompt_total: int | None = None
75
  self._read_reported = False
@@ -102,10 +103,14 @@ class _Attempt:
102
 
103
  def _preview(self) -> None:
104
  self._last_preview_at = time.monotonic()
105
- found = preview_content(self.base, self.raw)
106
- if found is None or found[0] == self._last_preview:
107
  return
108
- self._last_preview, applied = found
 
 
 
 
109
  self.emit("preview", {"content": self._last_preview, "applied_blocks": applied})
110
 
111
 
@@ -189,10 +194,13 @@ class FixRun:
189
  self.attempts += 1
190
  number = self.attempts
191
  self.emit("attempt", {"n": number, "pass": pass_no, "sampled": sampled})
192
- prompt = build_prompt(base, hygiene, self.request.filename)
 
193
  if self.refiner.prompt_fits(prompt):
194
- reply = self._generate(prompt, base, sampled)
195
- verdict = verify_patch(base, reply, self.request.filename, self.request.threshold, hygiene)
 
 
196
  verdict.reply = reply
197
  else:
198
  verdict = Verdict(False, "too_long", base, squiggles_before=len(hygiene["squiggles"]))
@@ -207,11 +215,13 @@ class FixRun:
207
  })
208
  return verdict
209
 
210
- def _generate(self, prompt: str, base: str, sampled: bool) -> str:
211
  temperature, top_p = SAMPLED if sampled else GREEDY
212
- attempt = _Attempt(self.emit, self.cancel, base)
213
  first_token_at: float | None = None
214
- pieces = self.refiner.stream(prompt, base, temperature, top_p, self.cancel, attempt.progress)
 
 
215
  for piece in pieces:
216
  first_token_at = first_token_at or time.monotonic()
217
  attempt.add(piece)
 
7
  from typing import Any
8
 
9
  from .backends import Refiner
10
+ from .guards import Verdict
11
  from .hygiene import score_dict
12
  from .prompt import build_prompt
13
  from .vendored_pipeline import apply_changes
14
+ from .windowing import CodeWindow, select_code_window, verify_window_patch
15
 
16
  Emit = Callable[[str, dict[str, Any]], None]
17
 
 
62
 
63
 
64
  class _Attempt:
65
+ def __init__(self, emit: Emit, cancel: threading.Event, window: CodeWindow) -> None:
66
  self.emit = emit
67
  self.cancel = cancel
68
+ self.window = window
69
  self.raw = ""
70
  self.tokens = 0
71
  self.seconds = 0.0
72
  self._last_preview_at = 0.0
73
+ self._last_preview = window.full_content
74
  self._last_progress_at = -PROGRESS_INTERVAL
75
  self._prompt_total: int | None = None
76
  self._read_reported = False
 
103
 
104
  def _preview(self) -> None:
105
  self._last_preview_at = time.monotonic()
106
+ found = preview_content(self.window.excerpt, self.raw)
107
+ if found is None:
108
  return
109
+ patched_excerpt, applied = found
110
+ full_preview = self.window.splice(patched_excerpt)
111
+ if full_preview == self._last_preview:
112
+ return
113
+ self._last_preview = full_preview
114
  self.emit("preview", {"content": self._last_preview, "applied_blocks": applied})
115
 
116
 
 
194
  self.attempts += 1
195
  number = self.attempts
196
  self.emit("attempt", {"n": number, "pass": pass_no, "sampled": sampled})
197
+ window = select_code_window(base, hygiene)
198
+ prompt = build_prompt(window.excerpt, window.prompt_hygiene, self.request.filename)
199
  if self.refiner.prompt_fits(prompt):
200
+ reply = self._generate(prompt, window, sampled)
201
+ verdict = verify_window_patch(
202
+ window, reply, self.request.filename, self.request.threshold, hygiene
203
+ )
204
  verdict.reply = reply
205
  else:
206
  verdict = Verdict(False, "too_long", base, squiggles_before=len(hygiene["squiggles"]))
 
215
  })
216
  return verdict
217
 
218
+ def _generate(self, prompt: str, window: CodeWindow, sampled: bool) -> str:
219
  temperature, top_p = SAMPLED if sampled else GREEDY
220
+ attempt = _Attempt(self.emit, self.cancel, window)
221
  first_token_at: float | None = None
222
+ pieces = self.refiner.stream(
223
+ prompt, window.excerpt, temperature, top_p, self.cancel, attempt.progress
224
+ )
225
  for piece in pieces:
226
  first_token_at = first_token_at or time.monotonic()
227
  attempt.add(piece)
server/windowing.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from dataclasses import dataclass
5
+ from typing import Any
6
+
7
+ from .guards import Verdict, verify_patch
8
+ from .hygiene import score_dict
9
+ from .vendored_guards import (
10
+ check_basic_syntax,
11
+ check_reference_integrity,
12
+ is_bloated,
13
+ is_gutted,
14
+ )
15
+ from .vendored_pipeline import apply_changes
16
+
17
+ WINDOW_MAX_CHARS = 9_000
18
+ WINDOW_MAX_LINES = 180
19
+ WINDOW_PROMPT_SQUIGGLES = 20
20
+ CLUSTER_SPAN_LINES = 100
21
+ HEADER_LOOKBACK_LINES = 60
22
+
23
+ _TOP_DECL_RE = re.compile(
24
+ r"^(?:async\s+def|def|class|pub\s+(?:async\s+)?fn|fn|func|"
25
+ r"export\s+(?:default\s+)?(?:async\s+)?(?:function|class)|function)\b"
26
+ )
27
+
28
+
29
+ @dataclass(frozen=True)
30
+ class CodeWindow:
31
+ full_content: str
32
+ excerpt: str
33
+ prompt_hygiene: dict[str, Any]
34
+ start_line: int
35
+ end_line: int
36
+ is_sliced: bool
37
+
38
+ def splice(self, updated_excerpt: str) -> str:
39
+ if not self.is_sliced:
40
+ return updated_excerpt
41
+ lines = self.full_content.split("\n")
42
+ return "\n".join([*lines[: self.start_line], updated_excerpt, *lines[self.end_line :]])
43
+
44
+
45
+ def _find_best_cluster(squiggles: list[dict[str, Any]], total_lines: int) -> tuple[int, int]:
46
+ best_anchor = 0
47
+ best_end = 0
48
+ best_count = -1
49
+ for item in squiggles:
50
+ anchor = max(0, min(total_lines - 1, int(item.get("line", 1)) - 1))
51
+ hits = [
52
+ max(0, min(total_lines - 1, int(cand.get("line", 1)) - 1))
53
+ for cand in squiggles
54
+ if anchor <= int(cand.get("line", 1)) - 1 <= anchor + CLUSTER_SPAN_LINES
55
+ ]
56
+ if len(hits) > best_count:
57
+ best_count = len(hits)
58
+ best_anchor = anchor
59
+ best_end = max(hits, default=anchor)
60
+ return best_anchor, best_end
61
+
62
+
63
+ def _snap_window_start(lines: list[str], anchor: int) -> int:
64
+ floor = max(0, anchor - HEADER_LOOKBACK_LINES)
65
+ for index in range(anchor, floor - 1, -1):
66
+ if _TOP_DECL_RE.match(lines[index]):
67
+ return max(0, index - 2)
68
+ return max(0, anchor - 35)
69
+
70
+
71
+ def _trim_bounds_to_budget(
72
+ lines: list[str],
73
+ start: int,
74
+ end: int,
75
+ anchor: int,
76
+ max_chars: int,
77
+ ) -> tuple[int, int]:
78
+ char_count = sum(len(lines[idx]) + 1 for idx in range(start, end))
79
+ min_end = min(len(lines), anchor + 20)
80
+ while end - start > 20 and char_count > max_chars:
81
+ if end > min_end and (end - anchor) >= (anchor - start):
82
+ end -= 1
83
+ char_count -= len(lines[end]) + 1
84
+ elif start < anchor:
85
+ char_count -= len(lines[start]) + 1
86
+ start += 1
87
+ else:
88
+ end -= 1
89
+ char_count -= len(lines[end]) + 1
90
+ while end > anchor + 1 and char_count > max_chars:
91
+ end -= 1
92
+ char_count -= len(lines[end]) + 1
93
+ return start, end
94
+
95
+
96
+ def select_code_window(
97
+ content: str,
98
+ hygiene: dict[str, Any],
99
+ max_chars: int = WINDOW_MAX_CHARS,
100
+ ) -> CodeWindow:
101
+ squiggles = list(hygiene.get("squiggles", []))
102
+ if len(content) <= max_chars or not squiggles:
103
+ capped = {**hygiene, "squiggles": squiggles[:WINDOW_PROMPT_SQUIGGLES]}
104
+ return CodeWindow(content, content, capped, 0, content.count("\n") + 1, False)
105
+
106
+ lines = content.split("\n")
107
+ total_lines = len(lines)
108
+ anchor, cluster_end = _find_best_cluster(squiggles, total_lines)
109
+ start = _snap_window_start(lines, anchor)
110
+ end = min(total_lines, max(cluster_end + 35, start + 80), start + WINDOW_MAX_LINES)
111
+ start, end = _trim_bounds_to_budget(lines, start, end, anchor, max_chars)
112
+
113
+ excerpt = "\n".join(lines[start:end])
114
+ shifted = [
115
+ {**sq, "line": int(sq["line"]) - start}
116
+ for sq in squiggles
117
+ if start + 1 <= int(sq.get("line", 0)) <= end
118
+ ][:WINDOW_PROMPT_SQUIGGLES]
119
+ prompt_hygiene = {**hygiene, "squiggles": shifted}
120
+ is_sliced = start > 0 or end < total_lines
121
+ return CodeWindow(content, excerpt, prompt_hygiene, start, end, is_sliced)
122
+
123
+
124
+ def verify_window_patch(
125
+ window: CodeWindow,
126
+ reply: str,
127
+ filename: str,
128
+ threshold: float,
129
+ before_hygiene: dict[str, Any],
130
+ ) -> Verdict:
131
+ if not window.is_sliced:
132
+ return verify_patch(window.full_content, reply, filename, threshold, before_hygiene)
133
+
134
+ full_before = window.full_content
135
+ squiggles_before = len(before_hygiene["squiggles"])
136
+ rejected = Verdict(False, None, full_before, squiggles_before=squiggles_before)
137
+ if not reply.strip():
138
+ rejected.reason = "empty_output"
139
+ return rejected
140
+ try:
141
+ updated_excerpt, applied = apply_changes(window.excerpt, reply)
142
+ except ValueError:
143
+ rejected.reason = "search_miss"
144
+ return rejected
145
+ rejected.applied_blocks = applied
146
+ if applied == 0 or updated_excerpt == window.excerpt:
147
+ rejected.reason = "no_blocks_applied"
148
+ return rejected
149
+ if is_gutted(window.excerpt, updated_excerpt):
150
+ rejected.reason = "gutted_file"
151
+ return rejected
152
+ if is_bloated(window.excerpt, updated_excerpt):
153
+ rejected.reason = "line_bloat"
154
+ return rejected
155
+
156
+ updated_full = window.splice(updated_excerpt)
157
+ if filename and not check_basic_syntax(updated_full, filename):
158
+ rejected.reason = "invalid_syntax"
159
+ return rejected
160
+ if filename and not check_reference_integrity(full_before, updated_full, filename):
161
+ rejected.reason = "invalid_syntax"
162
+ return rejected
163
+ if is_gutted(full_before, updated_full):
164
+ rejected.reason = "gutted_file"
165
+ return rejected
166
+ if is_bloated(full_before, updated_full):
167
+ rejected.reason = "line_bloat"
168
+ return rejected
169
+
170
+ after = score_dict(updated_full, threshold)
171
+ squiggles_after = len(after["squiggles"])
172
+ accepted = squiggles_after < squiggles_before
173
+ return Verdict(
174
+ accepted=accepted,
175
+ reason=None if accepted else "no_improvement",
176
+ content=updated_full if accepted else full_before,
177
+ applied_blocks=applied,
178
+ squiggles_before=squiggles_before,
179
+ squiggles_after=squiggles_after,
180
+ hygiene=after,
181
+ )
web/build.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { copyFile, mkdir, rm } from "node:fs/promises";
 
2
  import { existsSync } from "node:fs";
3
  import path from "node:path";
4
  import { fileURLToPath } from "node:url";
@@ -45,14 +46,44 @@ function sharedFilesPlugin(shared) {
45
  }
46
 
47
  const PAGES = { "board.html": "leaderboard", "privacy.html": "privacy" };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  async function copyStatic(shared) {
50
- await copyFile(path.join(srcDir, "index.html"), path.join(distDir, "index.html"));
 
 
51
  for (const [page, folder] of Object.entries(PAGES)) {
52
  await mkdir(path.join(distDir, folder), { recursive: true });
53
- await copyFile(path.join(srcDir, page), path.join(distDir, folder, "index.html"));
54
  }
55
- await copyFile(shared.weights, path.join(distDir, "weights.npz"));
56
  }
57
 
58
  const copyStaticPlugin = (shared) => ({
 
1
+ import { createHash } from "node:crypto";
2
+ import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
3
  import { existsSync } from "node:fs";
4
  import path from "node:path";
5
  import { fileURLToPath } from "node:url";
 
46
  }
47
 
48
  const PAGES = { "board.html": "leaderboard", "privacy.html": "privacy" };
49
+ const HASHED_ASSETS = [
50
+ "app.js",
51
+ "board.js",
52
+ "score-worker.js",
53
+ "bonsai-worker.js",
54
+ "styles.css",
55
+ "board.css",
56
+ "privacy.css",
57
+ "weights.npz",
58
+ ];
59
+ const ASSET_TAG_RE = /(<(?:link|script)\b[^>]*?\b(?:href|src)=")((?:\.\.\/)?[a-z0-9-]+\.(?:css|js))(")/gi;
60
+
61
+ async function computeAssetVersion() {
62
+ const hash = createHash("sha256");
63
+ for (const name of HASHED_ASSETS) {
64
+ const filePath = path.join(distDir, name);
65
+ if (existsSync(filePath)) {
66
+ hash.update(name);
67
+ hash.update(await readFile(filePath));
68
+ }
69
+ }
70
+ return hash.digest("hex").slice(0, 10);
71
+ }
72
+
73
+ async function writeVersionedHtml(srcHtmlPath, destHtmlPath, version) {
74
+ const rawHtml = await readFile(srcHtmlPath, "utf8");
75
+ const versioned = rawHtml.replace(ASSET_TAG_RE, `$1$2?v=${version}$3`);
76
+ await writeFile(destHtmlPath, versioned, "utf8");
77
+ }
78
 
79
  async function copyStatic(shared) {
80
+ await copyFile(shared.weights, path.join(distDir, "weights.npz"));
81
+ const version = await computeAssetVersion();
82
+ await writeVersionedHtml(path.join(srcDir, "index.html"), path.join(distDir, "index.html"), version);
83
  for (const [page, folder] of Object.entries(PAGES)) {
84
  await mkdir(path.join(distDir, folder), { recursive: true });
85
+ await writeVersionedHtml(path.join(srcDir, page), path.join(distDir, folder, "index.html"), version);
86
  }
 
87
  }
88
 
89
  const copyStaticPlugin = (shared) => ({
web/src/bonsai-guards.js CHANGED
@@ -374,7 +374,7 @@ function checkPythonStructure(code) {
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);
@@ -396,7 +396,7 @@ function collectRegexMatches(re, text) {
396
  return found;
397
  }
398
 
399
- function checkReferenceIntegrity(beforeCode, afterCode, filename) {
400
  const lang = inferLang(filename);
401
  if (lang === "python") {
402
  const defsBefore = collectRegexMatches(PY_DEF_RE, beforeCode);
@@ -438,14 +438,14 @@ function countNonCommentLines(code) {
438
  return total;
439
  }
440
 
441
- function isGutted(beforeCode, updated) {
442
  const origN = countNonCommentLines(beforeCode);
443
  const newN = countNonCommentLines(updated);
444
  if (origN >= 1 && newN === 0) return true;
445
  return origN >= 10 && newN < Math.max(5, Math.trunc(origN * 0.55));
446
  }
447
 
448
- function isBloated(beforeCode, updated) {
449
  const beforeN = beforeCode.split("\n").filter((ln) => ln.trim()).length;
450
  const afterN = updated.split("\n").filter((ln) => ln.trim()).length;
451
  return afterN > Math.max(40, Math.trunc(beforeN * 1.6));
 
374
  return openTries.length === 0;
375
  }
376
 
377
+ export function checkBasicSyntax(code, filename) {
378
  if (!code.trim()) return false;
379
  const lang = inferLang(filename);
380
  if (lang === "python") return checkPythonStructure(code);
 
396
  return found;
397
  }
398
 
399
+ export function checkReferenceIntegrity(beforeCode, afterCode, filename) {
400
  const lang = inferLang(filename);
401
  if (lang === "python") {
402
  const defsBefore = collectRegexMatches(PY_DEF_RE, beforeCode);
 
438
  return total;
439
  }
440
 
441
+ export function isGutted(beforeCode, updated) {
442
  const origN = countNonCommentLines(beforeCode);
443
  const newN = countNonCommentLines(updated);
444
  if (origN >= 1 && newN === 0) return true;
445
  return origN >= 10 && newN < Math.max(5, Math.trunc(origN * 0.55));
446
  }
447
 
448
+ export function isBloated(beforeCode, updated) {
449
  const beforeN = beforeCode.split("\n").filter((ln) => ln.trim()).length;
450
  const afterN = updated.split("\n").filter((ln) => ln.trim()).length;
451
  return afterN > Math.max(40, Math.trunc(beforeN * 1.6));
web/src/bonsai-window.js ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Squiggle-centered line windowing for files exceeding Browser WebGPU / ZeroGPU prompt budgets.
2
+ import {
3
+ applyChanges,
4
+ checkBasicSyntax,
5
+ checkReferenceIntegrity,
6
+ isBloated,
7
+ isGutted,
8
+ previewContent,
9
+ scoreHygieneDict,
10
+ verifyPatch,
11
+ } from "./bonsai-guards.js";
12
+
13
+ export const WINDOW_MAX_CHARS = 9000;
14
+ const WINDOW_MAX_LINES = 180;
15
+ const WINDOW_PROMPT_SQUIGGLES = 20;
16
+ const CLUSTER_SPAN_LINES = 100;
17
+ const HEADER_LOOKBACK_LINES = 60;
18
+
19
+ const TOP_DECL_RE = new RegExp(
20
+ "^(?:async\\s+def|def|class|pub\\s+(?:async\\s+)?fn|fn|func|" +
21
+ "export\\s+(?:default\\s+)?(?:async\\s+)?(?:function|class)|function)\\b",
22
+ );
23
+
24
+ function findBestCluster(squiggles, totalLines) {
25
+ let bestAnchor = 0;
26
+ let bestEnd = 0;
27
+ let bestCount = -1;
28
+ for (const item of squiggles) {
29
+ const anchor = Math.max(0, Math.min(totalLines - 1, Number(item.line ?? 1) - 1));
30
+ let count = 0;
31
+ let end = anchor;
32
+ for (const cand of squiggles) {
33
+ const idx = Math.max(0, Math.min(totalLines - 1, Number(cand.line ?? 1) - 1));
34
+ if (idx >= anchor && idx <= anchor + CLUSTER_SPAN_LINES) {
35
+ count += 1;
36
+ if (idx > end) end = idx;
37
+ }
38
+ }
39
+ if (count > bestCount) {
40
+ bestCount = count;
41
+ bestAnchor = anchor;
42
+ bestEnd = end;
43
+ }
44
+ }
45
+ return { anchor: bestAnchor, clusterEnd: bestEnd };
46
+ }
47
+
48
+ function snapWindowStart(lines, anchor) {
49
+ const floor = Math.max(0, anchor - HEADER_LOOKBACK_LINES);
50
+ for (let idx = anchor; idx >= floor; idx -= 1) {
51
+ if (TOP_DECL_RE.test(lines[idx])) {
52
+ return Math.max(0, idx - 2);
53
+ }
54
+ }
55
+ return Math.max(0, anchor - 35);
56
+ }
57
+
58
+ function trimBoundsToBudget(lines, initialStart, initialEnd, anchor, maxChars) {
59
+ let start = initialStart;
60
+ let end = initialEnd;
61
+ let charCount = 0;
62
+ for (let idx = start; idx < end; idx += 1) {
63
+ charCount += lines[idx].length + 1;
64
+ }
65
+ const minEnd = Math.min(lines.length, anchor + 20);
66
+ while (end - start > 20 && charCount > maxChars) {
67
+ if (end > minEnd && end - anchor >= anchor - start) {
68
+ end -= 1;
69
+ charCount -= lines[end].length + 1;
70
+ } else if (start < anchor) {
71
+ charCount -= lines[start].length + 1;
72
+ start += 1;
73
+ } else {
74
+ end -= 1;
75
+ charCount -= lines[end].length + 1;
76
+ }
77
+ }
78
+ while (end > anchor + 1 && charCount > maxChars) {
79
+ end -= 1;
80
+ charCount -= lines[end].length + 1;
81
+ }
82
+ return { start, end };
83
+ }
84
+
85
+ function makeWindowObject(fullContent, excerpt, promptHygiene, startLine, endLine, isSliced) {
86
+ return {
87
+ fullContent,
88
+ excerpt,
89
+ promptHygiene,
90
+ startLine,
91
+ endLine,
92
+ isSliced,
93
+ splice(updatedExcerpt) {
94
+ if (!isSliced) return updatedExcerpt;
95
+ const lines = fullContent.split("\n");
96
+ return [...lines.slice(0, startLine), updatedExcerpt, ...lines.slice(endLine)].join("\n");
97
+ },
98
+ };
99
+ }
100
+
101
+ export function selectCodeWindow(content, hygiene, maxChars = WINDOW_MAX_CHARS) {
102
+ const squiggles = hygiene.squiggles ?? [];
103
+ if (content.length <= maxChars || squiggles.length === 0) {
104
+ const capped = { ...hygiene, squiggles: squiggles.slice(0, WINDOW_PROMPT_SQUIGGLES) };
105
+ const lineCount = content.split("\n").length;
106
+ return makeWindowObject(content, content, capped, 0, lineCount, false);
107
+ }
108
+
109
+ const lines = content.split("\n");
110
+ const totalLines = lines.length;
111
+ const { anchor, clusterEnd } = findBestCluster(squiggles, totalLines);
112
+ const rawStart = snapWindowStart(lines, anchor);
113
+ const rawEnd = Math.min(
114
+ totalLines,
115
+ Math.max(clusterEnd + 35, rawStart + 80),
116
+ rawStart + WINDOW_MAX_LINES,
117
+ );
118
+ const { start, end } = trimBoundsToBudget(lines, rawStart, rawEnd, anchor, maxChars);
119
+
120
+ const excerpt = lines.slice(start, end).join("\n");
121
+ const shifted = squiggles
122
+ .filter((sq) => {
123
+ const ln = Number(sq.line ?? 0);
124
+ return ln >= start + 1 && ln <= end;
125
+ })
126
+ .slice(0, WINDOW_PROMPT_SQUIGGLES)
127
+ .map((sq) => ({ ...sq, line: Number(sq.line) - start }));
128
+ const promptHygiene = { ...hygiene, squiggles: shifted };
129
+ return makeWindowObject(content, excerpt, promptHygiene, start, end, start > 0 || end < totalLines);
130
+ }
131
+
132
+ export function previewWindowContent(codeWindow, raw) {
133
+ const found = previewContent(codeWindow.excerpt, raw);
134
+ if (!found) return null;
135
+ return { content: codeWindow.splice(found.content), applied: found.applied };
136
+ }
137
+
138
+ export function verifyWindowPatch(codeWindow, reply, filename, threshold, beforeHygiene, weights) {
139
+ if (!codeWindow.isSliced) {
140
+ return verifyPatch(codeWindow.fullContent, reply, filename, threshold, beforeHygiene, weights);
141
+ }
142
+
143
+ const fullBefore = codeWindow.fullContent;
144
+ const squigglesBefore = beforeHygiene.squiggles.length;
145
+ const reject = (reason, applied = 0) => ({
146
+ accepted: false,
147
+ reason,
148
+ content: fullBefore,
149
+ appliedBlocks: applied,
150
+ squigglesBefore,
151
+ squigglesAfter: null,
152
+ hygiene: {},
153
+ scoreAfter: null,
154
+ reply: "",
155
+ });
156
+
157
+ if (!reply.trim()) return reject("empty_output");
158
+ let patched;
159
+ try {
160
+ patched = applyChanges(codeWindow.excerpt, reply);
161
+ } catch {
162
+ return reject("search_miss");
163
+ }
164
+ if (patched.applied === 0 || patched.content === codeWindow.excerpt) {
165
+ return reject("no_blocks_applied", patched.applied);
166
+ }
167
+ if (isGutted(codeWindow.excerpt, patched.content)) return reject("gutted_file", patched.applied);
168
+ if (isBloated(codeWindow.excerpt, patched.content)) return reject("line_bloat", patched.applied);
169
+
170
+ const updatedFull = codeWindow.splice(patched.content);
171
+ if (filename) {
172
+ const syntaxOk = checkBasicSyntax(updatedFull, filename);
173
+ const refsOk = syntaxOk && checkReferenceIntegrity(fullBefore, updatedFull, filename);
174
+ if (!refsOk) return reject("invalid_syntax", patched.applied);
175
+ }
176
+ if (isGutted(fullBefore, updatedFull)) return reject("gutted_file", patched.applied);
177
+ if (isBloated(fullBefore, updatedFull)) return reject("line_bloat", patched.applied);
178
+
179
+ const after = scoreHygieneDict(updatedFull, weights, threshold);
180
+ const squigglesAfter = after.squiggles.length;
181
+ const accepted = squigglesAfter < squigglesBefore;
182
+ return {
183
+ accepted,
184
+ reason: accepted ? null : "no_improvement",
185
+ content: accepted ? updatedFull : fullBefore,
186
+ appliedBlocks: patched.applied,
187
+ squigglesBefore,
188
+ squigglesAfter,
189
+ hygiene: after,
190
+ scoreAfter: after.score ?? null,
191
+ reply,
192
+ };
193
+ }
web/src/bonsai-worker.js CHANGED
@@ -1,14 +1,16 @@
1
  // Web Worker running Bonsai 1.7B WebGPU inference and closed-loop v0.6 codehygiene verification.
2
  import { loadWeights } from "shared:engine";
3
- import { buildPrompt, previewContent, scoreHygieneDict, verifyPatch } from "./bonsai-guards.js";
4
  import { BonsaiWebGpuEngine, DEFAULT_MODEL_BASE } from "./bonsai-webgpu.js";
 
5
 
6
  const PREVIEW_INTERVAL_MS = 85;
7
  const PROGRESS_INTERVAL_MS = 200;
8
  const GREEDY = { temperature: 0.0, topP: 1.0 };
9
  const SAMPLED = { temperature: 0.45, topP: 0.92 };
10
 
11
- const weightsReady = loadWeights(new URL("./weights.npz", import.meta.url).href);
 
12
  let enginePromise = null;
13
  let loadedBaseUrl = null;
14
  const cancelledIds = new Set();
@@ -45,7 +47,8 @@ async function runSingleAttempt({
45
  if (isCancelled()) return { cancelled: true };
46
  emit("attempt", { n: attemptNo, pass: passNo, sampled });
47
 
48
- const prompt = buildPrompt(base, hygiene, filename);
 
49
  const sampling = sampled ? SAMPLED : GREEDY;
50
  let raw = "";
51
  let tokens = 0;
@@ -56,7 +59,7 @@ async function runSingleAttempt({
56
 
57
  const flushPreview = () => {
58
  lastPreviewAt = performance.now();
59
- const found = previewContent(base, raw);
60
  if (found && found.content !== lastPreview) {
61
  lastPreview = found.content;
62
  emit("preview", { content: lastPreview, applied_blocks: found.applied });
@@ -90,7 +93,7 @@ async function runSingleAttempt({
90
  if (isCancelled()) return { cancelled: true };
91
  flushPreview();
92
 
93
- const verdict = verifyPatch(base, reply, filename, threshold, hygiene, weights);
94
  emit("verdict", {
95
  n: attemptNo,
96
  accepted: verdict.accepted,
@@ -145,7 +148,8 @@ async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
145
  emit("progress", { phase: "read", done, total });
146
  });
147
 
148
- const firstPrompt = buildPrompt(content, before, filename);
 
149
  if (!engine.promptFits(engine.tokenizer.encode(firstPrompt), 256)) {
150
  throw new Error("Prompt exceeds browser WebGPU context budget.");
151
  }
 
1
  // Web Worker running Bonsai 1.7B WebGPU inference and closed-loop v0.6 codehygiene verification.
2
  import { loadWeights } from "shared:engine";
3
+ import { buildPrompt, scoreHygieneDict } from "./bonsai-guards.js";
4
  import { BonsaiWebGpuEngine, DEFAULT_MODEL_BASE } from "./bonsai-webgpu.js";
5
+ import { previewWindowContent, selectCodeWindow, verifyWindowPatch } from "./bonsai-window.js";
6
 
7
  const PREVIEW_INTERVAL_MS = 85;
8
  const PROGRESS_INTERVAL_MS = 200;
9
  const GREEDY = { temperature: 0.0, topP: 1.0 };
10
  const SAMPLED = { temperature: 0.45, topP: 0.92 };
11
 
12
+ const ASSET_QUERY = new URL(import.meta.url).search;
13
+ const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href);
14
  let enginePromise = null;
15
  let loadedBaseUrl = null;
16
  const cancelledIds = new Set();
 
47
  if (isCancelled()) return { cancelled: true };
48
  emit("attempt", { n: attemptNo, pass: passNo, sampled });
49
 
50
+ const codeWindow = selectCodeWindow(base, hygiene);
51
+ const prompt = buildPrompt(codeWindow.excerpt, codeWindow.promptHygiene, filename);
52
  const sampling = sampled ? SAMPLED : GREEDY;
53
  let raw = "";
54
  let tokens = 0;
 
59
 
60
  const flushPreview = () => {
61
  lastPreviewAt = performance.now();
62
+ const found = previewWindowContent(codeWindow, raw);
63
  if (found && found.content !== lastPreview) {
64
  lastPreview = found.content;
65
  emit("preview", { content: lastPreview, applied_blocks: found.applied });
 
93
  if (isCancelled()) return { cancelled: true };
94
  flushPreview();
95
 
96
+ const verdict = verifyWindowPatch(codeWindow, reply, filename, threshold, hygiene, weights);
97
  emit("verdict", {
98
  n: attemptNo,
99
  accepted: verdict.accepted,
 
148
  emit("progress", { phase: "read", done, total });
149
  });
150
 
151
+ const initialWindow = selectCodeWindow(content, before);
152
+ const firstPrompt = buildPrompt(initialWindow.excerpt, initialWindow.promptHygiene, filename);
153
  if (!engine.promptFits(engine.tokenizer.encode(firstPrompt), 256)) {
154
  throw new Error("Prompt exceeds browser WebGPU context budget.");
155
  }
web/src/browser-refiner.js CHANGED
@@ -2,7 +2,8 @@
2
  import { probeWebGpuSupport } from "./bonsai-webgpu.js";
3
  import { streamFix } from "./refiner.js";
4
 
5
- const BONSAI_WORKER_URL = new URL("./bonsai-worker.js", import.meta.url);
 
6
 
7
  let sharedWorker = null;
8
  let webGpuAvailablePromise = null;
 
2
  import { probeWebGpuSupport } from "./bonsai-webgpu.js";
3
  import { streamFix } from "./refiner.js";
4
 
5
+ const ASSET_QUERY = new URL(import.meta.url).search;
6
+ const BONSAI_WORKER_URL = new URL(`./bonsai-worker.js${ASSET_QUERY}`, import.meta.url);
7
 
8
  let sharedWorker = null;
9
  let webGpuAvailablePromise = null;
web/src/labels.js CHANGED
@@ -83,6 +83,7 @@ export const STATUS = {
83
  skipped: "Skipped",
84
  offline: "Offline",
85
  tooLong: "Too long",
 
86
  noIssues: "No issues found",
87
  };
88
 
 
83
  skipped: "Skipped",
84
  offline: "Offline",
85
  tooLong: "Too long",
86
+ filesTooLong: "Files too long",
87
  noIssues: "No issues found",
88
  };
89
 
web/src/main.js CHANGED
@@ -107,13 +107,21 @@ function issueCount(files) {
107
  return files.reduce((sum, file) => sum + file.result.squiggles.length, 0);
108
  }
109
 
 
 
 
 
 
 
 
 
110
  function renderTally() {
111
  nodes.progress.hidden = !app.ingest;
112
  nodes.progress.textContent = app.ingest ?? "";
113
  const total = app.initialIssues;
114
  const fixed = Math.max(0, total - issueCount(app.files));
115
  nodes.tally.classList.toggle("is-empty", total === 0);
116
- nodes.tallyLabel.textContent = total === 0 ? STATUS.noIssues : "Issues fixed";
117
  nodes.tallyTotal.textContent = String(total);
118
  tweenNumber(nodes.tallyFixed, app.shownFixed ?? fixed, fixed, TIMING.tallyTween);
119
  app.shownFixed = fixed;
 
107
  return files.reduce((sum, file) => sum + file.result.squiggles.length, 0);
108
  }
109
 
110
+ function tallyHeading(total) {
111
+ if (total === 0) {
112
+ return STATUS.noIssues;
113
+ }
114
+ const unstarted = app.idle && app.queue.length === 0 && !app.files.some((file) => file.attempted);
115
+ return unstarted ? STATUS.filesTooLong : "Issues fixed";
116
+ }
117
+
118
  function renderTally() {
119
  nodes.progress.hidden = !app.ingest;
120
  nodes.progress.textContent = app.ingest ?? "";
121
  const total = app.initialIssues;
122
  const fixed = Math.max(0, total - issueCount(app.files));
123
  nodes.tally.classList.toggle("is-empty", total === 0);
124
+ nodes.tallyLabel.textContent = tallyHeading(total);
125
  nodes.tallyTotal.textContent = String(total);
126
  tweenNumber(nodes.tallyFixed, app.shownFixed ?? fixed, fixed, TIMING.tallyTween);
127
  app.shownFixed = fixed;
web/src/refiner.js CHANGED
@@ -1,4 +1,4 @@
1
- export const DEFAULT_MAX_FILE_CHARS = 12000;
2
 
3
  // Pages below the site root (the leaderboard) set data-root="../" so API calls still reach /api.
4
  const SITE_ROOT = new URL(document.documentElement.dataset.root ?? "./", document.baseURI);
 
1
+ export const DEFAULT_MAX_FILE_CHARS = 131072;
2
 
3
  // Pages below the site root (the leaderboard) set data-root="../" so API calls still reach /api.
4
  const SITE_ROOT = new URL(document.documentElement.dataset.root ?? "./", document.baseURI);
web/src/score-worker.js CHANGED
@@ -2,7 +2,8 @@ import { loadWeights, scoreCode } from "shared:engine";
2
 
3
  const WARMUP_CODE = ["def warm(items):", " for item in items:", " return item", ""].join("\n");
4
 
5
- const weightsReady = loadWeights(new URL("./weights.npz", import.meta.url).href).then((weights) => {
 
6
  scoreCode(WARMUP_CODE, weights);
7
  return weights;
8
  });
 
2
 
3
  const WARMUP_CODE = ["def warm(items):", " for item in items:", " return item", ""].join("\n");
4
 
5
+ const ASSET_QUERY = new URL(import.meta.url).search;
6
+ const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href).then((weights) => {
7
  scoreCode(WARMUP_CODE, weights);
8
  return weights;
9
  });
web/src/scoring.js CHANGED
@@ -1,6 +1,7 @@
1
  export const THRESHOLD = 0.55;
2
 
3
- const WORKER_URL = new URL("./score-worker.js", import.meta.url);
 
4
 
5
  class ScoreWorker {
6
  constructor() {
 
1
  export const THRESHOLD = 0.55;
2
 
3
+ const ASSET_QUERY = new URL(import.meta.url).search;
4
+ const WORKER_URL = new URL(`./score-worker.js${ASSET_QUERY}`, import.meta.url);
5
 
6
  class ScoreWorker {
7
  constructor() {