brianbellx commited on
Commit
a04ea01
·
verified ·
1 Parent(s): 8e92141

Switch to ZeroGPU-only default and declaration-aligned multi-chunk 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.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
 
 
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 streams from Hugging Face ZeroGPU (`brianbellx/codehygiene-bonsai-1.7b`), chunking long files along top-level declarations. A patch is kept only if it parses, keeps references intact, and leaves fewer issues.
18
 
19
  ## Settings
20
 
server/backends.py CHANGED
@@ -213,6 +213,7 @@ class ZeroGpuRefiner:
213
  ) -> None:
214
  self.model_id = settings.model_id or settings.zerogpu_space
215
  self._space_id = settings.zerogpu_space
 
216
  self._n_ctx = settings.n_ctx
217
  self._max_new_tokens = settings.max_new_tokens
218
  self._transport = transport
@@ -297,11 +298,13 @@ class ZeroGpuRefiner:
297
  return
298
  ip_token = VISITOR_IP_TOKEN.get()
299
  headers = {"X-IP-Token": ip_token} if ip_token else {}
300
- start_res = self._http.post(
301
- "/gradio_api/call/stream_patch",
302
- json={"data": [prompt, self._max_new_tokens, temperature, top_p]},
303
- headers=headers,
304
- )
 
 
305
  start_res.raise_for_status()
306
  event_id = start_res.json()["event_id"]
307
 
 
213
  ) -> None:
214
  self.model_id = settings.model_id or settings.zerogpu_space
215
  self._space_id = settings.zerogpu_space
216
+ self._hf_token = settings.hf_token
217
  self._n_ctx = settings.n_ctx
218
  self._max_new_tokens = settings.max_new_tokens
219
  self._transport = transport
 
298
  return
299
  ip_token = VISITOR_IP_TOKEN.get()
300
  headers = {"X-IP-Token": ip_token} if ip_token else {}
301
+ if self._hf_token and not ip_token:
302
+ headers["Authorization"] = f"Bearer {self._hf_token}"
303
+ payload = {"data": [prompt, self._max_new_tokens, temperature, top_p]}
304
+ start_res = self._http.post("/gradio_api/call/stream_patch", json=payload, headers=headers)
305
+ if start_res.status_code in (401, 403) and "Authorization" in headers:
306
+ headers.pop("Authorization", None)
307
+ start_res = self._http.post("/gradio_api/call/stream_patch", json=payload, headers=headers)
308
  start_res.raise_for_status()
309
  event_id = start_res.json()["event_id"]
310
 
server/fix_loop.py CHANGED
@@ -11,7 +11,14 @@ 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
 
@@ -151,6 +158,8 @@ class FixRun:
151
  outcome["reason"] = "too_long"
152
  return outcome
153
  self._load_refiner()
 
 
154
 
155
  first = self._attempt(1, request.content, before, sampled=False)
156
  outcome["passes"] = 1
@@ -174,6 +183,75 @@ class FixRun:
174
  outcome["reason"] = first.reason
175
  return outcome
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  def _sampled_retry(self, before: dict[str, Any]) -> tuple[str | None, Verdict | None]:
178
  tries = [self._attempt(2, self.request.content, before, sampled=True) for _ in range(2)]
179
  valid = [verdict for verdict in tries if verdict.accepted]
@@ -189,21 +267,33 @@ class FixRun:
189
  raise RefinerUnavailable(f"{type(exc).__name__}: {exc}") from exc
190
 
191
  def _attempt(self, pass_no: int, base: str, hygiene: dict[str, Any], sampled: bool) -> Verdict:
 
 
 
 
 
 
192
  if self.cancel.is_set():
193
  raise Cancelled()
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"]))
 
 
207
  self.emit("verdict", {
208
  "n": number,
209
  "accepted": verdict.accepted,
 
11
  from .hygiene import score_dict
12
  from .prompt import build_prompt
13
  from .vendored_pipeline import apply_changes
14
+ from .windowing import (
15
+ CodeWindow,
16
+ build_chunk_window,
17
+ extract_squiggle_windows,
18
+ select_code_window,
19
+ should_chunk_file,
20
+ verify_window_patch,
21
+ )
22
 
23
  Emit = Callable[[str, dict[str, Any]], None]
24
 
 
158
  outcome["reason"] = "too_long"
159
  return outcome
160
  self._load_refiner()
161
+ if should_chunk_file(request.content):
162
+ return self._chunked_outcome(before)
163
 
164
  first = self._attempt(1, request.content, before, sampled=False)
165
  outcome["passes"] = 1
 
183
  outcome["reason"] = first.reason
184
  return outcome
185
 
186
+ def _run_single_chunk(
187
+ self,
188
+ cur_code: str,
189
+ cur_hyg: dict[str, Any],
190
+ w_lo: int,
191
+ w_hi: int,
192
+ allow_second_pass: bool,
193
+ ) -> tuple[list[Verdict], int, str | None]:
194
+ request = self.request
195
+ window = build_chunk_window(cur_code, cur_hyg, w_lo, w_hi, request.threshold)
196
+ first = self._attempt_window(1, window, cur_hyg, sampled=False)
197
+ if not first.accepted:
198
+ if not allow_second_pass or first.reason == "too_long":
199
+ return [], 1, first.reason
200
+ retry = self._attempt_window(2, window, cur_hyg, sampled=True)
201
+ return ([retry] if retry.accepted else []), 2, retry.reason
202
+
203
+ kept = [first]
204
+ passes = 1
205
+ new_hi = w_hi + (first.content.count("\n") - cur_code.count("\n"))
206
+ still_flagged = any(w_lo < int(sq["line"]) <= new_hi for sq in first.hygiene["squiggles"])
207
+ if allow_second_pass and still_flagged:
208
+ passes = 2
209
+ follow = build_chunk_window(
210
+ first.content, first.hygiene, w_lo, new_hi, request.threshold
211
+ )
212
+ second = self._attempt_window(2, follow, first.hygiene, sampled=False)
213
+ if second.accepted:
214
+ kept.append(second)
215
+ return kept, passes, None
216
+
217
+ def _chunked_outcome(self, before: dict[str, Any]) -> dict[str, Any]:
218
+ request = self.request
219
+ windows = extract_squiggle_windows(request.content, before["squiggles"])
220
+ cur_code = request.content
221
+ cur_hyg = before
222
+ line_shift = 0
223
+ accepted: list[Verdict] = []
224
+ passes_used = 0
225
+ last_reason: str | None = None
226
+ allow_second_pass = request.max_passes >= 2 and len(windows) <= 4
227
+
228
+ for orig_lo, orig_hi in windows:
229
+ total_lines = cur_code.count("\n") + 1
230
+ w_lo = max(0, min(total_lines, orig_lo + line_shift))
231
+ w_hi = max(w_lo, min(total_lines, orig_hi + line_shift))
232
+ has_sq = any(w_lo < int(sq["line"]) <= w_hi for sq in cur_hyg["squiggles"])
233
+ if not has_sq or w_lo >= w_hi:
234
+ continue
235
+
236
+ chunk_kept, chunk_passes, reason = self._run_single_chunk(
237
+ cur_code, cur_hyg, w_lo, w_hi, allow_second_pass
238
+ )
239
+ passes_used = max(passes_used, chunk_passes)
240
+ if chunk_kept:
241
+ latest = chunk_kept[-1]
242
+ line_shift += latest.content.count("\n") - cur_code.count("\n")
243
+ cur_code = latest.content
244
+ cur_hyg = latest.hygiene
245
+ accepted.extend(chunk_kept)
246
+ else:
247
+ last_reason = reason
248
+
249
+ return {
250
+ "accepted": accepted,
251
+ "reason": None if accepted else last_reason,
252
+ "passes": passes_used,
253
+ }
254
+
255
  def _sampled_retry(self, before: dict[str, Any]) -> tuple[str | None, Verdict | None]:
256
  tries = [self._attempt(2, self.request.content, before, sampled=True) for _ in range(2)]
257
  valid = [verdict for verdict in tries if verdict.accepted]
 
267
  raise RefinerUnavailable(f"{type(exc).__name__}: {exc}") from exc
268
 
269
  def _attempt(self, pass_no: int, base: str, hygiene: dict[str, Any], sampled: bool) -> Verdict:
270
+ window = select_code_window(base, hygiene, self.request.threshold)
271
+ return self._attempt_window(pass_no, window, hygiene, sampled)
272
+
273
+ def _attempt_window(
274
+ self, pass_no: int, window: CodeWindow, hygiene: dict[str, Any], sampled: bool
275
+ ) -> Verdict:
276
  if self.cancel.is_set():
277
  raise Cancelled()
278
  self.attempts += 1
279
  number = self.attempts
280
  self.emit("attempt", {"n": number, "pass": pass_no, "sampled": sampled})
 
281
  prompt = build_prompt(window.excerpt, window.prompt_hygiene, self.request.filename)
282
  if self.refiner.prompt_fits(prompt):
283
  reply = self._generate(prompt, window, sampled)
284
  verdict = verify_window_patch(
285
+ window,
286
+ reply,
287
+ self.request.filename,
288
+ self.request.threshold,
289
+ hygiene,
290
+ self.request.content,
291
  )
292
  verdict.reply = reply
293
  else:
294
+ verdict = Verdict(
295
+ False, "too_long", window.full_content, squiggles_before=len(hygiene["squiggles"])
296
+ )
297
  self.emit("verdict", {
298
  "n": number,
299
  "accepted": verdict.accepted,
server/windowing.py CHANGED
@@ -1,7 +1,7 @@
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
@@ -14,15 +14,39 @@ from .vendored_guards import (
14
  )
15
  from .vendored_pipeline import apply_changes
16
 
17
- WINDOW_MAX_CHARS = 5_500
18
- WINDOW_MAX_LINES = 140
19
- WINDOW_PROMPT_SQUIGGLES = 12
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
 
@@ -39,86 +63,199 @@ class CodeWindow:
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(
@@ -127,13 +264,15 @@ def verify_window_patch(
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
@@ -153,17 +292,28 @@ def verify_window_patch(
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
 
@@ -173,7 +323,7 @@ def verify_window_patch(
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,
 
1
  from __future__ import annotations
2
 
 
3
  from dataclasses import dataclass
4
+ import re
5
  from typing import Any
6
 
7
  from .guards import Verdict, verify_patch
 
14
  )
15
  from .vendored_pipeline import apply_changes
16
 
17
+ CHUNK_LINE_THRESHOLD = 160
18
+ CHUNK_CHAR_THRESHOLD = 8_500
19
+ MAX_WINDOW_LINES = 84
20
+ MAX_CHUNK_CHARS = 7_500
21
+ MAX_PROMPT_SQUIGGLES = 16
22
+ MAX_CHUNKS_PER_FILE = 12
23
 
24
+ _COL0_DECL_PREFIXES = (
25
+ "def ",
26
+ "async def ",
27
+ "class ",
28
+ "export ",
29
+ "function ",
30
+ "async function ",
31
+ "pub fn ",
32
+ "pub async fn ",
33
+ "pub struct ",
34
+ "impl ",
35
+ "fn ",
36
+ "func ",
37
+ "static ",
38
+ "void ",
39
+ "int ",
40
+ "bool ",
41
+ )
42
+ _METHOD_DECL_RE = re.compile(
43
+ r"^[ \t]{2,8}(?:async\s+def|def|pub\s+(?:async\s+)?fn|fn|"
44
+ r"(?:public|private|protected|static|async)\s+[A-Za-z_$])\b"
45
+ )
46
+ _LOCAL_ASSIGN_RE = re.compile(
47
+ r"^\s*(?:(?:let|const|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b"
48
+ r"|([A-Za-z_][A-Za-z0-9_]*)\s*:=)",
49
+ re.MULTILINE,
50
  )
51
 
52
 
 
63
  if not self.is_sliced:
64
  return updated_excerpt
65
  lines = self.full_content.split("\n")
66
+ patched_lines = updated_excerpt.split("\n")
67
+ return "\n".join([*lines[: self.start_line], *patched_lines, *lines[self.end_line :]])
68
+
69
+
70
+ def should_chunk_file(content: str) -> bool:
71
+ if len(content) > CHUNK_CHAR_THRESHOLD:
72
+ return True
73
+ return content.count("\n") + 1 > CHUNK_LINE_THRESHOLD
74
+
75
+
76
+ def _is_col0_decl(line: str) -> bool:
77
+ return line.startswith(_COL0_DECL_PREFIXES)
78
+
79
+
80
+ def _is_decorator_or_attr(line: str) -> bool:
81
+ stripped = line.strip()
82
+ return stripped.startswith("@") or stripped.startswith("#[")
83
+
84
+
85
+ def _snap_chunk_start(src_lines: list[str], cluster_lo: int, pad: int) -> int:
86
+ floor = max(0, cluster_lo - 55)
87
+ for scan in range(cluster_lo, floor - 1, -1):
88
+ if _is_col0_decl(src_lines[scan]):
89
+ while scan > 0 and _is_decorator_or_attr(src_lines[scan - 1]):
90
+ scan -= 1
91
+ return scan
92
+ for scan in range(cluster_lo, floor - 1, -1):
93
+ if _METHOD_DECL_RE.match(src_lines[scan]):
94
+ while scan > 0 and _is_decorator_or_attr(src_lines[scan - 1]):
95
+ scan -= 1
96
+ return scan
97
+ return max(0, cluster_lo - pad)
98
+
99
+
100
+ def _snap_chunk_end(src_lines: list[str], cluster_hi: int, pad: int, use_methods: bool) -> int:
101
+ total = len(src_lines)
102
+ limit = min(total, cluster_hi + 55)
103
+ end = min(total, cluster_hi + pad + 1)
104
+ for scan in range(cluster_hi + 1, limit):
105
+ line = src_lines[scan]
106
+ is_method = use_methods and bool(_METHOD_DECL_RE.match(line))
107
+ if _is_col0_decl(line) or is_method:
108
+ end = scan
109
+ while end > cluster_hi + 1 and _is_decorator_or_attr(src_lines[end - 1]):
110
+ end -= 1
111
+ break
112
+ while end > cluster_hi + 1 and not src_lines[end - 1].strip():
113
+ end -= 1
114
+ return end
115
+
116
+
117
+ def _clamp_chunk_chars(
118
+ src_lines: list[str],
119
+ w_lo: int,
120
+ w_hi: int,
121
+ c_lo: int,
122
+ c_hi: int,
123
+ max_chars: int = MAX_CHUNK_CHARS,
124
  ) -> tuple[int, int]:
125
+ char_total = sum(len(src_lines[idx]) + 1 for idx in range(w_lo, w_hi))
126
+ while w_hi - w_lo > 12 and char_total > max_chars:
127
+ if w_hi - 1 > c_hi and (w_hi - c_hi) >= (c_lo - w_lo):
128
+ w_hi -= 1
129
+ char_total -= len(src_lines[w_hi]) + 1
130
+ elif w_lo < c_lo:
131
+ char_total -= len(src_lines[w_lo]) + 1
132
+ w_lo += 1
 
133
  else:
134
+ w_hi -= 1
135
+ char_total -= len(src_lines[w_hi]) + 1
136
+ return w_lo, w_hi
137
+
138
+
139
+ def extract_squiggle_windows(
140
+ content: str,
141
+ squiggles: list[dict[str, Any]],
142
+ max_gap: int = 16,
143
+ pad: int = 14,
144
+ max_window_lines: int = MAX_WINDOW_LINES,
145
+ ) -> list[tuple[int, int]]:
146
+ """Partition squiggles into ordered, disjoint 0-based [w_lo, w_hi) declaration chunks."""
147
+ src_lines = content.replace("\r\n", "\n").split("\n")
148
+ n_lines = len(src_lines)
149
+ sq_indices = sorted(
150
+ {max(0, min(n_lines - 1, int(sq["line"]) - 1)) for sq in squiggles if "line" in sq}
151
+ )
152
+ if not sq_indices:
153
+ return []
154
+
155
+ clusters: list[tuple[int, int]] = []
156
+ for idx in sq_indices:
157
+ prev_ok = bool(clusters) and idx - clusters[-1][1] <= max_gap
158
+ span_ok = bool(clusters) and idx - clusters[-1][0] <= max_window_lines - 16
159
+ if prev_ok and span_ok:
160
+ clusters[-1] = (clusters[-1][0], idx)
161
+ else:
162
+ clusters.append((idx, idx))
163
+
164
+ windows: list[tuple[int, int]] = []
165
+ for c_lo, c_hi in clusters:
166
+ w_lo = _snap_chunk_start(src_lines, c_lo, pad)
167
+ started_on_method = bool(_METHOD_DECL_RE.match(src_lines[w_lo]))
168
+ w_hi = _snap_chunk_end(src_lines, c_hi, pad, use_methods=started_on_method)
169
+ w_lo, w_hi = _clamp_chunk_chars(src_lines, w_lo, w_hi, c_lo, c_hi)
170
+
171
+ if windows and w_lo <= windows[-1][1]:
172
+ merged_hi = max(windows[-1][1], w_hi)
173
+ merged_chars = sum(len(src_lines[i]) + 1 for i in range(windows[-1][0], merged_hi))
174
+ if merged_hi - windows[-1][0] <= max_window_lines and merged_chars <= MAX_CHUNK_CHARS:
175
+ windows[-1] = (windows[-1][0], merged_hi)
176
+ else:
177
+ split_lo = max(windows[-1][1], max(0, c_lo - 4))
178
+ if split_lo < w_hi:
179
+ windows.append((split_lo, w_hi))
180
+ else:
181
+ windows.append((w_lo, w_hi))
182
+
183
+ if len(windows) <= MAX_CHUNKS_PER_FILE:
184
+ return windows
185
+
186
+ scored = sorted(
187
+ windows,
188
+ key=lambda span: sum(1 for idx in sq_indices if span[0] <= idx < span[1]),
189
+ reverse=True,
190
+ )
191
+ return sorted(scored[:MAX_CHUNKS_PER_FILE], key=lambda span: span[0])
192
+
193
+
194
+ def build_chunk_window(
195
+ full_content: str,
196
+ full_hygiene: dict[str, Any],
197
+ w_lo: int,
198
+ w_hi: int,
199
+ threshold: float = 0.55,
200
+ ) -> CodeWindow:
201
+ src_lines = full_content.split("\n")
202
+ n_lines = len(src_lines)
203
+ is_sliced = w_lo > 0 or w_hi < n_lines
204
+ if not is_sliced:
205
+ squiggles = list(full_hygiene.get("squiggles", []))[:MAX_PROMPT_SQUIGGLES]
206
+ return CodeWindow(full_content, full_content, {**full_hygiene, "squiggles": squiggles}, 0, n_lines, False)
207
+
208
+ excerpt = "\n".join(src_lines[w_lo:w_hi])
209
+ win_res = score_dict(excerpt, threshold)
210
+ shifted = [
211
+ {**sq, "line": int(sq["line"]) - w_lo}
212
+ for sq in full_hygiene.get("squiggles", [])
213
+ if w_lo < int(sq.get("line", 0)) <= w_hi
214
+ ][:MAX_PROMPT_SQUIGGLES]
215
+ win_res["squiggles"] = shifted or win_res.get("squiggles", [])[:MAX_PROMPT_SQUIGGLES]
216
+ return CodeWindow(full_content, excerpt, win_res, w_lo, w_hi, True)
217
 
218
 
219
  def select_code_window(
220
  content: str,
221
  hygiene: dict[str, Any],
222
+ threshold: float = 0.55,
223
  ) -> CodeWindow:
224
+ """Select the first squiggle chunk window (or whole file when under the chunk threshold)."""
225
  squiggles = list(hygiene.get("squiggles", []))
226
+ if not should_chunk_file(content) or not squiggles:
227
+ total = content.count("\n") + 1
228
+ capped = {**hygiene, "squiggles": squiggles[:MAX_PROMPT_SQUIGGLES]}
229
+ return CodeWindow(content, content, capped, 0, total, False)
230
+ windows = extract_squiggle_windows(content, squiggles)
231
+ if not windows:
232
+ total = content.count("\n") + 1
233
+ return CodeWindow(content, content, hygiene, 0, total, False)
234
+ w_lo, w_hi = windows[0]
235
+ return build_chunk_window(content, hygiene, w_lo, w_hi, threshold)
236
+
237
+
238
+ def _local_vars_intact_across_boundary(
239
+ full_lines: list[str],
240
+ w_lo: int,
241
+ w_hi: int,
242
+ chunk_before: str,
243
+ chunk_after: str,
244
+ ) -> bool:
245
+ before_vars = {m.group(1) or m.group(2) for m in _LOCAL_ASSIGN_RE.finditer(chunk_before)}
246
+ after_vars = {m.group(1) or m.group(2) for m in _LOCAL_ASSIGN_RE.finditer(chunk_after)}
247
+ dropped = {name for name in (before_vars - after_vars) if name and len(name) > 1}
248
+ if not dropped:
249
+ return True
250
+ neighbor_lines = [
251
+ *full_lines[max(0, w_lo - 40) : w_lo],
252
+ *full_lines[w_hi : min(len(full_lines), w_hi + 50)],
253
+ ]
254
+ neighbor_text = "\n".join(neighbor_lines)
255
+ for name in dropped:
256
+ if re.search(rf"\b{re.escape(name)}\b", neighbor_text):
257
+ return False
258
+ return True
259
 
260
 
261
  def verify_window_patch(
 
264
  filename: str,
265
  threshold: float,
266
  before_hygiene: dict[str, Any],
267
+ original_full_content: str | None = None,
268
  ) -> Verdict:
269
  if not window.is_sliced:
270
  return verify_patch(window.full_content, reply, filename, threshold, before_hygiene)
271
 
272
+ cur_full = window.full_content
273
+ root_full = original_full_content if original_full_content is not None else cur_full
274
  squiggles_before = len(before_hygiene["squiggles"])
275
+ rejected = Verdict(False, None, cur_full, squiggles_before=squiggles_before)
276
  if not reply.strip():
277
  rejected.reason = "empty_output"
278
  return rejected
 
292
  rejected.reason = "line_bloat"
293
  return rejected
294
 
295
+ cur_lines = cur_full.split("\n")
296
+ if not _local_vars_intact_across_boundary(
297
+ cur_lines, window.start_line, window.end_line, window.excerpt, updated_excerpt
298
+ ):
299
+ rejected.reason = "invalid_syntax"
300
+ return rejected
301
+
302
  updated_full = window.splice(updated_excerpt)
303
  if filename and not check_basic_syntax(updated_full, filename):
304
  rejected.reason = "invalid_syntax"
305
  return rejected
306
+ if filename and not check_reference_integrity(cur_full, updated_full, filename):
307
  rejected.reason = "invalid_syntax"
308
  return rejected
309
+ if root_full != cur_full and filename:
310
+ if not check_reference_integrity(root_full, updated_full, filename):
311
+ rejected.reason = "invalid_syntax"
312
+ return rejected
313
+ if is_gutted(root_full, updated_full):
314
  rejected.reason = "gutted_file"
315
  return rejected
316
+ if is_bloated(root_full, updated_full):
317
  rejected.reason = "line_bloat"
318
  return rejected
319
 
 
323
  return Verdict(
324
  accepted=accepted,
325
  reason=None if accepted else "no_improvement",
326
+ content=updated_full if accepted else cur_full,
327
  applied_blocks=applied,
328
  squiggles_before=squiggles_before,
329
  squiggles_after=squiggles_after,
web/src/bonsai-window.js CHANGED
@@ -1,4 +1,4 @@
1
- // Squiggle-centered line windowing for files exceeding Browser WebGPU / ZeroGPU prompt budgets.
2
  import {
3
  applyChanges,
4
  checkBasicSyntax,
@@ -10,76 +10,162 @@ import {
10
  verifyPatch,
11
  } from "./bonsai-guards.js";
12
 
13
- export const WINDOW_MAX_CHARS = 5500;
14
- const WINDOW_MAX_LINES = 140;
15
- const WINDOW_PROMPT_SQUIGGLES = 12;
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) {
@@ -93,40 +179,77 @@ function makeWindowObject(fullContent, excerpt, promptHygiene, startLine, endLin
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) {
@@ -135,17 +258,26 @@ export function previewWindowContent(codeWindow, raw) {
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,
@@ -167,14 +299,26 @@ export function verifyWindowPatch(codeWindow, reply, filename, threshold, before
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;
@@ -182,7 +326,7 @@ export function verifyWindowPatch(codeWindow, reply, filename, threshold, before
182
  return {
183
  accepted,
184
  reason: accepted ? null : "no_improvement",
185
- content: accepted ? updatedFull : fullBefore,
186
  appliedBlocks: patched.applied,
187
  squigglesBefore,
188
  squigglesAfter,
 
1
+ // Declaration-aligned multi-chunk squiggle partitioning and verification matching server/windowing.py.
2
  import {
3
  applyChanges,
4
  checkBasicSyntax,
 
10
  verifyPatch,
11
  } from "./bonsai-guards.js";
12
 
13
+ export const CHUNK_LINE_THRESHOLD = 160;
14
+ export const CHUNK_CHAR_THRESHOLD = 8500;
15
+ export const MAX_WINDOW_LINES = 84;
16
+ export const MAX_CHUNK_CHARS = 7500;
17
+ const MAX_PROMPT_SQUIGGLES = 16;
18
+ const MAX_CHUNKS_PER_FILE = 12;
19
 
20
+ const COL0_DECL_PREFIXES = [
21
+ "def ",
22
+ "async def ",
23
+ "class ",
24
+ "export ",
25
+ "function ",
26
+ "async function ",
27
+ "pub fn ",
28
+ "pub async fn ",
29
+ "pub struct ",
30
+ "impl ",
31
+ "fn ",
32
+ "func ",
33
+ "static ",
34
+ "void ",
35
+ "int ",
36
+ "bool ",
37
+ ];
38
+ const METHOD_DECL_RE = new RegExp(
39
+ "^[ \\t]{2,8}(?:async\\s+def|def|pub\\s+(?:async\\s+)?fn|fn|" +
40
+ "(?:public|private|protected|static|async)\\s+[A-Za-z_$])\\b",
41
  );
42
+ const LOCAL_ASSIGN_RE =
43
+ /^\s*(?:(?:let|const|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b|([A-Za-z_][A-Za-z0-9_]*)\s*:=)/gm;
44
 
45
+ export function shouldChunkFile(content) {
46
+ if (content.length > CHUNK_CHAR_THRESHOLD) return true;
47
+ return content.split("\n").length > CHUNK_LINE_THRESHOLD;
48
+ }
49
+
50
+ function isCol0Decl(line) {
51
+ return COL0_DECL_PREFIXES.some((prefix) => line.startsWith(prefix));
52
+ }
53
+
54
+ function isDecoratorOrAttr(line) {
55
+ const stripped = line.trim();
56
+ return stripped.startsWith("@") || stripped.startsWith("#[");
57
+ }
58
+
59
+ function snapChunkStart(srcLines, clusterLo, pad) {
60
+ const floor = Math.max(0, clusterLo - 55);
61
+ for (let scan = clusterLo; scan >= floor; scan -= 1) {
62
+ if (isCol0Decl(srcLines[scan])) {
63
+ while (scan > 0 && isDecoratorOrAttr(srcLines[scan - 1])) scan -= 1;
64
+ return scan;
65
  }
66
+ }
67
+ for (let scan = clusterLo; scan >= floor; scan -= 1) {
68
+ if (METHOD_DECL_RE.test(srcLines[scan])) {
69
+ while (scan > 0 && isDecoratorOrAttr(srcLines[scan - 1])) scan -= 1;
70
+ return scan;
71
  }
72
  }
73
+ return Math.max(0, clusterLo - pad);
74
  }
75
 
76
+ function snapChunkEnd(srcLines, clusterHi, pad, useMethods) {
77
+ const total = srcLines.length;
78
+ const limit = Math.min(total, clusterHi + 55);
79
+ let end = Math.min(total, clusterHi + pad + 1);
80
+ for (let scan = clusterHi + 1; scan < limit; scan += 1) {
81
+ const hit = isCol0Decl(srcLines[scan]) || (useMethods && METHOD_DECL_RE.test(srcLines[scan]));
82
+ if (hit) {
83
+ end = scan;
84
+ while (end > clusterHi + 1 && isDecoratorOrAttr(srcLines[end - 1])) end -= 1;
85
+ break;
86
  }
87
  }
88
+ while (end > clusterHi + 1 && !srcLines[end - 1].trim()) end -= 1;
89
+ return end;
90
  }
91
 
92
+ function clampChunkChars(srcLines, initLo, initHi, cLo, cHi, maxChars = MAX_CHUNK_CHARS) {
93
+ let wLo = initLo;
94
+ let wHi = initHi;
95
+ let charTotal = 0;
96
+ for (let i = wLo; i < wHi; i += 1) charTotal += srcLines[i].length + 1;
97
+ while (wHi - wLo > 12 && charTotal > maxChars) {
98
+ if (wHi - 1 > cHi && wHi - cHi >= cLo - wLo) {
99
+ wHi -= 1;
100
+ charTotal -= srcLines[wHi].length + 1;
101
+ } else if (wLo < cLo) {
102
+ charTotal -= srcLines[wLo].length + 1;
103
+ wLo += 1;
 
 
 
104
  } else {
105
+ wHi -= 1;
106
+ charTotal -= srcLines[wHi].length + 1;
107
  }
108
  }
109
+ return [wLo, wHi];
110
+ }
111
+
112
+ export function extractSquiggleWindows(
113
+ content,
114
+ squiggles,
115
+ maxGap = 16,
116
+ pad = 14,
117
+ maxWindowLines = MAX_WINDOW_LINES,
118
+ ) {
119
+ const srcLines = content.replace(/\r\n/g, "\n").split("\n");
120
+ const nLines = srcLines.length;
121
+ const uniq = new Set();
122
+ for (const sq of squiggles ?? []) {
123
+ if (sq.line !== undefined) {
124
+ uniq.add(Math.max(0, Math.min(nLines - 1, Number(sq.line) - 1)));
125
+ }
126
+ }
127
+ const sqIndices = [...uniq].sort((a, b) => a - b);
128
+ if (sqIndices.length === 0) return [];
129
+
130
+ const clusters = [];
131
+ for (const idx of sqIndices) {
132
+ const prev = clusters[clusters.length - 1];
133
+ if (prev && idx - prev[1] <= maxGap && idx - prev[0] <= maxWindowLines - 16) {
134
+ prev[1] = idx;
135
+ } else {
136
+ clusters.push([idx, idx]);
137
+ }
138
  }
139
+
140
+ const windows = [];
141
+ for (const [cLo, cHi] of clusters) {
142
+ const rawLo = snapChunkStart(srcLines, cLo, pad);
143
+ const startedOnMethod = METHOD_DECL_RE.test(srcLines[rawLo]);
144
+ const rawHi = snapChunkEnd(srcLines, cHi, pad, startedOnMethod);
145
+ const [wLo, wHi] = clampChunkChars(srcLines, rawLo, rawHi, cLo, cHi);
146
+
147
+ const last = windows[windows.length - 1];
148
+ if (last && wLo <= last[1]) {
149
+ const mergedHi = Math.max(last[1], wHi);
150
+ let mergedChars = 0;
151
+ for (let i = last[0]; i < mergedHi; i += 1) mergedChars += srcLines[i].length + 1;
152
+ if (mergedHi - last[0] <= maxWindowLines && mergedChars <= MAX_CHUNK_CHARS) {
153
+ last[1] = mergedHi;
154
+ } else {
155
+ const splitLo = Math.max(last[1], Math.max(0, cLo - 4));
156
+ if (splitLo < wHi) windows.push([splitLo, wHi]);
157
+ }
158
+ } else {
159
+ windows.push([wLo, wHi]);
160
+ }
161
+ }
162
+
163
+ if (windows.length <= MAX_CHUNKS_PER_FILE) return windows;
164
+ const ranked = [...windows].sort((left, right) => {
165
+ const countIn = ([lo, hi]) => sqIndices.filter((idx) => idx >= lo && idx < hi).length;
166
+ return countIn(right) - countIn(left);
167
+ });
168
+ return ranked.slice(0, MAX_CHUNKS_PER_FILE).sort((a, b) => a[0] - b[0]);
169
  }
170
 
171
  function makeWindowObject(fullContent, excerpt, promptHygiene, startLine, endLine, isSliced) {
 
179
  splice(updatedExcerpt) {
180
  if (!isSliced) return updatedExcerpt;
181
  const lines = fullContent.split("\n");
182
+ return [...lines.slice(0, startLine), ...updatedExcerpt.split("\n"), ...lines.slice(endLine)].join("\n");
183
  },
184
  };
185
  }
186
 
187
+ export function buildChunkWindow(fullContent, fullHygiene, wLo, wHi, weights, threshold = 0.55) {
188
+ const srcLines = fullContent.split("\n");
189
+ const nLines = srcLines.length;
190
+ const isSliced = wLo > 0 || wHi < nLines;
191
+ if (!isSliced) {
192
+ const capped = (fullHygiene.squiggles ?? []).slice(0, MAX_PROMPT_SQUIGGLES);
193
+ return makeWindowObject(
194
+ fullContent,
195
+ fullContent,
196
+ { ...fullHygiene, squiggles: capped },
197
+ 0,
198
+ nLines,
199
+ false,
200
+ );
201
+ }
202
+ const excerpt = srcLines.slice(wLo, wHi).join("\n");
203
+ const winRes = scoreHygieneDict(excerpt, weights, threshold);
204
+ const shifted = (fullHygiene.squiggles ?? [])
205
+ .filter((sq) => Number(sq.line) > wLo && Number(sq.line) <= wHi)
206
+ .slice(0, MAX_PROMPT_SQUIGGLES)
207
+ .map((sq) => ({ ...sq, line: Number(sq.line) - wLo }));
208
+ winRes.squiggles = shifted.length > 0 ? shifted : (winRes.squiggles ?? []).slice(0, MAX_PROMPT_SQUIGGLES);
209
+ return makeWindowObject(fullContent, excerpt, winRes, wLo, wHi, true);
210
+ }
211
+
212
+ export function selectCodeWindow(content, hygiene, weights, threshold = 0.55) {
213
  const squiggles = hygiene.squiggles ?? [];
214
+ const totalLines = content.split("\n").length;
215
+ if (!shouldChunkFile(content) || squiggles.length === 0) {
216
+ const capped = { ...hygiene, squiggles: squiggles.slice(0, MAX_PROMPT_SQUIGGLES) };
217
+ return makeWindowObject(content, content, capped, 0, totalLines, false);
218
+ }
219
+ const windows = extractSquiggleWindows(content, squiggles);
220
+ if (windows.length === 0) {
221
+ return makeWindowObject(content, content, hygiene, 0, totalLines, false);
222
+ }
223
+ const [wLo, wHi] = windows[0];
224
+ return buildChunkWindow(content, hygiene, wLo, wHi, weights, threshold);
225
+ }
226
+
227
+ function collectAssignedVars(code) {
228
+ const found = new Set();
229
+ LOCAL_ASSIGN_RE.lastIndex = 0;
230
+ let m = LOCAL_ASSIGN_RE.exec(code);
231
+ while (m !== null) {
232
+ const name = m[1] || m[2] || "";
233
+ if (name.length > 1) found.add(name);
234
+ m = LOCAL_ASSIGN_RE.exec(code);
235
+ }
236
+ return found;
237
+ }
238
+
239
+ function localVarsIntactAcrossBoundary(fullLines, wLo, wHi, chunkBefore, chunkAfter) {
240
+ const beforeVars = collectAssignedVars(chunkBefore);
241
+ const afterVars = collectAssignedVars(chunkAfter);
242
+ const dropped = [...beforeVars].filter((name) => !afterVars.has(name));
243
+ if (dropped.length === 0) return true;
244
+ const neighborText = [
245
+ ...fullLines.slice(Math.max(0, wLo - 40), wLo),
246
+ ...fullLines.slice(wHi, Math.min(fullLines.length, wHi + 50)),
247
+ ].join("\n");
248
+ for (const name of dropped) {
249
+ const escaped = name.replace(/\$/g, "\\$");
250
+ if (new RegExp(`\\b${escaped}\\b`).test(neighborText)) return false;
251
+ }
252
+ return true;
253
  }
254
 
255
  export function previewWindowContent(codeWindow, raw) {
 
258
  return { content: codeWindow.splice(found.content), applied: found.applied };
259
  }
260
 
261
+ export function verifyWindowPatch(
262
+ codeWindow,
263
+ reply,
264
+ filename,
265
+ threshold,
266
+ beforeHygiene,
267
+ weights,
268
+ originalFullContent = null,
269
+ ) {
270
  if (!codeWindow.isSliced) {
271
  return verifyPatch(codeWindow.fullContent, reply, filename, threshold, beforeHygiene, weights);
272
  }
273
 
274
+ const curFull = codeWindow.fullContent;
275
+ const rootFull = originalFullContent ?? curFull;
276
  const squigglesBefore = beforeHygiene.squiggles.length;
277
  const reject = (reason, applied = 0) => ({
278
  accepted: false,
279
  reason,
280
+ content: curFull,
281
  appliedBlocks: applied,
282
  squigglesBefore,
283
  squigglesAfter: null,
 
299
  if (isGutted(codeWindow.excerpt, patched.content)) return reject("gutted_file", patched.applied);
300
  if (isBloated(codeWindow.excerpt, patched.content)) return reject("line_bloat", patched.applied);
301
 
302
+ const curLines = curFull.split("\n");
303
+ const boundaryOk = localVarsIntactAcrossBoundary(
304
+ curLines,
305
+ codeWindow.startLine,
306
+ codeWindow.endLine,
307
+ codeWindow.excerpt,
308
+ patched.content,
309
+ );
310
+ if (!boundaryOk) return reject("invalid_syntax", patched.applied);
311
+
312
  const updatedFull = codeWindow.splice(patched.content);
313
  if (filename) {
314
  const syntaxOk = checkBasicSyntax(updatedFull, filename);
315
+ const curRefsOk = syntaxOk && checkReferenceIntegrity(curFull, updatedFull, filename);
316
+ const rootRefsOk =
317
+ curRefsOk && (rootFull === curFull || checkReferenceIntegrity(rootFull, updatedFull, filename));
318
+ if (!rootRefsOk) return reject("invalid_syntax", patched.applied);
319
  }
320
+ if (isGutted(rootFull, updatedFull)) return reject("gutted_file", patched.applied);
321
+ if (isBloated(rootFull, updatedFull)) return reject("line_bloat", patched.applied);
322
 
323
  const after = scoreHygieneDict(updatedFull, weights, threshold);
324
  const squigglesAfter = after.squiggles.length;
 
326
  return {
327
  accepted,
328
  reason: accepted ? null : "no_improvement",
329
+ content: accepted ? updatedFull : curFull,
330
  appliedBlocks: patched.applied,
331
  squigglesBefore,
332
  squigglesAfter,
web/src/bonsai-worker.js CHANGED
@@ -1,9 +1,19 @@
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 };
@@ -15,13 +25,13 @@ let enginePromise = null;
15
  let loadedBaseUrl = null;
16
  const cancelledIds = new Set();
17
 
18
- function getEngine(baseUrl, forceBrowser, onLoadProgress) {
19
  const resolvedBase = baseUrl || DEFAULT_MODEL_BASE;
20
  if (!enginePromise || loadedBaseUrl !== resolvedBase) {
21
  loadedBaseUrl = resolvedBase;
22
  enginePromise = BonsaiWebGpuEngine.load({
23
  baseUrl: resolvedBase,
24
- skipSpeedGate: Boolean(forceBrowser),
25
  onProgress: onLoadProgress,
26
  }).catch((err) => {
27
  enginePromise = null;
@@ -31,23 +41,99 @@ function getEngine(baseUrl, forceBrowser, onLoadProgress) {
31
  return enginePromise;
32
  }
33
 
34
- function fitWindowToEngine(engine, code, hygiene, filename) {
35
- let codeWindow = selectCodeWindow(code, hygiene);
36
- let prompt = buildPrompt(codeWindow.excerpt, codeWindow.promptHygiene, filename);
37
- if (!engine.promptFits(engine.tokenizer.encode(prompt), 512)) {
38
- codeWindow = selectCodeWindow(code, hygiene, 2800);
39
- prompt = buildPrompt(codeWindow.excerpt, codeWindow.promptHygiene, filename);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  }
41
- return { codeWindow, prompt };
42
  }
43
 
44
- async function runSingleAttempt({
45
- engine,
46
  weights,
47
  attemptNo,
48
  passNo,
49
- base,
50
- hygiene,
 
51
  filename,
52
  threshold,
53
  sampled,
@@ -57,11 +143,11 @@ async function runSingleAttempt({
57
  if (isCancelled()) return { cancelled: true };
58
  emit("attempt", { n: attemptNo, pass: passNo, sampled });
59
 
60
- const { codeWindow, prompt } = fitWindowToEngine(engine, base, hygiene, filename);
61
  const sampling = sampled ? SAMPLED : GREEDY;
62
  let raw = "";
63
  let tokens = 0;
64
- let lastPreview = base;
65
  let lastPreviewAt = 0;
66
  let lastProgressAt = -PROGRESS_INTERVAL_MS;
67
  let readFinished = false;
@@ -75,8 +161,8 @@ async function runSingleAttempt({
75
  }
76
  };
77
 
78
- const reply = await engine.streamCompletion(prompt, {
79
- maxNewTokens: 1024,
80
  temperature: sampling.temperature,
81
  topP: sampling.topP,
82
  shouldCancel: isCancelled,
@@ -102,7 +188,15 @@ async function runSingleAttempt({
102
  if (isCancelled()) return { cancelled: true };
103
  flushPreview();
104
 
105
- const verdict = verifyWindowPatch(codeWindow, reply, filename, threshold, hygiene, weights);
 
 
 
 
 
 
 
 
106
  emit("verdict", {
107
  n: attemptNo,
108
  accepted: verdict.accepted,
@@ -115,8 +209,101 @@ async function runSingleAttempt({
115
  return { cancelled: false, verdict, tokens };
116
  }
117
 
118
- async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
119
- const isCancelled = () => cancelledIds.has(id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  const emit = (name, data) => {
121
  if (!isCancelled()) {
122
  globalThis.postMessage({ id, event: name, data });
@@ -131,8 +318,9 @@ async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
131
  const content = body.content ?? "";
132
 
133
  const before = scoreHygieneDict(content, weights, threshold);
 
134
  if (!before.squiggles || before.squiggles.length === 0) {
135
- emit("start", { before_hygiene: before, model_id: "bonsai-1.7b-webgpu", cached: false });
136
  emit("done", {
137
  filename,
138
  changes: "",
@@ -149,36 +337,38 @@ async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
149
  return;
150
  }
151
 
152
- let lastLoadEmit = 0;
153
- const engine = await getEngine(modelBaseUrl, forceBrowser, (done, total) => {
154
- const now = performance.now();
155
- if (done < total && now - lastLoadEmit < PROGRESS_INTERVAL_MS) return;
156
- lastLoadEmit = now;
157
- emit("progress", { phase: "read", done, total });
158
- });
159
-
160
- const { prompt: firstPrompt } = fitWindowToEngine(engine, content, before, filename);
161
- if (!engine.promptFits(engine.tokenizer.encode(firstPrompt), 256)) {
162
- throw new Error("Prompt exceeds browser WebGPU context budget.");
 
 
 
 
 
 
163
  }
164
 
165
- emit("start", { before_hygiene: before, model_id: "bonsai-1.7b-webgpu", cached: false });
166
 
167
  let attemptCount = 0;
168
- let totalTokens = 0;
169
- const acceptedList = [];
170
- let passesUsed = 1;
171
- let rejectReason = null;
172
-
173
- const nextAttempt = (passNo, base, hygiene, sampled) => {
174
  attemptCount += 1;
175
- return runSingleAttempt({
176
- engine,
177
  weights,
178
  attemptNo: attemptCount,
179
  passNo,
180
- base,
181
- hygiene,
 
182
  filename,
183
  threshold,
184
  sampled,
@@ -187,45 +377,63 @@ async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
187
  });
188
  };
189
 
190
- const first = await nextAttempt(1, content, before, false);
191
- if (first.cancelled) return;
192
- totalTokens += first.tokens;
193
- if (first.verdict.accepted) {
194
- acceptedList.push(first.verdict);
195
- }
196
 
197
- if (maxPasses >= 2) {
198
- if (first.verdict.accepted && first.verdict.squigglesAfter > 0) {
199
- passesUsed = 2;
200
- const second = await nextAttempt(2, first.verdict.content, first.verdict.hygiene, false);
201
- if (second.cancelled) return;
202
- totalTokens += second.tokens;
203
- if (second.verdict.accepted) {
204
- acceptedList.push(second.verdict);
205
- }
206
- } else if (!first.verdict.accepted) {
207
- passesUsed = 2;
208
- const retries = [];
209
- for (let r = 0; r < 2; r += 1) {
210
- const res = await nextAttempt(2, content, before, true);
211
- if (res.cancelled) return;
212
- totalTokens += res.tokens;
213
- retries.push(res.verdict);
214
- }
215
- const valid = retries.filter((v) => v.accepted);
216
- if (valid.length > 0) {
217
- valid.sort((a, b) => {
218
- const sqDiff = a.squigglesAfter - b.squigglesAfter;
219
- return sqDiff !== 0 ? sqDiff : (b.scoreAfter ?? 0) - (a.scoreAfter ?? 0);
220
- });
221
- acceptedList.push(valid[0]);
222
- } else {
223
- rejectReason = retries[retries.length - 1].reason;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  }
225
  }
226
- }
227
- if (acceptedList.length === 0 && rejectReason === null) {
228
- rejectReason = first.verdict.reason;
229
  }
230
 
231
  const finalVerdict = acceptedList.length > 0 ? acceptedList[acceptedList.length - 1] : null;
@@ -248,25 +456,27 @@ async function runFixJob(id, body, modelBaseUrl, forceBrowser) {
248
  let queueChain = Promise.resolve();
249
 
250
  globalThis.addEventListener("message", (messageEvent) => {
251
- const { type, id, body, modelBaseUrl, forceBrowser } = messageEvent.data;
252
  if (type === "cancel") {
253
- cancelledIds.add(id);
 
 
254
  return;
255
  }
256
  if (type === "fix") {
257
  queueChain = queueChain.then(async () => {
258
- if (cancelledIds.has(id)) {
 
259
  cancelledIds.delete(id);
260
  globalThis.postMessage({ id, done: true });
261
  return;
262
  }
263
  try {
264
- await runFixJob(id, body, modelBaseUrl, forceBrowser);
265
  globalThis.postMessage({ id, done: true });
266
  } catch (err) {
267
  const message = err && err.message ? err.message : String(err);
268
- const disableWebGpu = !/context|budget/i.test(message);
269
- globalThis.postMessage({ id, fallback: true, disableWebGpu, error: message });
270
  } finally {
271
  cancelledIds.delete(id);
272
  }
 
1
+ // Worker executing multi-chunk Bonsai 1.7B cleanup via ZeroGPU SSE (or opt-in Browser WebGPU).
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 {
6
+ buildChunkWindow,
7
+ extractSquiggleWindows,
8
+ previewWindowContent,
9
+ selectCodeWindow,
10
+ shouldChunkFile,
11
+ verifyWindowPatch,
12
+ } from "./bonsai-window.js";
13
 
14
+ const DEFAULT_ZEROGPU_BASE = "https://brianbellx-codehygiene-zerogpu.hf.space";
15
+ const STOP_MARKER = "<|im_end|>";
16
+ const PIECE_PATTERN = new RegExp(" ?\\w{1,6}| ?[^\\w\\s]{1,3}|\\s+", "g");
17
  const PREVIEW_INTERVAL_MS = 85;
18
  const PROGRESS_INTERVAL_MS = 200;
19
  const GREEDY = { temperature: 0.0, topP: 1.0 };
 
25
  let loadedBaseUrl = null;
26
  const cancelledIds = new Set();
27
 
28
+ function getWebGpuEngine(baseUrl, onLoadProgress) {
29
  const resolvedBase = baseUrl || DEFAULT_MODEL_BASE;
30
  if (!enginePromise || loadedBaseUrl !== resolvedBase) {
31
  loadedBaseUrl = resolvedBase;
32
  enginePromise = BonsaiWebGpuEngine.load({
33
  baseUrl: resolvedBase,
34
+ skipSpeedGate: true,
35
  onProgress: onLoadProgress,
36
  }).catch((err) => {
37
  enginePromise = null;
 
41
  return enginePromise;
42
  }
43
 
44
+ async function streamZeroGpuCompletion(prompt, {
45
+ zeroGpuUrl = DEFAULT_ZEROGPU_BASE,
46
+ maxNewTokens = 1280,
47
+ temperature = 0.0,
48
+ topP = 1.0,
49
+ signal,
50
+ shouldCancel,
51
+ onProgress,
52
+ onToken,
53
+ }) {
54
+ const base = zeroGpuUrl.replace(/\/$/, "");
55
+ const totalEst = Math.max(1, Math.floor(prompt.length / 3));
56
+ onProgress(0, totalEst);
57
+ const startRes = await fetch(`${base}/gradio_api/call/stream_patch`, {
58
+ method: "POST",
59
+ headers: { "Content-Type": "application/json" },
60
+ body: JSON.stringify({ data: [prompt, maxNewTokens, temperature, topP] }),
61
+ signal,
62
+ });
63
+ if (!startRes.ok) {
64
+ throw new Error(`ZeroGPU start failed (${startRes.status})`);
65
+ }
66
+ const { event_id: eventId } = await startRes.json();
67
+ const sseRes = await fetch(`${base}/gradio_api/call/stream_patch/${eventId}`, {
68
+ headers: { Accept: "text/event-stream" },
69
+ signal,
70
+ });
71
+ if (!sseRes.ok || !sseRes.body) {
72
+ throw new Error(`ZeroGPU stream failed (${sseRes.status})`);
73
+ }
74
+
75
+ const reader = sseRes.body.pipeThrough(new TextDecoderStream()).getReader();
76
+ let sseBuffer = "";
77
+ let sent = 0;
78
+ let fullReply = "";
79
+ let progressDone = false;
80
+
81
+ for (;;) {
82
+ if (shouldCancel()) {
83
+ await reader.cancel();
84
+ return fullReply;
85
+ }
86
+ const { value, done } = await reader.read();
87
+ if (done) break;
88
+ sseBuffer = (sseBuffer + value).replace(/\r\n?/g, "\n");
89
+ let boundary = sseBuffer.indexOf("\n\n");
90
+ while (boundary >= 0) {
91
+ const frame = sseBuffer.slice(0, boundary);
92
+ sseBuffer = sseBuffer.slice(boundary + 2);
93
+ boundary = sseBuffer.indexOf("\n\n");
94
+
95
+ let eventName = "";
96
+ let payloadText = "";
97
+ for (const rawLine of frame.split("\n")) {
98
+ if (rawLine.startsWith("event:")) eventName = rawLine.slice(6).trim();
99
+ if (rawLine.startsWith("data:")) payloadText = rawLine.slice(5).trim();
100
+ }
101
+ if (eventName === "error") {
102
+ throw new Error(`ZeroGPU error: ${payloadText}`);
103
+ }
104
+ if (!payloadText || payloadText === "null") continue;
105
+ const parsed = JSON.parse(payloadText);
106
+ const cumulative = Array.isArray(parsed) && parsed.length > 0 ? String(parsed[0] || "") : "";
107
+ if (!progressDone) {
108
+ onProgress(totalEst, totalEst);
109
+ progressDone = true;
110
+ }
111
+ if (cumulative.length > sent) {
112
+ const delta = cumulative.slice(sent).split(STOP_MARKER)[0];
113
+ sent = cumulative.length;
114
+ const pieces = delta.match(PIECE_PATTERN) ?? (delta ? [delta] : []);
115
+ for (const piece of pieces) {
116
+ fullReply += piece;
117
+ onToken(piece);
118
+ }
119
+ }
120
+ if (eventName === "complete") {
121
+ await reader.cancel();
122
+ return fullReply;
123
+ }
124
+ }
125
  }
126
+ return fullReply;
127
  }
128
 
129
+ async function runWindowAttempt({
130
+ runner,
131
  weights,
132
  attemptNo,
133
  passNo,
134
+ codeWindow,
135
+ curHygiene,
136
+ originalFullContent,
137
  filename,
138
  threshold,
139
  sampled,
 
143
  if (isCancelled()) return { cancelled: true };
144
  emit("attempt", { n: attemptNo, pass: passNo, sampled });
145
 
146
+ const prompt = buildPrompt(codeWindow.excerpt, codeWindow.promptHygiene, filename);
147
  const sampling = sampled ? SAMPLED : GREEDY;
148
  let raw = "";
149
  let tokens = 0;
150
+ let lastPreview = codeWindow.fullContent;
151
  let lastPreviewAt = 0;
152
  let lastProgressAt = -PROGRESS_INTERVAL_MS;
153
  let readFinished = false;
 
161
  }
162
  };
163
 
164
+ const reply = await runner(prompt, {
165
+ maxNewTokens: 1280,
166
  temperature: sampling.temperature,
167
  topP: sampling.topP,
168
  shouldCancel: isCancelled,
 
188
  if (isCancelled()) return { cancelled: true };
189
  flushPreview();
190
 
191
+ const verdict = verifyWindowPatch(
192
+ codeWindow,
193
+ reply,
194
+ filename,
195
+ threshold,
196
+ curHygiene,
197
+ weights,
198
+ originalFullContent,
199
+ );
200
  emit("verdict", {
201
  n: attemptNo,
202
  accepted: verdict.accepted,
 
209
  return { cancelled: false, verdict, tokens };
210
  }
211
 
212
+ function countNewlines(text) {
213
+ let count = 0;
214
+ for (let i = 0; i < text.length; i += 1) {
215
+ if (text.charCodeAt(i) === 10) count += 1;
216
+ }
217
+ return count;
218
+ }
219
+
220
+ async function runChunkedLoop({
221
+ content,
222
+ before,
223
+ filename,
224
+ threshold,
225
+ maxPasses,
226
+ weights,
227
+ nextAttempt,
228
+ }) {
229
+ const windows = extractSquiggleWindows(content, before.squiggles);
230
+ let curCode = content;
231
+ let curHyg = before;
232
+ let lineShift = 0;
233
+ const acceptedList = [];
234
+ let passesUsed = 0;
235
+ let lastReason = null;
236
+ let totalTokens = 0;
237
+ const allowSecondPass = maxPasses >= 2 && windows.length <= 4;
238
+
239
+ for (const [origLo, origHi] of windows) {
240
+ const totalLines = countNewlines(curCode) + 1;
241
+ const wLo = Math.max(0, Math.min(totalLines, origLo + lineShift));
242
+ const wHi = Math.max(wLo, Math.min(totalLines, origHi + lineShift));
243
+ const hasSq = (curHyg.squiggles ?? []).some((sq) => Number(sq.line) > wLo && Number(sq.line) <= wHi);
244
+ if (!hasSq || wLo >= wHi) continue;
245
+
246
+ const win1 = buildChunkWindow(curCode, curHyg, wLo, wHi, weights, threshold);
247
+ passesUsed = Math.max(passesUsed, 1);
248
+ const first = await nextAttempt(1, win1, curHyg, false);
249
+ if (first.cancelled) return { cancelled: true };
250
+ totalTokens += first.tokens;
251
+
252
+ if (first.verdict.accepted) {
253
+ const delta = countNewlines(first.verdict.content) - countNewlines(curCode);
254
+ const newHi = wHi + delta;
255
+ lineShift += delta;
256
+ curCode = first.verdict.content;
257
+ curHyg = first.verdict.hygiene;
258
+ acceptedList.push(first.verdict);
259
+ const stillFlagged = (curHyg.squiggles ?? []).some(
260
+ (sq) => Number(sq.line) > wLo && Number(sq.line) <= newHi,
261
+ );
262
+ if (allowSecondPass && stillFlagged) {
263
+ passesUsed = 2;
264
+ const win2 = buildChunkWindow(curCode, curHyg, wLo, newHi, weights, threshold);
265
+ const second = await nextAttempt(2, win2, curHyg, false);
266
+ if (second.cancelled) return { cancelled: true };
267
+ totalTokens += second.tokens;
268
+ if (second.verdict.accepted) {
269
+ lineShift += countNewlines(second.verdict.content) - countNewlines(curCode);
270
+ curCode = second.verdict.content;
271
+ curHyg = second.verdict.hygiene;
272
+ acceptedList.push(second.verdict);
273
+ }
274
+ }
275
+ continue;
276
+ }
277
+
278
+ lastReason = first.verdict.reason;
279
+ if (allowSecondPass && first.verdict.reason !== "too_long") {
280
+ passesUsed = 2;
281
+ const retry = await nextAttempt(2, win1, curHyg, true);
282
+ if (retry.cancelled) return { cancelled: true };
283
+ totalTokens += retry.tokens;
284
+ if (retry.verdict.accepted) {
285
+ lineShift += countNewlines(retry.verdict.content) - countNewlines(curCode);
286
+ curCode = retry.verdict.content;
287
+ curHyg = retry.verdict.hygiene;
288
+ acceptedList.push(retry.verdict);
289
+ } else {
290
+ lastReason = retry.verdict.reason;
291
+ }
292
+ }
293
+ }
294
+ return {
295
+ cancelled: false,
296
+ acceptedList,
297
+ passesUsed,
298
+ rejectReason: acceptedList.length > 0 ? null : lastReason,
299
+ totalTokens,
300
+ };
301
+ }
302
+
303
+ async function runFixJob(id, body, options) {
304
+ const aborter = new AbortController();
305
+ cancelledIds.set(id, aborter);
306
+ const isCancelled = () => aborter.signal.aborted;
307
  const emit = (name, data) => {
308
  if (!isCancelled()) {
309
  globalThis.postMessage({ id, event: name, data });
 
318
  const content = body.content ?? "";
319
 
320
  const before = scoreHygieneDict(content, weights, threshold);
321
+ const modelTag = options.backend === "webgpu" ? "bonsai-1.7b-webgpu" : "brianbellx/codehygiene-bonsai-1.7b";
322
  if (!before.squiggles || before.squiggles.length === 0) {
323
+ emit("start", { before_hygiene: before, model_id: modelTag, cached: false });
324
  emit("done", {
325
  filename,
326
  changes: "",
 
337
  return;
338
  }
339
 
340
+ let runner;
341
+ if (options.backend === "webgpu") {
342
+ let lastLoadEmit = 0;
343
+ const engine = await getWebGpuEngine(options.modelBaseUrl, (done, total) => {
344
+ const now = performance.now();
345
+ if (done < total && now - lastLoadEmit < PROGRESS_INTERVAL_MS) return;
346
+ lastLoadEmit = now;
347
+ emit("progress", { phase: "read", done, total });
348
+ });
349
+ runner = (prompt, opts) => engine.streamCompletion(prompt, { ...opts, maxNewTokens: 1024 });
350
+ } else {
351
+ runner = (prompt, opts) =>
352
+ streamZeroGpuCompletion(prompt, {
353
+ ...opts,
354
+ zeroGpuUrl: options.zeroGpuUrl || DEFAULT_ZEROGPU_BASE,
355
+ signal: aborter.signal,
356
+ });
357
  }
358
 
359
+ emit("start", { before_hygiene: before, model_id: modelTag, cached: false });
360
 
361
  let attemptCount = 0;
362
+ const nextAttempt = (passNo, codeWindow, curHygiene, sampled) => {
 
 
 
 
 
363
  attemptCount += 1;
364
+ return runWindowAttempt({
365
+ runner,
366
  weights,
367
  attemptNo: attemptCount,
368
  passNo,
369
+ codeWindow,
370
+ curHygiene,
371
+ originalFullContent: content,
372
  filename,
373
  threshold,
374
  sampled,
 
377
  });
378
  };
379
 
380
+ let acceptedList = [];
381
+ let passesUsed = 1;
382
+ let rejectReason = null;
383
+ let totalTokens = 0;
 
 
384
 
385
+ if (shouldChunkFile(content)) {
386
+ const chunked = await runChunkedLoop({
387
+ content,
388
+ before,
389
+ filename,
390
+ threshold,
391
+ maxPasses,
392
+ weights,
393
+ nextAttempt,
394
+ });
395
+ if (chunked.cancelled) return;
396
+ ({ acceptedList, passesUsed, rejectReason, totalTokens } = chunked);
397
+ } else {
398
+ const win1 = selectCodeWindow(content, before, weights, threshold);
399
+ const first = await nextAttempt(1, win1, before, false);
400
+ if (first.cancelled) return;
401
+ totalTokens += first.tokens;
402
+ if (first.verdict.accepted) {
403
+ acceptedList.push(first.verdict);
404
+ }
405
+ if (maxPasses >= 2) {
406
+ if (first.verdict.accepted && first.verdict.squigglesAfter > 0) {
407
+ passesUsed = 2;
408
+ const win2 = selectCodeWindow(first.verdict.content, first.verdict.hygiene, weights, threshold);
409
+ const second = await nextAttempt(2, win2, first.verdict.hygiene, false);
410
+ if (second.cancelled) return;
411
+ totalTokens += second.tokens;
412
+ if (second.verdict.accepted) acceptedList.push(second.verdict);
413
+ } else if (!first.verdict.accepted) {
414
+ passesUsed = 2;
415
+ const retries = [];
416
+ for (let r = 0; r < 2; r += 1) {
417
+ const res = await nextAttempt(2, win1, before, true);
418
+ if (res.cancelled) return;
419
+ totalTokens += res.tokens;
420
+ retries.push(res.verdict);
421
+ }
422
+ const valid = retries.filter((v) => v.accepted);
423
+ if (valid.length > 0) {
424
+ valid.sort((a, b) => {
425
+ const sqDiff = a.squigglesAfter - b.squigglesAfter;
426
+ return sqDiff !== 0 ? sqDiff : (b.scoreAfter ?? 0) - (a.scoreAfter ?? 0);
427
+ });
428
+ acceptedList.push(valid[0]);
429
+ } else {
430
+ rejectReason = retries[retries.length - 1].reason;
431
+ }
432
  }
433
  }
434
+ if (acceptedList.length === 0 && rejectReason === null) {
435
+ rejectReason = first.verdict.reason;
436
+ }
437
  }
438
 
439
  const finalVerdict = acceptedList.length > 0 ? acceptedList[acceptedList.length - 1] : null;
 
456
  let queueChain = Promise.resolve();
457
 
458
  globalThis.addEventListener("message", (messageEvent) => {
459
+ const { type, id, body, options } = messageEvent.data;
460
  if (type === "cancel") {
461
+ const ctrl = cancelledIds.get(id);
462
+ if (ctrl) ctrl.abort();
463
+ else cancelledIds.set(id, { signal: { aborted: true }, abort() {} });
464
  return;
465
  }
466
  if (type === "fix") {
467
  queueChain = queueChain.then(async () => {
468
+ const preCancelled = cancelledIds.get(id);
469
+ if (preCancelled?.signal?.aborted) {
470
  cancelledIds.delete(id);
471
  globalThis.postMessage({ id, done: true });
472
  return;
473
  }
474
  try {
475
+ await runFixJob(id, body, options ?? {});
476
  globalThis.postMessage({ id, done: true });
477
  } catch (err) {
478
  const message = err && err.message ? err.message : String(err);
479
+ globalThis.postMessage({ id, fallback: true, error: message });
 
480
  } finally {
481
  cancelledIds.delete(id);
482
  }
web/src/browser-refiner.js CHANGED
@@ -1,4 +1,4 @@
1
- // Browser-first Bonsai 1.7B WebGPU cleanup with automatic Hugging Face ZeroGPU fallback.
2
  import { probeWebGpuSupport } from "./bonsai-webgpu.js";
3
  import { streamFix } from "./refiner.js";
4
 
@@ -6,8 +6,8 @@ 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;
10
  let serverBackend = "zerogpu";
 
11
  let nextRequestId = 1;
12
  const pendingJobs = new Map();
13
 
@@ -15,62 +15,40 @@ export function setServerBackend(backend) {
15
  serverBackend = backend || "zerogpu";
16
  }
17
 
18
- function configuredMode() {
19
  try {
20
- const param = new URLSearchParams(globalThis.location?.search ?? "").get("refiner");
21
- return param ? param.toLowerCase() : "auto";
22
- } catch {
23
- return "auto";
24
- }
25
- }
26
-
27
- function configuredModelBaseUrl() {
28
- try {
29
- const override = globalThis.__CODEHYGIENE_BONSAI_URL;
30
- if (override) return override;
31
- return new URLSearchParams(globalThis.location?.search ?? "").get("bonsai_url") || "";
32
  } catch {
33
  return "";
34
  }
35
  }
36
 
37
- function canTryBrowserModel() {
38
- const mode = configuredMode();
39
- if (mode === "zerogpu" || mode === "server") {
40
- return Promise.resolve(false);
41
- }
42
- if (mode === "auto" && serverBackend === "mock") {
43
- return Promise.resolve(false);
44
- }
45
- if (!webGpuAvailablePromise) {
46
- webGpuAvailablePromise = probeWebGpuSupport();
47
- }
48
- return webGpuAvailablePromise;
49
  }
50
 
51
  function getBonsaiWorker() {
52
  if (!sharedWorker) {
53
  sharedWorker = new Worker(BONSAI_WORKER_URL, { type: "module" });
54
  sharedWorker.addEventListener("message", (event) => {
55
- const { id, event: eventName, data, done, fallback, disableWebGpu, error } = event.data;
56
  const job = pendingJobs.get(id);
57
  if (!job) return;
58
  if (eventName) {
59
- job.emittedTerminal ||= eventName === "done";
60
  job.onEvent(eventName, data);
61
  return;
62
  }
63
  pendingJobs.delete(id);
64
  if (fallback) {
65
- job.resolve({ fallback: true, disableWebGpu: disableWebGpu !== false, error });
66
  } else if (done) {
67
- job.resolve({ fallback: false });
68
  }
69
  });
70
  sharedWorker.addEventListener("error", () => {
71
  for (const [id, job] of pendingJobs.entries()) {
72
  pendingJobs.delete(id);
73
- job.resolve({ fallback: true, error: "worker_crash" });
74
  }
75
  sharedWorker = null;
76
  });
@@ -78,47 +56,74 @@ function getBonsaiWorker() {
78
  return sharedWorker;
79
  }
80
 
81
- function runWorkerSession(body, { signal, onEvent }) {
82
  return new Promise((resolve) => {
83
  const id = nextRequestId;
84
  nextRequestId += 1;
85
  const worker = getBonsaiWorker();
86
- const job = { onEvent, emittedTerminal: false, resolve };
87
- pendingJobs.set(id, job);
88
 
89
  const onAbort = () => {
90
  pendingJobs.delete(id);
91
  worker.postMessage({ type: "cancel", id });
92
- resolve({ fallback: false, aborted: true });
93
  };
94
  if (signal?.aborted) {
95
  onAbort();
96
  return;
97
  }
98
  signal?.addEventListener("abort", onAbort, { once: true });
99
- worker.postMessage({
100
- type: "fix",
101
- id,
102
- body,
103
- modelBaseUrl: configuredModelBaseUrl(),
104
- forceBrowser: configuredMode() === "browser",
105
- });
106
  });
107
  }
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  export async function streamCleanup(body, { signal, onEvent }) {
110
- if (await canTryBrowserModel()) {
111
- const outcome = await runWorkerSession(body, { signal, onEvent });
112
- if (!outcome.fallback || signal?.aborted) {
113
- return;
114
- }
115
- if (outcome.disableWebGpu) {
116
- webGpuAvailablePromise = Promise.resolve(false);
117
  }
118
- if (configuredMode() === "browser") {
119
- onEvent("error", { code: "webgpu_failed", message: outcome.error || "WebGPU failed" });
 
 
 
 
 
 
 
 
 
 
120
  return;
121
  }
 
 
 
 
 
 
 
122
  }
123
- await streamFix(body, { signal, onEvent });
124
  }
 
1
+ // ZeroGPU-first cleanup router with per-visitor CORS ZeroGPU fallback (and opt-in ?refiner=browser).
2
  import { probeWebGpuSupport } from "./bonsai-webgpu.js";
3
  import { streamFix } from "./refiner.js";
4
 
 
6
  const BONSAI_WORKER_URL = new URL(`./bonsai-worker.js${ASSET_QUERY}`, import.meta.url);
7
 
8
  let sharedWorker = null;
 
9
  let serverBackend = "zerogpu";
10
+ let serverQuotaExhausted = false;
11
  let nextRequestId = 1;
12
  const pendingJobs = new Map();
13
 
 
15
  serverBackend = backend || "zerogpu";
16
  }
17
 
18
+ function queryParam(name) {
19
  try {
20
+ return new URLSearchParams(globalThis.location?.search ?? "").get(name) || "";
 
 
 
 
 
 
 
 
 
 
 
21
  } catch {
22
  return "";
23
  }
24
  }
25
 
26
+ function configuredMode() {
27
+ return queryParam("refiner").toLowerCase() || "zerogpu";
 
 
 
 
 
 
 
 
 
 
28
  }
29
 
30
  function getBonsaiWorker() {
31
  if (!sharedWorker) {
32
  sharedWorker = new Worker(BONSAI_WORKER_URL, { type: "module" });
33
  sharedWorker.addEventListener("message", (event) => {
34
+ const { id, event: eventName, data, done, fallback, error } = event.data;
35
  const job = pendingJobs.get(id);
36
  if (!job) return;
37
  if (eventName) {
 
38
  job.onEvent(eventName, data);
39
  return;
40
  }
41
  pendingJobs.delete(id);
42
  if (fallback) {
43
+ job.resolve({ ok: false, error });
44
  } else if (done) {
45
+ job.resolve({ ok: true });
46
  }
47
  });
48
  sharedWorker.addEventListener("error", () => {
49
  for (const [id, job] of pendingJobs.entries()) {
50
  pendingJobs.delete(id);
51
+ job.resolve({ ok: false, error: "worker_crash" });
52
  }
53
  sharedWorker = null;
54
  });
 
56
  return sharedWorker;
57
  }
58
 
59
+ function runWorkerCleanup(body, options, { signal, onEvent }) {
60
  return new Promise((resolve) => {
61
  const id = nextRequestId;
62
  nextRequestId += 1;
63
  const worker = getBonsaiWorker();
64
+ pendingJobs.set(id, { onEvent, resolve });
 
65
 
66
  const onAbort = () => {
67
  pendingJobs.delete(id);
68
  worker.postMessage({ type: "cancel", id });
69
+ resolve({ ok: true, aborted: true });
70
  };
71
  if (signal?.aborted) {
72
  onAbort();
73
  return;
74
  }
75
  signal?.addEventListener("abort", onAbort, { once: true });
76
+ worker.postMessage({ type: "fix", id, body, options });
 
 
 
 
 
 
77
  });
78
  }
79
 
80
+ async function tryServerZeroGpu(body, { signal, onEvent }) {
81
+ let unavailable = false;
82
+ let acceptedAny = false;
83
+ await streamFix(body, {
84
+ signal,
85
+ onEvent: (name, data) => {
86
+ if (name === "verdict" && data?.accepted) {
87
+ acceptedAny = true;
88
+ }
89
+ if (name === "error" && data?.code === "model_unavailable" && !acceptedAny) {
90
+ unavailable = true;
91
+ return;
92
+ }
93
+ onEvent(name, data);
94
+ },
95
+ });
96
+ return { unavailable };
97
+ }
98
+
99
  export async function streamCleanup(body, { signal, onEvent }) {
100
+ const mode = configuredMode();
101
+ if (mode === "browser") {
102
+ if (await probeWebGpuSupport()) {
103
+ const modelBaseUrl = globalThis.__CODEHYGIENE_BONSAI_URL || queryParam("bonsai_url");
104
+ const res = await runWorkerCleanup(body, { backend: "webgpu", modelBaseUrl }, { signal, onEvent });
105
+ if (res.ok || signal?.aborted) return;
 
106
  }
107
+ onEvent("error", { code: "webgpu_failed", message: "WebGPU unavailable" });
108
+ return;
109
+ }
110
+
111
+ if (serverBackend === "mock" || mode === "server") {
112
+ await streamFix(body, { signal, onEvent });
113
+ return;
114
+ }
115
+
116
+ if (mode !== "direct" && !serverQuotaExhausted) {
117
+ const { unavailable } = await tryServerZeroGpu(body, { signal, onEvent });
118
+ if (!unavailable || signal?.aborted) {
119
  return;
120
  }
121
+ serverQuotaExhausted = true;
122
+ }
123
+
124
+ const zeroGpuUrl = queryParam("zerogpu_url");
125
+ const direct = await runWorkerCleanup(body, { backend: "zerogpu", zeroGpuUrl }, { signal, onEvent });
126
+ if (!direct.ok && !signal?.aborted) {
127
+ onEvent("error", { code: "model_unavailable", message: direct.error || "ZeroGPU unavailable" });
128
  }
 
129
  }
web/src/privacy.html CHANGED
@@ -51,7 +51,7 @@
51
  </div>
52
  <div class="fact">
53
  <dt>Cleanup</dt>
54
- <dd>A small model in your browser does the work, so the code stays on your device. If your device can't run it, a GPU on Hugging Face (ZeroGPU) does, holding the file in memory only. Public repo cleanups may be reused from memory until a restart.</dd>
55
  </div>
56
  <div class="fact">
57
  <dt>Pull requests</dt>
 
51
  </div>
52
  <div class="fact">
53
  <dt>Cleanup</dt>
54
+ <dd>A stateless GPU on Hugging Face (ZeroGPU) rewrites flagged sections in memory only and discards the code when done. Public repo cleanups may be reused from memory until a restart.</dd>
55
  </div>
56
  <div class="fact">
57
  <dt>Pull requests</dt>