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

Fix worker cancellation Map, buffer ZeroGPU failover events, and support ZEROGPU_HF_TOKEN

Browse files
server/backends.py CHANGED
@@ -213,7 +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._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
 
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.zerogpu_hf_token
217
  self._n_ctx = settings.n_ctx
218
  self._max_new_tokens = settings.max_new_tokens
219
  self._transport = transport
server/config.py CHANGED
@@ -57,6 +57,7 @@ class Settings:
57
  stats_path: Path = Path("/tmp/codehygiene_stats.json")
58
  stats_dataset_repo: str = ""
59
  hf_token: str = field(default="", repr=False)
 
60
  stats_sync_seconds: float = 300.0
61
  allow_mock_telemetry: bool = False
62
 
@@ -136,6 +137,7 @@ def load_settings(env: dict[str, str] | None = None) -> Settings:
136
  stats_path=Path(_env_text(env, "STATS_PATH") or defaults.stats_path),
137
  stats_dataset_repo=_env_text(env, "HF_STATS_DATASET_REPO"),
138
  hf_token=_env_text(env, "HF_TOKEN"),
 
139
  stats_sync_seconds=_env_float(env, "STATS_SYNC_SECONDS", defaults.stats_sync_seconds),
140
  allow_mock_telemetry=_env_flag(env, "ALLOW_MOCK_TELEMETRY", False),
141
  github_app_id=_env_text(env, "GITHUB_APP_ID"),
 
57
  stats_path: Path = Path("/tmp/codehygiene_stats.json")
58
  stats_dataset_repo: str = ""
59
  hf_token: str = field(default="", repr=False)
60
+ zerogpu_hf_token: str = field(default="", repr=False)
61
  stats_sync_seconds: float = 300.0
62
  allow_mock_telemetry: bool = False
63
 
 
137
  stats_path=Path(_env_text(env, "STATS_PATH") or defaults.stats_path),
138
  stats_dataset_repo=_env_text(env, "HF_STATS_DATASET_REPO"),
139
  hf_token=_env_text(env, "HF_TOKEN"),
140
+ zerogpu_hf_token=_env_text(env, "ZEROGPU_HF_TOKEN"),
141
  stats_sync_seconds=_env_float(env, "STATS_SYNC_SECONDS", defaults.stats_sync_seconds),
142
  allow_mock_telemetry=_env_flag(env, "ALLOW_MOCK_TELEMETRY", False),
143
  github_app_id=_env_text(env, "GITHUB_APP_ID"),
web/src/bonsai-worker.js CHANGED
@@ -23,7 +23,7 @@ const ASSET_QUERY = new URL(import.meta.url).search;
23
  const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href);
24
  let enginePromise = null;
25
  let loadedBaseUrl = null;
26
- const cancelledIds = new Set();
27
 
28
  function getWebGpuEngine(baseUrl, onLoadProgress) {
29
  const resolvedBase = baseUrl || DEFAULT_MODEL_BASE;
 
23
  const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href);
24
  let enginePromise = null;
25
  let loadedBaseUrl = null;
26
+ const cancelledIds = new Map();
27
 
28
  function getWebGpuEngine(baseUrl, onLoadProgress) {
29
  const resolvedBase = baseUrl || DEFAULT_MODEL_BASE;
web/src/browser-refiner.js CHANGED
@@ -27,6 +27,10 @@ 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" });
@@ -78,22 +82,40 @@ function runWorkerCleanup(body, options, { signal, onEvent }) {
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 }) {
@@ -114,8 +136,8 @@ export async function streamCleanup(body, { signal, onEvent }) {
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;
@@ -124,6 +146,8 @@ export async function streamCleanup(body, { signal, onEvent }) {
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
  }
 
27
  return queryParam("refiner").toLowerCase() || "zerogpu";
28
  }
29
 
30
+ function isQuotaErrorMessage(text) {
31
+ return /zerogpu runs limit|quota exceeded/i.test(String(text || ""));
32
+ }
33
+
34
  function getBonsaiWorker() {
35
  if (!sharedWorker) {
36
  sharedWorker = new Worker(BONSAI_WORKER_URL, { type: "module" });
 
82
  }
83
 
84
  async function tryServerZeroGpu(body, { signal, onEvent }) {
85
+ const buffered = [];
86
+ let committed = false;
87
  let unavailable = false;
88
+ let errorMessage = "";
89
+
90
+ const flushBuffered = () => {
91
+ if (committed) return;
92
+ committed = true;
93
+ while (buffered.length > 0) {
94
+ const [evName, evData] = buffered.shift();
95
+ onEvent(evName, evData);
96
+ }
97
+ };
98
+
99
  await streamFix(body, {
100
  signal,
101
  onEvent: (name, data) => {
102
+ if (!committed && (name === "start" || name === "attempt" || name === "progress" || name === "queue")) {
103
+ buffered.push([name, data]);
104
+ return;
105
  }
106
+ if (!committed && name === "error" && data?.code === "model_unavailable") {
107
  unavailable = true;
108
+ errorMessage = data?.message || "";
109
  return;
110
  }
111
+ flushBuffered();
112
  onEvent(name, data);
113
  },
114
  });
115
+ if (!committed && !unavailable && buffered.length > 0) {
116
+ flushBuffered();
117
+ }
118
+ return { unavailable, errorMessage };
119
  }
120
 
121
  export async function streamCleanup(body, { signal, onEvent }) {
 
136
  }
137
 
138
  if (mode !== "direct" && !serverQuotaExhausted) {
139
+ const serverRes = await tryServerZeroGpu(body, { signal, onEvent });
140
+ if (!serverRes.unavailable || signal?.aborted) {
141
  return;
142
  }
143
  serverQuotaExhausted = true;
 
146
  const zeroGpuUrl = queryParam("zerogpu_url");
147
  const direct = await runWorkerCleanup(body, { backend: "zerogpu", zeroGpuUrl }, { signal, onEvent });
148
  if (!direct.ok && !signal?.aborted) {
149
+ const msg = direct.error || "ZeroGPU unavailable";
150
+ const code = isQuotaErrorMessage(msg) ? "quota_exceeded" : "model_unavailable";
151
+ onEvent("error", { code, message: msg });
152
  }
153
  }
web/src/fix-run.js CHANGED
@@ -163,6 +163,10 @@ FixSession.prototype.handlers = {
163
  });
164
  },
165
  error(data) {
 
 
 
 
166
  this.settle({ kind: data?.code === "model_unavailable" ? "unreachable" : "rejected" });
167
  },
168
  };
 
163
  });
164
  },
165
  error(data) {
166
+ if (data?.code === "quota_exceeded") {
167
+ this.settle({ kind: "quota_exceeded" });
168
+ return;
169
+ }
170
  this.settle({ kind: data?.code === "model_unavailable" ? "unreachable" : "rejected" });
171
  },
172
  };
web/src/labels.js CHANGED
@@ -82,6 +82,7 @@ export const STATUS = {
82
  cleaned: "Cleaned",
83
  skipped: "Skipped",
84
  offline: "Offline",
 
85
  tooLong: "Too long",
86
  filesTooLong: "Files too long",
87
  noIssues: "No issues found",
 
82
  cleaned: "Cleaned",
83
  skipped: "Skipped",
84
  offline: "Offline",
85
+ quotaExceeded: "GPU quota reached",
86
  tooLong: "Too long",
87
  filesTooLong: "Files too long",
88
  noIssues: "No issues found",
web/src/main.js CHANGED
@@ -364,6 +364,17 @@ async function finishSession(session) {
364
  }
365
  return;
366
  }
 
 
 
 
 
 
 
 
 
 
 
367
  if (outcome.kind === "unreachable") {
368
  if (app.shown === file) {
369
  renderShown();
 
364
  }
365
  return;
366
  }
367
+ if (outcome.kind === "quota_exceeded") {
368
+ app.activeFile = null;
369
+ app.idle = true;
370
+ app.queue = [];
371
+ if (app.shown === file) {
372
+ renderShown();
373
+ stage.setStatus(STATUS.quotaExceeded);
374
+ }
375
+ paintNav(app.shown);
376
+ return;
377
+ }
378
  if (outcome.kind === "unreachable") {
379
  if (app.shown === file) {
380
  renderShown();