Files
fish e520efe84e feat(account): add quota trend chart and multi-account risk gate
- Gate multi-account login, import, switch, backup quota polling, and vault controls behind explicit risk consent (accountSwitchRiskAccepted), restricting unaccepted sessions to active-account quota viewing
- Add collinear-compressed per-account quota history tracking and a full-bleed 184px AccountQuotaTrendCard supporting cycle, preset, all-time, and custom ranges with cycle-boundary pre-reset filtering and isolated impulse spike sanitization
- Unify Account tab controls onto shared SegmentedControl, CustomSelect, CustomDateTimePicker, .ctx-risk-mask, and global status/button primitives while removing bespoke .account-* CSS and balancing card padding
2026-11-25 03:42:33 +09:00

112 lines
3.2 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 { flushAccountQuotaHistoryNow } from './account-quota-history.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 {
flushAccountQuotaHistoryNow();
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 });