* fix(ws): exponential backoff + jitter on WsClient reconnect Replaces the fixed 5-second reconnect delay with an exponential backoff (5s→10s→20s→40s→60s cap, +4s random jitter) so simultaneous browser sessions don't thunder-herd the engine on restart. - Backoff resets only after a stable 5-second connection, not on every successful open, so rapid reconnect cycles don't mask the retry. - Stale-timer bookkeeping added to the cleanup/unsubscribe path. Also bundles two companion fixes discovered during the same session: fix(scripts): BOM-safe JSON parsing in sync/watch-local-plugins (Fix) PowerShell 5.1 emits a UTF-8 BOM that Node's JSON.parse rejects; readJsonFile() strips U+FEFF before parsing, matching pnpm behavior. docs(agents): add UTF-8 BOM troubleshooting entry (Fix) Documents diagnosis, fix, and prevention for the BOM crash. Co-Authored-By: Claude Opus 4.7 <[email protected]> * test(ws): pin Math.random to 0 so reconnect backoff test is deterministic With jitter (Math.random()*4000), advancing timers by exactly 5000ms no longer guaranteed to trigger the reconnect. Mocking Math.random to 0 in beforeEach makes the delay deterministic (base only, no jitter) across the full test suite. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.7 <[email protected]>
97 lines
2.8 KiB
JavaScript
97 lines
2.8 KiB
JavaScript
// scripts/watch-local-plugins.mjs
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { syncAll } from "./sync-local-plugins.mjs";
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const ROOT = path.resolve(__dirname, "..");
|
|
const LOCAL_PLUGINS_DIR = path.join(ROOT, "local-plugins");
|
|
|
|
if (!fs.existsSync(LOCAL_PLUGINS_DIR)) {
|
|
fs.mkdirSync(LOCAL_PLUGINS_DIR, { recursive: true });
|
|
}
|
|
|
|
let debounceTimeout = null;
|
|
let isSyncing = false;
|
|
let pendingSync = false;
|
|
/** Files changed since last sync completed — collected during the debounce window */
|
|
const pendingChanges = new Set();
|
|
|
|
function handleFileChange(eventType, filename) {
|
|
if (!filename) return;
|
|
if (
|
|
filename.includes("dist") ||
|
|
filename.includes("node_modules") ||
|
|
filename.endsWith(".map") ||
|
|
filename.includes(".git")
|
|
) return; // ignore build artifacts and git internals
|
|
|
|
pendingChanges.add(filename);
|
|
|
|
if (debounceTimeout) {
|
|
clearTimeout(debounceTimeout);
|
|
}
|
|
|
|
debounceTimeout = setTimeout(async () => {
|
|
if (isSyncing) {
|
|
pendingSync = true;
|
|
return;
|
|
}
|
|
await runSync();
|
|
}, 500);
|
|
}
|
|
|
|
/**
|
|
* Pulls plugin slugs out of changed file paths so we can name what triggered the rebuild.
|
|
* fs.watch on `local-plugins/` emits paths like `aviation/src/index.ts` (POSIX or Windows).
|
|
*/
|
|
function summarizeChangedPlugins(files) {
|
|
const slugs = new Set();
|
|
for (const f of files) {
|
|
const head = f.split(/[\\/]/)[0];
|
|
if (head) slugs.add(head);
|
|
}
|
|
return [...slugs];
|
|
}
|
|
|
|
async function runSync() {
|
|
isSyncing = true;
|
|
const changed = summarizeChangedPlugins(pendingChanges);
|
|
pendingChanges.clear();
|
|
const label = changed.length === 0
|
|
? "pending changes"
|
|
: changed.length <= 3
|
|
? changed.join(", ")
|
|
: `${changed.slice(0, 3).join(", ")} (+${changed.length - 3} more)`;
|
|
console.log(`\n[watch] 🔄 Change detected in: ${label}`);
|
|
try {
|
|
await syncAll();
|
|
console.log(`[watch] ✨ Hot-reload ready`);
|
|
} catch (err) {
|
|
console.error(`[watch] Sync failed:`, err);
|
|
} finally {
|
|
isSyncing = false;
|
|
if (pendingSync) {
|
|
pendingSync = false;
|
|
await runSync();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Initial sync
|
|
console.log(`[watch] Starting initial sync...`);
|
|
isSyncing = true;
|
|
syncAll().then(() => {
|
|
isSyncing = false;
|
|
if (pendingSync) {
|
|
pendingSync = false;
|
|
runSync("pending changes").catch(console.error);
|
|
}
|
|
console.log(`[watch] Watching ${LOCAL_PLUGINS_DIR} for changes...`);
|
|
fs.watch(LOCAL_PLUGINS_DIR, { recursive: true }, handleFileChange);
|
|
}).catch(err => {
|
|
isSyncing = false;
|
|
console.error(`[watch] Initial sync failed:`, err);
|
|
});
|