Files
antigravity-usage-monitor/sidecars/panel/backend/sidecar-worker.mjs
T
fish 173e21c607 fix(panel): harden SQLite ledger concurrency, LRU sizing, and recovery
- Preserve step detail_json and peer-appended rows during cold metadata flushes, slim-step spreads, and mixed-case UUID re-keying without triggering case-insensitive self-deletion
- Raise default hot session LRU limit to 64 and prime stepSigs during on-demand hydration to avoid cyclic read thrashing across 30-session quick stats plus active subagents
- Quarantine corrupt SQLite images to .corrupt.<ts> with automatic .json.bak recovery and defer latching loadedOnce until initial hydration succeeds
- Flush pending throttled endpoint caches on worker/standalone stop, isolate unscoped node --test runs from live user data, and transitively retain nested subagents and side questions on cache rebuilds
2026-11-24 15:04:14 +09:00

110 lines
3.1 KiB
JavaScript

/**
* @fileoverview Hot-reloadable backend Worker runtime (`sidecar-worker.mjs`) for `main.mjs`.
*
* Isolates all ESM backend modules (`api-actions.mjs`, `telemetry-builder.mjs`, `window-bridge.mjs`,
* `pricing-catalog.mjs`, etc.) inside a dedicated V8 Worker isolate so plugin updates (in-app Git/ZIP
* or external file overwrite while Antigravity is open) can tear down and respawn a fresh Worker
* without ever exiting the parent `SidecarApp` process or losing its host-assigned HTTP port.
*/
import { parentPort } from 'node:worker_threads';
import {
bootBundleVersion,
bootWindowBridgeRuntime,
dispatchBridgeApiRequest,
} from './api-actions.mjs';
import { computeRuntimeCodeVersion } from './config.mjs';
import { flushAllEndpointCachesNow } from './endpoint-registry.mjs';
let shuttingDown = false;
let reloadRequested = false;
function requestWorkerReload() {
if (shuttingDown || reloadRequested) return;
reloadRequested = true;
try {
parentPort?.postMessage({ type: 'reload_request' });
} catch {
// Ignore postMessage errors if parent is already closing
}
}
const bridgeController = bootWindowBridgeRuntime({
allowDynamicLs: true,
onRestart: requestWorkerReload,
});
const driftWatchTimer = setInterval(() => {
if (shuttingDown || reloadRequested) return;
try {
const diskVersion = String(computeRuntimeCodeVersion() || '');
if (bootBundleVersion && diskVersion && diskVersion !== String(bootBundleVersion)) {
requestWorkerReload();
}
} catch {
// Ignore transient file stat errors while files are being overwritten
}
}, 3000);
driftWatchTimer.unref?.();
async function gracefulStop() {
if (shuttingDown) return;
shuttingDown = true;
clearInterval(driftWatchTimer);
try {
flushAllEndpointCachesNow();
} catch {
// Ignore cache flush errors
}
try {
await bridgeController?.stop?.({ disposeWindows: false });
} catch {
// Ignore shutdown errors
}
}
parentPort?.on('message', async (msg) => {
if (!msg || typeof msg !== 'object') return;
if (msg.type === 'shutdown') {
await gracefulStop();
try {
parentPort?.postMessage({ type: 'shutdown_ack', id: msg.id });
} catch {
// Ignore closed channel
}
return;
}
if (msg.type === 'api') {
const reqId = msg.id;
try {
const out = await dispatchBridgeApiRequest(
String(msg.pathname || ''),
String(msg.method || 'GET'),
msg.data,
{ onRestart: requestWorkerReload }
);
parentPort?.postMessage({
type: 'api_result',
id: reqId,
ok: true,
response: {
status: out?.status || 200,
contentType: out?.contentType || 'application/json',
headers: out?.headers || { 'Cache-Control': 'no-store' },
body: typeof out?.body === 'string' ? out.body : JSON.stringify(out?.body ?? {}),
},
});
} catch (err) {
parentPort?.postMessage({
type: 'api_result',
id: reqId,
ok: false,
error: err?.message || 'Worker API execution failed',
});
}
}
});
parentPort?.postMessage({ type: 'ready', bundleVersion: bootBundleVersion });