diff --git a/server/src/db/repositories/PluginEntityMetadata.repository.ts b/server/src/db/repositories/PluginEntityMetadata.repository.ts index 0932170a2..63e487614 100644 --- a/server/src/db/repositories/PluginEntityMetadata.repository.ts +++ b/server/src/db/repositories/PluginEntityMetadata.repository.ts @@ -90,16 +90,14 @@ export class PluginEntityMetadataRepository extends TrekRepository { return this.getEntityManager().transactional(async () => { diff --git a/server/src/nest/plugins/host/rpc/host-surface.rpc.ts b/server/src/nest/plugins/host/rpc/host-surface.rpc.ts index 6ea195144..9a8eb07e9 100644 --- a/server/src/nest/plugins/host/rpc/host-surface.rpc.ts +++ b/server/src/nest/plugins/host/rpc/host-surface.rpc.ts @@ -11,6 +11,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors'; import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators'; import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types'; import { asPayload, num, str } from '../../../../nest-rpc/rpc-params'; +import { UnitOfWork } from '../../../database/unit-of-work'; import { UnreadableLlmResponse } from '../../../llm-parse/clients/openai-compatible.client'; import { createLlmClient } from '../../../llm-parse/llm-client.factory'; import type { ResolvedLlmConfig } from '../../../llm-parse/llm-config'; @@ -58,6 +59,7 @@ export class HostSurfaceRpc { @InjectRepository(Trips) private readonly trips: TripsRepository, // HR5–HR8 (Plan 3j Task 5) — a plugin's own scheduler.set/scheduler.cancel RPCs. @InjectRepository(PluginScheduledTasks) private readonly scheduledTasks: PluginScheduledTasksRepository, + private readonly uow: UnitOfWork, ) {} @PluginMethod('users.getById', { permission: 'db:read:users' }) @@ -212,11 +214,16 @@ export class HostSurfaceRpc { throw new BadParams(`scheduler payload too large (max ${SCHED_PAYLOAD_MAX} bytes)`); // HR5/HR6/HR7 — Plan 3j Task 7 fix (must-land 3): one atomic call, not // three separately-awaited ones (task-7-review.md's concurrent-cap-bypass). - // Upsert by (plugin, name): re-scheduling the same name replaces it. - const written = await this.scheduledTasks.upsertTaskCapped( - { plugin_id: ctx.pluginId, name, due_at: Math.max(dueAt, Date.now()), payload: json, every_ms: everyMs ?? null }, - SCHED_MAX, - ); + // Upsert by (plugin, name): re-scheduling the same name replaces it. In a + // unit of work, whose write lock keeps the cap check atomic on Postgres too. + const task = { + plugin_id: ctx.pluginId, + name, + due_at: Math.max(dueAt, Date.now()), + payload: json, + every_ms: everyMs ?? null, + }; + const written = await this.uow.transactional(() => this.scheduledTasks.upsertTaskCapped(task, SCHED_MAX)); if (!written) throw new BadParams(`too many scheduled tasks (max ${SCHED_MAX})`); return { scheduled: true }; } diff --git a/server/src/nest/plugins/host/rpc/meta.rpc.ts b/server/src/nest/plugins/host/rpc/meta.rpc.ts index f8d40e929..8b14a3e25 100644 --- a/server/src/nest/plugins/host/rpc/meta.rpc.ts +++ b/server/src/nest/plugins/host/rpc/meta.rpc.ts @@ -15,6 +15,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors'; import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators'; import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types'; import { num, str } from '../../../../nest-rpc/rpc-params'; +import { UnitOfWork } from '../../../database/unit-of-work'; import { InjectRepository } from '@mikro-orm/nestjs'; /** Core entities a plugin may attach its own db:meta to. */ @@ -57,6 +58,7 @@ export class MetaRpc { @InjectRepository(Days) private readonly days: DaysRepository, @InjectRepository(Reservations) private readonly reservations: ReservationsRepository, @InjectRepository(DayAccommodations) private readonly dayAccommodations: DayAccommodationsRepository, + private readonly uow: UnitOfWork, ) {} @PluginMethod('meta.get', { permission: 'db:meta' }) @@ -79,8 +81,11 @@ export class MetaRpc { const json = JSON.stringify(params.value ?? null); if (json.length > META_VALUE_MAX) throw new BadParams(`metadata value too large (>${META_VALUE_MAX} bytes)`); // MR2/MR3/MR4 — Plan 3j Task 7 fix (must-land 2): one atomic call, not - // three separately-awaited ones (task-7-review.md's concurrent-cap-bypass). - const written = await this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX); + // three separately-awaited ones (task-7-review.md's concurrent-cap-bypass), + // in a unit of work, whose write lock keeps it atomic on Postgres's pool too. + const written = await this.uow.transactional(() => + this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX), + ); if (!written) throw new BadParams(`too many metadata keys on this ${entityType} (max ${META_KEYS_MAX})`); return { key, value: params.value ?? null }; } diff --git a/server/tests/helpers/plugin-host.ts b/server/tests/helpers/plugin-host.ts index e22926b82..ee8a20d8a 100644 --- a/server/tests/helpers/plugin-host.ts +++ b/server/tests/helpers/plugin-host.ts @@ -641,6 +641,7 @@ export async function createPluginRpcHostParts( await createTestDaysRepo(db), await createTestReservationsRepo(db), await createTestDayAccommodationsRepo(db), + await createTestUnitOfWork(db), ), // Plan 3j Task 5 — HostSurfaceRpc's own HR1/HR5–HR9 conversion: the plugin-visible // user row, the bilateral trip-sharing gate, and its own scheduler.set/cancel. @@ -655,6 +656,7 @@ export async function createPluginRpcHostParts( usersRepo, await createTestTripsRepo(db), pluginOrm.repo(PluginScheduledTasks), + await createTestUnitOfWork(db), ), new PluginHooks(undefined as never), ]; diff --git a/server/tests/helpers/rpc-host-deps.ts b/server/tests/helpers/rpc-host-deps.ts index e044ac3d9..9fde90e75 100644 --- a/server/tests/helpers/rpc-host-deps.ts +++ b/server/tests/helpers/rpc-host-deps.ts @@ -94,7 +94,16 @@ export function allRpcControllers(): object[] { // Plan 3j Task 5 — MetaRpc/HostSurfaceRpc's own repository conversions (MR#/HR#). // Plan 4 Task 2 — both classes' own DatabaseService param is gone (canAccessTrip // now reads through the TripsRepository they already inject), one fewer anyService(). - new MetaRpc(anyService(), anyService(), anyService(), anyService(), anyService(), anyService(), anyService()), + new MetaRpc( + anyService(), + anyService(), + anyService(), + anyService(), + anyService(), + anyService(), + anyService(), + anyService(), + ), new HostSurfaceRpc( anyService(), anyService(), @@ -105,6 +114,7 @@ export function allRpcControllers(): object[] { anyService(), anyService(), anyService(), + anyService(), ), // Owns no wire method, but declares all 18 hook contracts, which requireTotalCoverage checks too. new PluginHooks(anyService()), diff --git a/server/tests/unit/plugins/host-rpc.test.ts b/server/tests/unit/plugins/host-rpc.test.ts index 592b4ce8f..b20377461 100644 --- a/server/tests/unit/plugins/host-rpc.test.ts +++ b/server/tests/unit/plugins/host-rpc.test.ts @@ -22,6 +22,7 @@ import { PluginGuards } from '../../../src/nest-rpc/plugin-guards.service'; import { PluginRpcRegistryService } from '../../../src/nest-rpc/rpc-kit/registry.service'; import { createTestPluginRegistry } from '../../../src/nest-rpc/rpc-kit/testing'; import type { AddonsService } from '../../../src/nest/addons/addons.service'; +import type { UnitOfWork } from '../../../src/nest/database/unit-of-work'; import { UnreadableLlmResponse } from '../../../src/nest/llm-parse/clients/openai-compatible.client'; import type { LlmConfigResolver } from '../../../src/nest/llm-parse/llm-config.resolver'; import type { NotificationsService } from '../../../src/nest/notifications/notifications.service'; @@ -397,9 +398,11 @@ const usersRepo = { // params are gone: canAccessTrip now reads through the TripsRepository each // already injects (`tripsRepo` above, findAccessible-only). const guards = new PluginGuards(tripsRepo, permissions, addons, usersRepo); +/** The capped writes run in a unit of work; the stand-in repositories here are atomic on their own. */ +const uow = { transactional: (fn: () => Promise) => fn() } as unknown as UnitOfWork; const registry = createTestPluginRegistry([ new DbRpc(userSettings), - new MetaRpc(guards, metaRepo, tripsRepo, placesRepo, daysRepo, reservationsRepo, dayAccommodationsRepo), + new MetaRpc(guards, metaRepo, tripsRepo, placesRepo, daysRepo, reservationsRepo, dayAccommodationsRepo, uow), new HostSurfaceRpc( realtime, notifications, @@ -410,6 +413,7 @@ const registry = createTestPluginRegistry([ usersRepo, tripsRepo, scheduledTasksRepo, + uow, ), ]); const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService);