Files
BountyBoard/web/static/js/ws.js
T
etalon f87b954f27 feat: atomization pipeline with job queue, atomizer client, WS hub, and board UI (phase 7)
- internal/extsvc: circuit breaker (§11.16) + retrying bearer JSON client
- atomizer client: §5.1 contract incl. defensive coefficient normalization
  and extension coefficient range (0,2]
- persisted jobs collection: atomic claim, panic recovery, exponential
  backoff (max 3), stale-running requeue, shutdown-safe bookkeeping
- subdivide (async 202, atomizing status, re-run replaces unpublished
  children after confirm) and extend (sibling task, source budget)
- failure path reverts status and notifies the consultant on final attempt
- PATCH task editing with bounty recompute, budget cascade to descendants
- publish single + bulk; task detail endpoint role-scoped
- coder/websocket hub: multiplexed channels, origin check, 30s heartbeats;
  client ws.js with reconnect + 15s polling fallback
- consultant atomization board: tree by root, coefficient sliders with live
  per-parent sum indicator, bounty preview, modals, shimmer, atomizer-down
  gating via /api/v1/service-health
- fix: tasks external-ref unique index is partial, not sparse (compound
  sparse indexed every task through customerId and broke child inserts)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 19:17:09 +02:00

58 lines
1.9 KiB
JavaScript

// Live updates: one multiplexed WebSocket with auto-reconnect and a 15 s
// polling fallback signal (§2.2). Subscribers register per channel.
const subs = new Map(); // channel -> Set<fn>
let socket = null;
let attempts = 0;
let fallbackPolling = false;
const pollListeners = new Set();
export function subscribe(channel, fn) {
if (!subs.has(channel)) subs.set(channel, new Set());
subs.get(channel).add(fn);
ensureSocket();
return () => subs.get(channel).delete(fn);
}
// onPollFallback registers a callback invoked every 15 s while the socket is
// down, so pages can refresh their data without WS.
export function onPollFallback(fn) {
pollListeners.add(fn);
}
export function send(channel, event, data) {
if (socket && socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ channel, event, data }));
}
}
let pollTimer = null;
function startPolling() {
if (fallbackPolling) return;
fallbackPolling = true;
pollTimer = setInterval(() => pollListeners.forEach((fn) => fn()), 15000);
}
function stopPolling() {
fallbackPolling = false;
if (pollTimer) clearInterval(pollTimer);
}
function ensureSocket() {
if (socket && socket.readyState <= WebSocket.OPEN) return;
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
socket = new WebSocket(`${proto}//${window.location.host}/ws`);
socket.addEventListener('open', () => { attempts = 0; stopPolling(); });
socket.addEventListener('message', (e) => {
try {
const msg = JSON.parse(e.data);
(subs.get(msg.channel) || []).forEach((fn) => fn(msg.event, msg.data));
} catch (err) { /* ignore malformed frames */ }
});
socket.addEventListener('close', () => {
startPolling();
attempts += 1;
const delay = Math.min(30000, 1000 * 2 ** attempts);
setTimeout(ensureSocket, delay);
});
socket.addEventListener('error', () => socket.close());
}