style: format server and shared with prettier

A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
This commit is contained in:
Maurice
2026-10-10 21:23:37 +02:00
parent f4e518b2df
commit 724c88c201
1978 changed files with 78448 additions and 39347 deletions
@@ -1,16 +1,30 @@
import fs from 'node:fs';
import { randomUUID } from 'node:crypto';
import { fork, type ChildProcess } from 'node:child_process';
import type { EntityManager } from '@mikro-orm/core';
import { readEnv } from '../../../app-config';
import { resolveChildEntry, pluginCodeDir, pluginRealCodeDir, pluginPermissionArgs, ensurePluginModuleType } from '../paths';
import { HOOK_PERMISSION, USER_DATA_PERMISSION, EVENTS_PERMISSION, type Envelope, type RpcError, type RpcRequest } from '../protocol/envelope';
import type { PluginRpcHost } from '../host/rpc-host';
import { scheduleJobs, stopJobs, type ScheduledJob } from '../host/plugin-jobs';
import { SNAPSHOT_GRANT, type PluginEventMeta } from '../../../plugin-event-sink';
import { RpcRateLimiter, DEFAULT_RPC_LIMIT, TokenBucket, DEFAULT_LOG_LIMIT } from '../host/rate-limit';
import { withRequestContext } from '../../database/request-context';
import { traceEntry } from '../../audit/entry-trace.logger';
import { withRequestContext } from '../../database/request-context';
import { scheduleJobs, stopJobs, type ScheduledJob } from '../host/plugin-jobs';
import { RpcRateLimiter, DEFAULT_RPC_LIMIT, TokenBucket, DEFAULT_LOG_LIMIT } from '../host/rate-limit';
import type { PluginRpcHost } from '../host/rpc-host';
import {
resolveChildEntry,
pluginCodeDir,
pluginRealCodeDir,
pluginPermissionArgs,
ensurePluginModuleType,
} from '../paths';
import {
HOOK_PERMISSION,
USER_DATA_PERMISSION,
EVENTS_PERMISSION,
type Envelope,
type RpcError,
type RpcRequest,
} from '../protocol/envelope';
import type { EntityManager } from '@mikro-orm/core';
import { fork, type ChildProcess } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import fs from 'node:fs';
export interface PluginRouteInfo {
i: number;
@@ -124,8 +138,11 @@ export class PluginSupervisor {
// (no persistence, so no DB writes on the broadcast fast-path), bounded per plugin
// and TTL'd. Grants + snapshot gating are re-evaluated at replay time from the
// CURRENT grant set, never trusted from when the event was buffered.
private readonly pendingEvents = new Map<string, Array<{ tripId: number; event: string; meta?: PluginEventMeta; expiresAt: number }>>();
private static readonly EVENT_BUFFER_MAX = 200; // events held per plugin (drop oldest past this)
private readonly pendingEvents = new Map<
string,
Array<{ tripId: number; event: string; meta?: PluginEventMeta; expiresAt: number }>
>();
private static readonly EVENT_BUFFER_MAX = 200; // events held per plugin (drop oldest past this)
private static readonly EVENT_BUFFER_TTL_MS = 15 * 60_000; // a buffered event older than this is dropped unreplayed
constructor(
@@ -151,7 +168,12 @@ export class PluginSupervisor {
}
/** Spawn a plugin and resolve once it reports `loaded` (or reject on load error). */
activate(id: string, granted: ReadonlySet<string>, config: Record<string, unknown> = {}, egress: string[] = []): Promise<void> {
activate(
id: string,
granted: ReadonlySet<string>,
config: Record<string, unknown> = {},
egress: string[] = [],
): Promise<void> {
const existing = this.running.get(id);
if (existing) {
// A live plugin (starting/active) is idempotent — already activated.
@@ -314,7 +336,8 @@ export class PluginSupervisor {
subscribersOf(sourceId: string, event: string): string[] {
const out: string[] = [];
for (const [id, sup] of this.running) {
if (sup.status === 'active' && sup.subscriptions.some((s) => s.plugin === sourceId && s.event === event)) out.push(id);
if (sup.status === 'active' && sup.subscriptions.some((s) => s.plugin === sourceId && s.event === event))
out.push(id);
}
return out;
}
@@ -350,7 +373,12 @@ export class PluginSupervisor {
const { snapshot, ...hint } = meta ?? {};
const grant = hint.entity ? SNAPSHOT_GRANT[hint.entity] : undefined;
const withSnapshot = snapshot !== undefined && grant !== undefined && sup.granted.has(grant);
this.invoke(sup.id, 'invoke.event', { event, tripId, ...hint, ...(withSnapshot ? { snapshot } : {}) }, { actingUserId: undefined, timeoutMs: 5000 }).catch(() => {
this.invoke(
sup.id,
'invoke.event',
{ event, tripId, ...hint, ...(withSnapshot ? { snapshot } : {}) },
{ actingUserId: undefined, timeoutMs: 5000 },
).catch(() => {
/* a subscriber that errors or times out is ignored — events are best-effort */
});
}
@@ -358,7 +386,10 @@ export class PluginSupervisor {
/** Append an event to a subscriber's bounded redelivery buffer (drop-oldest past the cap). */
private bufferEvent(id: string, tripId: number, event: string, meta?: PluginEventMeta): void {
let q = this.pendingEvents.get(id);
if (!q) { q = []; this.pendingEvents.set(id, q); }
if (!q) {
q = [];
this.pendingEvents.set(id, q);
}
q.push({ tripId, event, meta, expiresAt: Date.now() + PluginSupervisor.EVENT_BUFFER_TTL_MS });
if (q.length > PluginSupervisor.EVENT_BUFFER_MAX) q.splice(0, q.length - PluginSupervisor.EVENT_BUFFER_MAX);
}
@@ -388,9 +419,11 @@ export class PluginSupervisor {
deliverScheduled(id: string, name: string, payload: unknown): void {
const sup = this.running.get(id);
if (!sup || sup.status !== 'active') return;
void this.invoke(id, 'invoke.scheduled', { name, payload }, { actingUserId: undefined, timeoutMs: 60_000 }).catch(() => {
/* a scheduled task that errors/times out is ignored — best-effort like jobs */
});
void this.invoke(id, 'invoke.scheduled', { name, payload }, { actingUserId: undefined, timeoutMs: 60_000 }).catch(
() => {
/* a scheduled task that errors/times out is ignored — best-effort like jobs */
},
);
}
/**
@@ -420,11 +453,19 @@ export class PluginSupervisor {
* gated by the same hook:user-data grant. Returns the plugin's exported payload,
* or undefined if it is inactive, ungranted, doesn't implement the hook, or errors.
*/
async collectUserExport(id: string, userId: number): Promise<{ ok: true; data: unknown } | { ok: false } | undefined> {
async collectUserExport(
id: string,
userId: number,
): Promise<{ ok: true; data: unknown } | { ok: false } | undefined> {
const sup = this.running.get(id);
if (!sup || sup.status !== 'active' || !sup.granted.has(USER_DATA_PERMISSION)) return undefined; // not applicable
try {
const res = (await this.invoke(id, 'invoke.exportUserData', { userId }, { actingUserId: undefined, timeoutMs: 30_000 })) as { data?: unknown } | undefined;
const res = (await this.invoke(
id,
'invoke.exportUserData',
{ userId },
{ actingUserId: undefined, timeoutMs: 30_000 },
)) as { data?: unknown } | undefined;
return { ok: true, data: res?.data };
} catch {
return { ok: false }; // errored/timed out — the caller flags this as incomplete, not "no data"
@@ -583,7 +624,12 @@ export class PluginSupervisor {
// sweep. A throttled call is refused with HOST_ERROR (retryable) rather than
// executed; a legitimate plugin never hits the generous burst.
if (!sup.rpcLimiter.tryAcquire(Date.now())) {
sup.child?.send({ k: 'res', id: req.id, ok: false, error: { code: 'HOST_ERROR', message: 'rate limit exceeded — slow down ctx.* calls' } } satisfies RpcError);
sup.child?.send({
k: 'res',
id: req.id,
ok: false,
error: { code: 'HOST_ERROR', message: 'rate limit exceeded — slow down ctx.* calls' },
} satisfies RpcError);
return;
}
const inv = req.params as { _inv?: unknown } | undefined;
@@ -617,7 +663,12 @@ export class PluginSupervisor {
// refusal a few lines up, THEN throw for host-side visibility. The
// dispatch still never runs either way — this only changes whether
// the caller ever hears back.
sup.child?.send({ k: 'res', id: req.id, ok: false, error: { code: 'HOST_ERROR', message: 'no ORM available to build a request context' } } satisfies RpcError);
sup.child?.send({
k: 'res',
id: req.id,
ok: false,
error: { code: 'HOST_ERROR', message: 'no ORM available to build a request context' },
} satisfies RpcError);
throw new Error('PluginSupervisor: no ORM available to build a request context for this RPC dispatch');
}
// One unit of work per RPC, with its own correlation id and one log line;
@@ -650,7 +701,11 @@ export class PluginSupervisor {
if (msg.k === 'evt') {
switch (msg.topic) {
case 'hello':
sup.child?.send({ k: 'evt', topic: 'init', data: { config: sup.config, egress: sup.egress } } satisfies Envelope);
sup.child?.send({
k: 'evt',
topic: 'init',
data: { config: sup.config, egress: sup.egress },
} satisfies Envelope);
break;
case 'heartbeat': {
sup.lastBeat = Date.now();
@@ -667,8 +722,13 @@ export class PluginSupervisor {
if (this.running.get(sup.id) !== sup || sup.status !== 'starting') break;
sup.lastBeat = Date.now();
const d = msg.data as {
routes?: PluginRouteInfo[]; jobs?: ScheduledJob[]; hooks?: string[]; hookFns?: unknown; events?: string[];
exports?: string[]; subscriptions?: Array<{ plugin: string; event: string }>;
routes?: PluginRouteInfo[];
jobs?: ScheduledJob[];
hooks?: string[];
hookFns?: unknown;
events?: string[];
exports?: string[];
subscriptions?: Array<{ plugin: string; event: string }>;
mcpTools?: string[];
};
sup.routes = d.routes ?? [];
@@ -681,7 +741,10 @@ export class PluginSupervisor {
sup.exports = Array.isArray(d.exports) ? d.exports.filter((e): e is string => typeof e === 'string') : [];
sup.mcpTools = Array.isArray(d.mcpTools) ? d.mcpTools.filter((t): t is string => typeof t === 'string') : [];
sup.subscriptions = Array.isArray(d.subscriptions)
? d.subscriptions.filter((s): s is { plugin: string; event: string } => !!s && typeof s.plugin === 'string' && typeof s.event === 'string')
? d.subscriptions.filter(
(s): s is { plugin: string; event: string } =>
!!s && typeof s.plugin === 'string' && typeof s.event === 'string',
)
: [];
this.clearActivationTimer(sup);
this.setStatus(sup, 'active');
@@ -690,7 +753,9 @@ export class PluginSupervisor {
// egress. Wrapped so a scheduling hiccup can never break activation.
try {
sup.jobTasks = scheduleJobs(sup.granted, sup.jobs, (jobId) => {
void this.invoke(sup.id, 'invoke.job', { jobId }, { actingUserId: undefined, timeoutMs: 60_000 }).catch(() => {});
void this.invoke(sup.id, 'invoke.job', { jobId }, { actingUserId: undefined, timeoutMs: 60_000 }).catch(
() => {},
);
});
} catch {
/* a scheduler error must never stop a plugin from going live */