From 005060ac7a3dfbb6ce1319dcaed43eb2f9d6d006 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Thu, 22 Oct 2026 00:21:02 -0700 Subject: [PATCH] refactor(relay): group assignment cleanup and diagnostics (#31871) --- .../apps/relay/src/activity-lease-reader-census.test.ts | 7 ++++--- cloud/apps/relay/src/app.ts | 3 +-- .../{ => assignment}/assignment-cleanup-steps.test.ts | 0 .../src/{ => assignment}/assignment-cleanup-steps.ts | 2 +- .../assignment-inventory-snapshot.test.ts | 2 +- .../{ => assignment}/assignment-inventory-snapshot.ts | 4 ++-- .../assignment-rejection-log-window.test.ts | 0 .../{ => assignment}/assignment-rejection-log-window.ts | 0 .../assignment-rejection-logging.test.ts | 9 +++++---- cloud/apps/relay/src/floating-promise-census.test.ts | 9 +++++++-- cloud/apps/relay/src/index.ts | 4 ++-- cloud/apps/relay/src/phone-connection-log.ts | 2 +- cloud/apps/relay/src/post-proof-close-log.ts | 2 +- cloud/apps/relay/src/regional-rehome-store.test.ts | 2 +- 14 files changed, 26 insertions(+), 20 deletions(-) rename cloud/apps/relay/src/{ => assignment}/assignment-cleanup-steps.test.ts (100%) rename cloud/apps/relay/src/{ => assignment}/assignment-cleanup-steps.ts (97%) rename cloud/apps/relay/src/{ => assignment}/assignment-inventory-snapshot.test.ts (98%) rename cloud/apps/relay/src/{ => assignment}/assignment-inventory-snapshot.ts (98%) rename cloud/apps/relay/src/{ => assignment}/assignment-rejection-log-window.test.ts (100%) rename cloud/apps/relay/src/{ => assignment}/assignment-rejection-log-window.ts (100%) rename cloud/apps/relay/src/{ => assignment}/assignment-rejection-logging.test.ts (97%) diff --git a/cloud/apps/relay/src/activity-lease-reader-census.test.ts b/cloud/apps/relay/src/activity-lease-reader-census.test.ts index 749b0573c1c..b30fcdfed14 100644 --- a/cloud/apps/relay/src/activity-lease-reader-census.test.ts +++ b/cloud/apps/relay/src/activity-lease-reader-census.test.ts @@ -1,5 +1,5 @@ import { readdirSync, readFileSync } from 'node:fs' -import { join } from 'node:path' +import { join, sep } from 'node:path' import { fileURLToPath } from 'node:url' import ts from 'typescript-api' import { afterEach, describe, expect, it } from 'vitest' @@ -44,7 +44,7 @@ const ACTIVITY_LEASE_READERS: Record = { // One host's rows under lock; each caller is classified in the reserve-mode census. 'assignment-store.ts:lockAssignmentActivities': 'host-lock', // Metrics and previews: they report, and act on nothing. - 'assignment-inventory-snapshot.ts:readAssignmentInventorySnapshot': 'report-only', + 'assignment/assignment-inventory-snapshot.ts:readAssignmentInventorySnapshot': 'report-only', 'region-correction-preview.ts:previewRegionalRehomeEligibility': 'report-only', 'registered-migration-inventory.ts:readRegisteredMigrationInventory': 'report-only', // Step 5's existing-only stranded release, fixed separately in #30609 before the first flip. @@ -147,7 +147,8 @@ function activityLeaseReaders(): { readers: string[]; bareTableNames: string[] } const files = readdirSync(directory, { recursive: true, encoding: 'utf8' }).filter( (file) => file.endsWith('.ts') && !file.includes('.test.') && !file.split(/[\\/]/).includes('test-fixtures') ) - for (const file of files) { + for (const relativeFile of files) { + const file = relativeFile.split(sep).join('/') const text = readFileSync(join(directory, file), 'utf8') if (!text.includes(LEASE_TABLE)) continue const source = ts.createSourceFile(file, text, ts.ScriptTarget.ES2022, true) diff --git a/cloud/apps/relay/src/app.ts b/cloud/apps/relay/src/app.ts index 694bf72f113..5b0bc6608ba 100644 --- a/cloud/apps/relay/src/app.ts +++ b/cloud/apps/relay/src/app.ts @@ -38,7 +38,7 @@ import { type ResolvedRelayAssignment } from './assignment-store.js' import { ASSIGNMENT_LEASE_AUDIENCE, assignmentLeaseKeyId } from './assignment-lease.js' -import { AssignmentRejectionLogWindow } from './assignment-rejection-log-window.js' +import { AssignmentRejectionLogWindow } from './assignment/assignment-rejection-log-window.js' import { CELL_ADMISSION_STATES } from './cell-admission-selector.js' import { registerCellSeatFeedRoute } from './cell-seat-feed-route.js' import { ReservePaceError, type ReserveAssignment, type ReservePlan } from './reserve-assignment.js' @@ -2375,7 +2375,6 @@ function operationError(error: unknown): string { return error instanceof Error ? error.message : 'operation_failed' } -export { relayHostLogDigest } // `suppressed` is present only on a window-closing line, and counts the rejections // that line stands for beyond the one already logged when the window opened. diff --git a/cloud/apps/relay/src/assignment-cleanup-steps.test.ts b/cloud/apps/relay/src/assignment/assignment-cleanup-steps.test.ts similarity index 100% rename from cloud/apps/relay/src/assignment-cleanup-steps.test.ts rename to cloud/apps/relay/src/assignment/assignment-cleanup-steps.test.ts diff --git a/cloud/apps/relay/src/assignment-cleanup-steps.ts b/cloud/apps/relay/src/assignment/assignment-cleanup-steps.ts similarity index 97% rename from cloud/apps/relay/src/assignment-cleanup-steps.ts rename to cloud/apps/relay/src/assignment/assignment-cleanup-steps.ts index 9edd7044313..9dad9e8593f 100644 --- a/cloud/apps/relay/src/assignment-cleanup-steps.ts +++ b/cloud/apps/relay/src/assignment/assignment-cleanup-steps.ts @@ -1,4 +1,4 @@ -import { runRelayBackgroundOperation } from './relay-background-operation.js' +import { runRelayBackgroundOperation } from '../relay-background-operation.js' // The twelve periodic assignment sweeps the director runs every 30s. Each step // re-derives its state from the database and is idempotent, so they carry no diff --git a/cloud/apps/relay/src/assignment-inventory-snapshot.test.ts b/cloud/apps/relay/src/assignment/assignment-inventory-snapshot.test.ts similarity index 98% rename from cloud/apps/relay/src/assignment-inventory-snapshot.test.ts rename to cloud/apps/relay/src/assignment/assignment-inventory-snapshot.test.ts index 0e55a22a3df..d5e0b02855a 100644 --- a/cloud/apps/relay/src/assignment-inventory-snapshot.test.ts +++ b/cloud/apps/relay/src/assignment/assignment-inventory-snapshot.test.ts @@ -3,7 +3,7 @@ import { formatAssignmentInventorySnapshot, readAssignmentInventorySnapshot } from './assignment-inventory-snapshot.js' -import { openInMemoryRelayDatabase } from './database.js' +import { openInMemoryRelayDatabase } from '../database.js' describe('assignment inventory snapshot', () => { it('reports per-cell counters, lease backlog, and reservation debt', async () => { diff --git a/cloud/apps/relay/src/assignment-inventory-snapshot.ts b/cloud/apps/relay/src/assignment/assignment-inventory-snapshot.ts similarity index 98% rename from cloud/apps/relay/src/assignment-inventory-snapshot.ts rename to cloud/apps/relay/src/assignment/assignment-inventory-snapshot.ts index 9e12610492f..b17fb8dcf04 100644 --- a/cloud/apps/relay/src/assignment-inventory-snapshot.ts +++ b/cloud/apps/relay/src/assignment/assignment-inventory-snapshot.ts @@ -1,6 +1,6 @@ import { RELAY_DEFAULT_REGION } from '@orca-cloud/relay-contract' -import type { RelayDatabase, SqlRow } from './database.js' -import { REGIONAL_REHOME_ABORT_REPORT_WINDOW_MS } from './regional-rehome-abort-reason.js' +import type { RelayDatabase, SqlRow } from '../database.js' +import { REGIONAL_REHOME_ABORT_REPORT_WINDOW_MS } from '../regional-rehome-abort-reason.js' export type CellInventorySnapshotRow = { cellId: string diff --git a/cloud/apps/relay/src/assignment-rejection-log-window.test.ts b/cloud/apps/relay/src/assignment/assignment-rejection-log-window.test.ts similarity index 100% rename from cloud/apps/relay/src/assignment-rejection-log-window.test.ts rename to cloud/apps/relay/src/assignment/assignment-rejection-log-window.test.ts diff --git a/cloud/apps/relay/src/assignment-rejection-log-window.ts b/cloud/apps/relay/src/assignment/assignment-rejection-log-window.ts similarity index 100% rename from cloud/apps/relay/src/assignment-rejection-log-window.ts rename to cloud/apps/relay/src/assignment/assignment-rejection-log-window.ts diff --git a/cloud/apps/relay/src/assignment-rejection-logging.test.ts b/cloud/apps/relay/src/assignment/assignment-rejection-logging.test.ts similarity index 97% rename from cloud/apps/relay/src/assignment-rejection-logging.test.ts rename to cloud/apps/relay/src/assignment/assignment-rejection-logging.test.ts index d84a813d46c..c0181223314 100644 --- a/cloud/apps/relay/src/assignment-rejection-logging.test.ts +++ b/cloud/apps/relay/src/assignment/assignment-rejection-logging.test.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it, vi } from 'vitest' -import { RelayHomeCellUnavailableError, type RelayAssignment } from './assignment-store.js' -import type { RelayConfig } from './config.js' +import { RelayHomeCellUnavailableError, type RelayAssignment } from '../assignment-store.js' +import type { RelayConfig } from '../config.js' const fakes = vi.hoisted(() => ({ verifyRelayToken: vi.fn(async (token: string) => ({ @@ -9,12 +9,13 @@ const fakes = vi.hoisted(() => ({ })) })) -vi.mock('./relay-token-verifier.js', () => ({ +vi.mock('../relay-token-verifier.js', () => ({ createRelayTokenVerifier: () => fakes.verifyRelayToken, readBearer: (value: string | undefined) => value?.replace(/^Bearer /, '') ?? null })) -import { createRelayApp, relayHostLogDigest } from './app.js' +import { createRelayApp } from '../app.js' +import { relayHostLogDigest } from '../relay-host-log-digest.js' describe('assignment rejection logging', () => { afterEach(() => { diff --git a/cloud/apps/relay/src/floating-promise-census.test.ts b/cloud/apps/relay/src/floating-promise-census.test.ts index 04259d9e29a..581e548027b 100644 --- a/cloud/apps/relay/src/floating-promise-census.test.ts +++ b/cloud/apps/relay/src/floating-promise-census.test.ts @@ -35,8 +35,13 @@ const COMPILER_OPTIONS: ts.CompilerOptions = { } function productionFiles(): string[] { - return readdirSync(sourceDirectory) - .filter((entry) => entry.endsWith('.ts') && !entry.endsWith('.test.ts')) + return readdirSync(sourceDirectory, { recursive: true, encoding: 'utf8' }) + .filter( + (entry) => + entry.endsWith('.ts') && + !entry.endsWith('.test.ts') && + !entry.split(/[\\/]/).includes('test-fixtures') + ) .map((entry) => join(sourceDirectory, entry)) } diff --git a/cloud/apps/relay/src/index.ts b/cloud/apps/relay/src/index.ts index 67547e4e781..d27f82eac75 100644 --- a/cloud/apps/relay/src/index.ts +++ b/cloud/apps/relay/src/index.ts @@ -1,7 +1,7 @@ import { formatAssignmentInventorySnapshot, readAssignmentInventorySnapshot -} from './assignment-inventory-snapshot.js' +} from './assignment/assignment-inventory-snapshot.js' import { readRegionCorrectionOutcomes } from './region-correction-outcomes.js' import { RelayAssignmentStore } from './assignment-store.js' import { loadRelayConfig } from './config.js' @@ -24,7 +24,7 @@ import { consumeRelayDatabasePoolPressure, readRelayDatabasePoolPressure } from './database.js' -import { runAssignmentCleanup } from './assignment-cleanup-steps.js' +import { runAssignmentCleanup } from './assignment/assignment-cleanup-steps.js' import { runRelayBackgroundOperation } from './relay-background-operation.js' import { readPostgresLockWaitSample } from './postgres-lock-wait-sample.js' import { jitteredSweepIntervalMs } from './relay-sweep-schedule.js' diff --git a/cloud/apps/relay/src/phone-connection-log.ts b/cloud/apps/relay/src/phone-connection-log.ts index 52b8c3ace6f..c252c8390af 100644 --- a/cloud/apps/relay/src/phone-connection-log.ts +++ b/cloud/apps/relay/src/phone-connection-log.ts @@ -1,4 +1,4 @@ -import { AssignmentRejectionLogWindow } from './assignment-rejection-log-window.js' +import { AssignmentRejectionLogWindow } from './assignment/assignment-rejection-log-window.js' import { relayHostLogDigest } from './relay-host-log-digest.js' // Phone-connection anomalies that repeat per dial (a stuck database, a lost ledger write, a diff --git a/cloud/apps/relay/src/post-proof-close-log.ts b/cloud/apps/relay/src/post-proof-close-log.ts index 8385509309d..51d074d3aea 100644 --- a/cloud/apps/relay/src/post-proof-close-log.ts +++ b/cloud/apps/relay/src/post-proof-close-log.ts @@ -1,4 +1,4 @@ -import { AssignmentRejectionLogWindow } from './assignment-rejection-log-window.js' +import { AssignmentRejectionLogWindow } from './assignment/assignment-rejection-log-window.js' import { printableCloseReason } from './control-close-reason-text.js' import { relayHostLogDigest } from './relay-host-log-digest.js' import type { RelayRuntimeObserver } from './relay-observability.js' diff --git a/cloud/apps/relay/src/regional-rehome-store.test.ts b/cloud/apps/relay/src/regional-rehome-store.test.ts index 5a7c1072d81..51f5638d299 100644 --- a/cloud/apps/relay/src/regional-rehome-store.test.ts +++ b/cloud/apps/relay/src/regional-rehome-store.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { readAssignmentInventorySnapshot } from './assignment-inventory-snapshot.js' +import { readAssignmentInventorySnapshot } from './assignment/assignment-inventory-snapshot.js' import { REGIONAL_REHOME_ARRIVAL_WINDOW_MS } from './regional-rehome-abort-reason.js' import { RelayAssignmentStore as BaseRelayAssignmentStore,