Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

37.9 KB
Raw Blame
import { randomUUID } from 'node:crypto';
import path from 'node:path';

import type { FtsSearchDocumentEntity } from '@lobechat/types';
import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';

import { ElasticsearchFtsSearchHttpClient } from '../../apps/server/src/services/ftsSearch/elasticsearch';
import { FtsSearchSyncService } from '../../apps/server/src/services/ftsSearchSync/service';
import {
  FTS_SEARCH_DOCUMENT_ENTITIES,
  FTS_SEARCH_INDEX_DEFINITIONS,
  FtsSearchDocumentBuilder,
  getFtsSearchIndexAlias,
  getFtsSearchIndexSchemaVersion,
  getFtsSearchPhysicalIndexName,
} from '../../packages/database/src/repositories/ftsSearchDocument';
import { FtsSearchSyncOutboxRepository } from '../../packages/database/src/repositories/ftsSearchSyncOutbox';
import * as schema from '../../packages/database/src/schemas';
import {
  observeFtsSearchReindexRun,
  recordFtsSearchReindexBatch,
  recordFtsSearchReindexBulkRequest,
  recordFtsSearchReindexBulkRetry,
  recordFtsSearchReindexReconciliation,
} from '../../packages/observability-otel/src/modules/fts-search-reindex';
import { DiagLogLevel, register, shutdownSafely } from '../../packages/observability-otel/src/node';
import { runElasticsearchFtsSearchSync } from '../elasticsearchSync';
import { runWithLockRetry } from '../migrateServerDB/retry';
import { resolveFtsSearchMigrationCommand } from './commandOptions';
import {
  assertFtsSearchReindexElasticsearchHostname,
  assertFtsSearchReindexRangeCollation,
  assertFtsSearchReindexTelemetryExportConfigured,
  resolveFtsSearchReindexBatchSizeByEntity,
  resolveFtsSearchReindexElasticsearchEnvironment,
  resolveFtsSearchReindexEntities,
  resolveFtsSearchReindexRangeConcurrencyByEntity,
  resolveFtsSearchReindexTelemetryEnvironment,
} from './options';
import { runFtsSearchReindexCommand } from './preparation';
import {
  describeEntityGeneration,
  type FtsSearchReindexAuditValue,
  FtsSearchReindexEntityError,
  FtsSearchReindexFileLogger,
  FtsSearchReindexFileRepository,
  FtsSearchReindexHttpClient,
  FtsSearchReindexService,
  promoteGeneration,
  retireGenerations,
  summarizeFtsSearchReindexError,
} from './runtime';
import type { FtsSearchGenerationSummary } from './runtime/generationService';
import {
  planRetiredGenerations,
  purgeRetiredGenerations,
  resolveInPlaceTarget,
} from './runtime/generationService';
import { FtsSearchMigrationLockClient, FtsSearchMigrationLockError } from './runtime/migrationLock';
import {
  assertFtsSearchStartupGenerationSafe,
  runFtsSearchStartupMigration,
} from './startupCoordinator';

const { Pool } = pg;

const REINDEX_BYTE_BUCKETS = [
  0, 256, 1024, 4096, 16_384, 65_536, 262_144, 1_048_576, 4_194_304, 16_777_216, 52_428_800,
];
const REINDEX_COUNT_BUCKETS = [0, 1, 5, 10, 25, 50, 100, 250, 500, 1000];
const REINDEX_DURATION_MS_BUCKETS = [
  0, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 3000, 5000, 10_000, 30_000, 60_000,
];

const readPositiveIntegerArgument = (name: string) => {
  const argument = process.argv.find((item) => item.startsWith(`${name}=`));
  if (!argument) return;
  const value = Number.parseInt(argument.slice(name.length + 1), 10);
  if (!Number.isInteger(value) || value < 1) throw new Error(`${name} must be a positive integer`);
  return value;
};

const readNonNegativeIntegerArgument = (name: string) => {
  const argument = process.argv.find((item) => item.startsWith(`${name}=`));
  if (!argument) return;
  const value = Number.parseInt(argument.slice(name.length + 1), 10);
  if (!Number.isInteger(value) || value < 0) {
    throw new Error(`${name} must be a non-negative integer`);
  }
  return value;
};

const readStringArgument = (name: string) => {
  const argumentsForName = process.argv.filter((item) => item.startsWith(`${name}=`));
  if (argumentsForName.length > 1) throw new Error(`${name} may only be provided once`);
  const value = argumentsForName[0]?.slice(name.length + 1);
  if (value !== undefined && value.length === 0) throw new Error(`${name} requires a value`);
  return value;
};

const readUuidArgument = (name: string) => {
  const value = readStringArgument(name);
  if (
    value !== undefined &&
    !/^[\da-f]{8}-[\da-f]{4}-[1-8][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i.test(value)
  ) {
    throw new Error(`${name} must be a UUID`);
  }
  return value?.toLowerCase();
};

const args = new Set(process.argv.slice(2));
const { command, releaseLockOwner } = resolveFtsSearchMigrationCommand(process.argv.slice(2));
const apply = args.has('--apply');
const startup = args.has('--startup');
const freshRun = args.has('--fresh-run');
const inPlace = args.has('--in-place');
const rebuildCurrent = args.has('--rebuild-current');
const promote = args.has('--promote');
const retire = args.has('--retire');
const purge = args.has('--purge');
const skipFailureArgument = process.argv.find((item) => item.startsWith('--skip-failure='));
const promoteVersion = readPositiveIntegerArgument('--version');
const promoteGenerationName = readStringArgument('--generation');
const rebuildRunId = readUuidArgument('--run-id');
const batchSize = readPositiveIntegerArgument('--batch-size');
const bulkConcurrency = readPositiveIntegerArgument('--bulk-concurrency');
const bulkMaxBytes = readPositiveIntegerArgument('--bulk-max-bytes');
const batchSizeByEntity = resolveFtsSearchReindexBatchSizeByEntity(process.argv.slice(2));
const entities = resolveFtsSearchReindexEntities(process.argv.slice(2));
const rangeConcurrencyByEntity = resolveFtsSearchReindexRangeConcurrencyByEntity(
  process.argv.slice(2),
);
const entityConcurrency = readPositiveIntegerArgument('--entity-concurrency');
const maxBatchesPerEntity = readPositiveIntegerArgument('--max-batches-per-entity');
const maxRequestRetries = readNonNegativeIntegerArgument('--max-request-retries');
const requestTimeoutMs = readPositiveIntegerArgument('--request-timeout-ms');
const retryBaseDelayMs = readNonNegativeIntegerArgument('--retry-base-delay-ms');
const telemetryEnvironment = resolveFtsSearchReindexTelemetryEnvironment(process.argv.slice(2));

const knownArguments = new Set([
  '--apply',
  '--fresh-run',
  '--in-place',
  '--rebuild-current',
  '--promote',
  '--retire',
  '--purge',
  '--status',
  '--startup',
  '--yes',
]);
const unknownArgument = process.argv
  .slice(2)
  .find(
    (item) =>
      item.startsWith('--') &&
      !knownArguments.has(item) &&
      !item.startsWith('--batch-size=') &&
      !item.startsWith('--bulk-concurrency=') &&
      !item.startsWith('--bulk-max-bytes=') &&
      !item.startsWith('--entity-batch-size=') &&
      !item.startsWith('--entity=') &&
      !item.startsWith('--entity-concurrency=') &&
      !item.startsWith('--entity-range-concurrency=') &&
      !item.startsWith('--generation=') &&
      !item.startsWith('--elasticsearch-api-key-env=') &&
      !item.startsWith('--elasticsearch-url-env=') &&
      !item.startsWith('--expected-elasticsearch-host-prefix=') &&
      !item.startsWith('--max-batches-per-entity=') &&
      !item.startsWith('--max-request-retries=') &&
      !item.startsWith('--request-timeout-ms=') &&
      !item.startsWith('--retry-base-delay-ms=') &&
      !item.startsWith('--run-id=') &&
      !item.startsWith('--release-lock=') &&
      !item.startsWith('--skip-failure=') &&
      !item.startsWith('--telemetry-environment=') &&
      !item.startsWith('--version='),
  );
if (unknownArgument) throw new Error(`Unknown argument: ${unknownArgument}`);

const readFailureReference = ():
  { documentId: string; entity: FtsSearchDocumentEntity } | undefined => {
  if (!skipFailureArgument) return;
  const reference = skipFailureArgument.slice('--skip-failure='.length);
  const separator = reference.indexOf(':');
  if (separator < 1 || separator === reference.length - 1) {
    throw new Error('--skip-failure must use <entity>:<document-id>');
  }
  const entityName = reference.slice(0, separator);
  const entity = FTS_SEARCH_DOCUMENT_ENTITIES.find((item) => item === entityName);
  if (!entity) throw new Error(`Unknown search entity: ${entityName}`);
  return { documentId: reference.slice(separator + 1), entity };
};

const failureReference = readFailureReference();
const { apiKeyEnvironmentName, expectedHostPrefix, urlEnvironmentName } =
  resolveFtsSearchReindexElasticsearchEnvironment(process.argv.slice(2));

const databaseUrl = process.env.DATABASE_URL;
const elasticsearchApiKey = process.env[apiKeyEnvironmentName];
const elasticsearchUrl = process.env[urlEnvironmentName];
/** Same explicit opt-in as the application runtime; see `packages/env/src/ftsSearch.ts`. */
const allowInsecureHttp = process.env.ES_ALLOW_INSECURE_HTTP === 'true';
const namespace = process.env.ES_INDEX_NAMESPACE;
const configuredStateDirectory = process.env.ES_REINDEX_STATE_DIR;
const stateDirectory = path.resolve(configuredStateDirectory ?? '.elasticsearch-reindex');

/** Commands that talk to Elasticsearch; `--status` only inspects it when the URL is configured. */
const elasticsearchMutation = command === 'status' ? null : `--${command}`;

if (!databaseUrl && command !== 'release-lock') throw new Error('DATABASE_URL is required');
if (!namespace) throw new Error('ES_INDEX_NAMESPACE is required');
if (elasticsearchMutation && !elasticsearchApiKey && !allowInsecureHttp) {
  throw new Error(`${apiKeyEnvironmentName} is required with ${elasticsearchMutation}`);
}
if (elasticsearchMutation && !elasticsearchUrl) {
  throw new Error(`${urlEnvironmentName} is required with ${elasticsearchMutation}`);
}
if ((apply || startup || failureReference) && !configuredStateDirectory) {
  throw new Error('ES_REINDEX_STATE_DIR is required for reindex mutations and resume attempts');
}
if (process.env.ENABLE_TELEMETRY && !telemetryEnvironment) {
  throw new Error('--telemetry-environment is required when ENABLE_TELEMETRY is set');
}
if (process.env.ENABLE_TELEMETRY) {
  assertFtsSearchReindexTelemetryExportConfigured(process.env);
}

const telemetrySdk = process.env.ENABLE_TELEMETRY
  ? register({
      autoDetectResources: false,
      autoInstrumentations: false,
      debug: DiagLogLevel.ERROR,
      environment: telemetryEnvironment,
      histogramViews: [
        {
          boundaries: [0, 1, 2, 3, 5, 10],
          instrumentName: 'fts_search_reindex_bulk_request_attempts',
          meterName: 'fts-search-reindex',
        },
        {
          boundaries: REINDEX_BYTE_BUCKETS,
          instrumentName: 'fts_search_reindex_bulk_request_size',
          meterName: 'fts-search-reindex',
        },
        {
          boundaries: REINDEX_DURATION_MS_BUCKETS,
          instrumentName: 'fts_search_reindex_bulk_request_duration',
          meterName: 'fts-search-reindex',
        },
        {
          boundaries: REINDEX_COUNT_BUCKETS,
          instrumentName: 'fts_search_reindex_bulk_request_items',
          meterName: 'fts-search-reindex',
        },
      ],
      name: 'lobehub-fts-search-reindex',
    })
  : undefined;
const pool = new Pool({ connectionString: databaseUrl });
const db = drizzle(pool, { schema });
const outbox = new FtsSearchSyncOutboxRepository(db);
const repository = new FtsSearchReindexFileRepository({
  readCaptureFingerprint: () => outbox.readCaptureFingerprint(),
  readHighWaterRevision: () => outbox.readHighWaterRevision(),
  reserveRevisionWithWriteFence: (entities) => outbox.reserveRevisionWithWriteFence(entities),
  stateDirectory,
});

const logErrorSummary = (message: string, error: unknown) => {
  console.error(message, summarizeFtsSearchReindexError(error));
};

let assertMigrationOwnership: (() => Promise<void>) | undefined;

const createMigrationLockClient = () =>
  new FtsSearchMigrationLockClient({
    allowInsecureHttp,
    apiKey: elasticsearchApiKey,
    namespace,
    requestTimeoutMs,
    url: elasticsearchUrl!,
  });

const createElasticsearchClient = () =>
  new FtsSearchReindexHttpClient({
    allowInsecureHttp,
    apiKey: elasticsearchApiKey,
    beforeMutation: assertMigrationOwnership,
    requestTimeoutMs,
    url: elasticsearchUrl!,
  });

/** Every schema generation the deployed code declares, ascending, with the entities on each. */
const declaredGenerations = (): Array<[number, FtsSearchDocumentEntity[]]> => {
  const groups = new Map<number, FtsSearchDocumentEntity[]>();
  for (const entity of FTS_SEARCH_DOCUMENT_ENTITIES) {
    const version = getFtsSearchIndexSchemaVersion(entity);
    groups.set(version, [...(groups.get(version) ?? []), entity]);
  }
  return [...groups].sort(([left], [right]) => left - right);
};

const readCheckpoint = (
  checkpointNamespace: string,
  schemaVersion: number,
  reindexRunId: string | null,
) => repository.getGenerationRun(checkpointNamespace, schemaVersion, reindexRunId);

const generationAuditValue = (generation: FtsSearchGenerationSummary) => ({
  ...generation,
  fieldCompatibility: generation.fieldCompatibility
    ? {
        ...generation.fieldCompatibility,
        incompatibleFields: generation.fieldCompatibility.incompatibleFields.map((field) => ({
          ...field,
          targetType: field.targetType ?? null,
        })),
      }
    : null,
});

const readStatus = async () => {
  const runs = [];
  for (const state of await repository.listRuns(namespace)) {
    const unresolvedFailures = await repository.listUnresolvedFailures(state.run.id);
    runs.push({
      baseRevision: state.run.baseRevision,
      backfillHighWaterRevision: state.run.backfillHighWaterRevision,
      entities: state.progress.map((progress) => ({
        cursor: progress.cursor,
        entity: progress.entity,
        failedCount: progress.failedCount,
        indexedCount: progress.indexedCount,
        physicalIndex: progress.physicalIndex,
        processedCount: progress.processedCount,
        status: progress.status,
      })),
      id: state.run.id,
      schemaVersion: state.run.schemaVersion,
      status: state.run.status,
      unresolvedFailures: unresolvedFailures.map(
        ({ attempts, documentId, entity, error, retryable }) => ({
          attempts,
          documentId,
          entity,
          errorSummary: summarizeFtsSearchReindexError(error),
          retryable,
        }),
      ),
    });
  }
  const outboxStats = await outbox.stats();
  const entityStats: Record<string, FtsSearchReindexAuditValue> = Object.fromEntries(
    Object.entries(outboxStats.entities).map(([entity, stats]) => [entity, { ...stats }]),
  );

  /**
   * Generation state lives in Elasticsearch (`_meta` plus the alias), so it can only be reported
   * when the endpoint is configured. Checkpoints alone cannot tell which generation is live.
   */
  let generations: FtsSearchReindexAuditValue = null;
  if (elasticsearchUrl) {
    const client = createElasticsearchClient();
    const statuses = [];
    for (const entity of entities ?? FTS_SEARCH_DOCUMENT_ENTITIES) {
      const entityStatus = await describeEntityGeneration({
        client,
        entity,
        namespace,
        readCheckpoint,
      });
      statuses.push({
        action: entityStatus.action,
        alias: entityStatus.alias,
        candidates: entityStatus.candidates.map(generationAuditValue),
        classification: entityStatus.classification,
        declared: entityStatus.declared,
        entity: entityStatus.entity,
        live: entityStatus.live ? generationAuditValue(entityStatus.live) : null,
        mappingChange: entityStatus.mappingChange,
        mappingDiff: entityStatus.mappingDiff
          ? {
              ...entityStatus.mappingDiff,
              changed: entityStatus.mappingDiff.changed.map((field) => ({ ...field })),
            }
          : null,
        retirementPlan: { ...planRetiredGenerations(entityStatus) },
      });
    }
    generations = statuses;
  }

  const migrationLock = elasticsearchUrl ? await createMigrationLockClient().read() : null;
  return {
    generations,
    migrationLock: migrationLock ? { ...migrationLock } : null,
    namespace,
    outbox: {
      dead: outboxStats.dead,
      entities: entityStats,
      expiredLeases: outboxStats.expiredLeases,
      highWaterRevision: outboxStats.highWaterRevision,
      inFlight: outboxStats.inFlight,
      oldestActiveRevision: outboxStats.oldestActiveRevision,
      oldestReadyAgeSeconds: outboxStats.oldestReadyAgeSeconds,
      pending: outboxStats.pending,
      ready: outboxStats.ready,
      revisionLag: outboxStats.revisionLag,
      retrying: outboxStats.retrying,
    },
    runs,
    stateDirectory,
  };
};

const printStatus = async () => {
  const currentStatus = await readStatus();
  console.log(JSON.stringify(currentStatus, null, 2));
  return currentStatus;
};

let auditLogger: FtsSearchReindexFileLogger | undefined;
const executionStartedAt = Date.now();

const run = async () => {
  if (failureReference) {
    await runFtsSearchReindexCommand({
      command: 'skip-failure',
      installCaptureInfrastructure: () => outbox.installCaptureInfrastructure(),
      runWithLockRetry,
      run: async () => {
        const state = rebuildRunId
          ? await repository.getRun(rebuildRunId)
          : await repository.getTargetRun(
              namespace,
              getFtsSearchIndexSchemaVersion(failureReference.entity),
            );
        if (!state) throw new Error(`No reindex run exists for namespace ${namespace}`);
        const skipped = await repository.skipFailure(
          state.run.id,
          failureReference.entity,
          failureReference.documentId,
        );
        if (!skipped) {
          throw new Error('The requested unresolved, non-retryable reindex failure was not found');
        }
        await printStatus();
      },
    });
    return;
  }

  if (promote || retire || purge) {
    const client = createElasticsearchClient();
    const endpointHostname = new URL(elasticsearchUrl!).hostname;
    assertFtsSearchReindexElasticsearchHostname(endpointHostname, expectedHostPrefix);
    await runFtsSearchReindexCommand({
      command: promote ? 'promote' : purge ? 'purge' : 'retire',
      installCaptureInfrastructure: () => outbox.installCaptureInfrastructure(),
      runWithLockRetry,
      run: async () => {
        for (const entity of entities!) {
          if (promote) {
            const result = await promoteGeneration({
              client,
              entity,
              generation: promoteGenerationName,
              namespace,
              outboxStats: await outbox.stats(),
              readCheckpoint,
              validateCheckpointCapture: (runId) => repository.assertRunCaptureFingerprint(runId),
              version: promoteVersion,
            });
            console.log(JSON.stringify({ ...result, entity, type: 'generation_promoted' }));
          } else if (purge) {
            const result = await purgeRetiredGenerations({
              client,
              entity,
              namespace,
              readCheckpoint,
            });
            console.log(JSON.stringify({ ...result, entity, type: 'generations_purged' }));
          } else {
            const result = await retireGenerations({ client, entity, namespace, readCheckpoint });
            console.log(JSON.stringify({ ...result, entity, type: 'generations_retired' }));
          }
        }
        await printStatus();
      },
    });
    return;
  }

  if (startup) {
    const endpointHostname = new URL(elasticsearchUrl!).hostname;
    assertFtsSearchReindexElasticsearchHostname(endpointHostname, expectedHostPrefix);
    const client = createElasticsearchClient();
    const syncClient = new ElasticsearchFtsSearchHttpClient({
      allowInsecureHttp,
      apiKey: elasticsearchApiKey,
      indexNamespace: namespace,
      requestTimeoutMs: requestTimeoutMs ?? 20_000,
      url: elasticsearchUrl!,
    });
    const syncService = new FtsSearchSyncService(
      new FtsSearchDocumentBuilder(db),
      outbox,
      {
        bulk: async (body) => {
          await assertMigrationOwnership?.();
          return syncClient.bulk(body);
        },
        getFtsSearchSyncGenerationTargets: (aliases) =>
          syncClient.getFtsSearchSyncGenerationTargets(aliases),
        getFtsSearchSyncIndexFields: (entitiesByIndex) =>
          syncClient.getFtsSearchSyncIndexFields(entitiesByIndex),
      },
      namespace,
    );
    await runFtsSearchReindexCommand({
      command: 'startup',
      installCaptureInfrastructure: () => outbox.installCaptureInfrastructure(),
      runWithLockRetry,
      run: () =>
        runFtsSearchStartupMigration({
          applyGeneration: ({ freshRun, generationEntities, processEntities, schemaVersion }) =>
            applyGeneration({
              client,
              endpointHostname,
              freshRunForGeneration: freshRun,
              generationEntities,
              processEntities,
              schemaVersion,
            }),
          describeEntity: (entity) =>
            describeEntityGeneration({
              client,
              entity,
              namespace,
              readCheckpoint,
            }),
          drainIncrementalSync: async () => {
            const aliases = FTS_SEARCH_DOCUMENT_ENTITIES.map((entity) =>
              getFtsSearchIndexAlias(namespace, entity),
            );
            const summary = await runElasticsearchFtsSearchSync({
              loadRuntime: async () => ({
                getFtsSearchSyncService: () => syncService,
                verifyFtsSearchSyncReadiness: async () => {
                  await outbox.assertCaptureInfrastructure();
                  await syncClient.assertFtsSearchSyncAliases(aliases);
                },
              }),
              logStep: (step) =>
                console.log(JSON.stringify({ ...step, type: 'fts_search_startup_sync_step' })),
              maxSteps: 10_000,
            });
            return { hasMore: summary.hasMore };
          },
          generations: declaredGenerations().map(([schemaVersion, generationEntities]) => ({
            entities: generationEntities,
            schemaVersion,
          })),
          maxDrainRounds: 60,
          promoteEntity: async (entity) => {
            const result = await promoteGeneration({
              client,
              entity,
              namespace,
              outboxStats: await outbox.stats(),
              readCheckpoint,
              validateCheckpointCapture: (runId) => repository.assertRunCaptureFingerprint(runId),
            });
            console.log(JSON.stringify({ ...result, entity, type: 'generation_promoted' }));
          },
          readCheckpointEntities: async (schemaVersion) => {
            const checkpoint = await repository.getTargetRun(namespace, schemaVersion);
            return checkpoint?.progress.map(({ entity }) => entity);
          },
          readOutboxStats: () => outbox.stats(),
          waitForOutbox: () => new Promise((resolve) => setTimeout(resolve, 1000)),
        }),
    });
    await printStatus();
    return;
  }

  if (!apply) {
    await runFtsSearchReindexCommand({
      command: 'status',
      installCaptureInfrastructure: () => outbox.installCaptureInfrastructure(),
      runWithLockRetry,
      run: printStatus,
    });
    return;
  }

  if (Object.values(rangeConcurrencyByEntity).some((concurrency) => concurrency > 1)) {
    const collationResult = await pool.query<{ datcollate: string }>(
      'SELECT datcollate FROM pg_database WHERE datname = current_database()',
    );
    const databaseCollation = collationResult.rows[0]?.datcollate;
    if (!databaseCollation) throw new Error('Failed to read the PostgreSQL database collation');
    assertFtsSearchReindexRangeCollation(databaseCollation, rangeConcurrencyByEntity);
  }

  const endpointHostname = new URL(elasticsearchUrl!).hostname;
  assertFtsSearchReindexElasticsearchHostname(endpointHostname, expectedHostPrefix);
  const client = createElasticsearchClient();

  /** Preserve old source fields while any open generation can still receive full-document writes. */
  for (const entity of entities ?? FTS_SEARCH_DOCUMENT_ENTITIES) {
    const generation = await describeEntityGeneration({
      client,
      entity,
      namespace,
      readCheckpoint,
    });
    for (const target of [
      ...generation.candidates,
      ...(generation.live ? [generation.live] : []),
    ]) {
      if (target.state !== 'open') continue;
      if (!target.fieldCompatibility?.compatible) {
        throw new Error(
          `${target.index} cannot receive the current document projection; retain its source fields and compatible types until it is retired. Inspect --status fieldCompatibility before upgrading.`,
        );
      }
    }
  }

  /**
   * Each declared schema generation is backfilled by its own checkpoint. A first install has one
   * generation covering every entity; after a mapping bump only the bumped entities form a new,
   * higher generation that is built next to the live one and promoted separately.
   */
  const requestedEntities = new Set(entities ?? FTS_SEARCH_DOCUMENT_ENTITIES);
  const generations = declaredGenerations().filter(([, generationEntities]) =>
    generationEntities.some((entity) => requestedEntities.has(entity)),
  );
  if (rebuildCurrent && rebuildRunId && generations.length !== 1) {
    throw new Error('--run-id can only resume entities from one declared schema version');
  }
  for (const [schemaVersion, generationEntities] of generations) {
    const processEntities = generationEntities.filter((entity) => requestedEntities.has(entity));
    await applyGeneration({
      client,
      endpointHostname,
      generationEntities: rebuildCurrent ? processEntities : generationEntities,
      processEntities,
      rebuildCurrentForGeneration: rebuildCurrent,
      rebuildRunIdForGeneration: rebuildRunId,
      schemaVersion,
    });
  }
};

const applyGeneration = async ({
  client,
  endpointHostname,
  generationEntities,
  freshRunForGeneration = freshRun,
  processEntities,
  rebuildCurrentForGeneration = false,
  rebuildRunIdForGeneration,
  schemaVersion,
}: {
  client: FtsSearchReindexHttpClient;
  endpointHostname: string;
  generationEntities: FtsSearchDocumentEntity[];
  freshRunForGeneration?: boolean;
  processEntities: FtsSearchDocumentEntity[];
  rebuildCurrentForGeneration?: boolean;
  rebuildRunIdForGeneration?: string;
  schemaVersion: number;
}) => {
  const existing = rebuildCurrentForGeneration
    ? rebuildRunIdForGeneration
      ? await repository.getRun(rebuildRunIdForGeneration)
      : undefined
    : await repository.getTargetRun(namespace, schemaVersion);
  if (
    existing &&
    (existing.run.namespace !== namespace || existing.run.schemaVersion !== schemaVersion)
  ) {
    throw new Error(
      `Reindex run ${existing.run.id} belongs to ${existing.run.namespace} v${existing.run.schemaVersion}, not ${namespace} v${schemaVersion}`,
    );
  }
  if (rebuildCurrentForGeneration && rebuildRunIdForGeneration && !existing) {
    throw new Error(
      `No checkpoint exists for rebuild run ${rebuildRunIdForGeneration}; restore its checkpoint or start without --run-id after removing the orphan target`,
    );
  }
  const trackedEntities = existing
    ? existing.progress.map(({ entity }) => entity)
    : generationEntities;
  const runEntities = rebuildCurrentForGeneration ? trackedEntities : generationEntities;
  const missingRequestedEntity = processEntities.find(
    (entity) => !trackedEntities.includes(entity),
  );
  if (missingRequestedEntity) {
    throw new Error(
      `Reindex run ${existing?.run.id} does not cover ${missingRequestedEntity}; start a separate current-version rebuild`,
    );
  }
  /**
   * `--in-place` pins each requested entity to the index its alias serves today instead of a new
   * `<alias>-v<schemaVersion>`; the checkpoint remembers that choice, so a resume needs no flag.
   */
  const physicalIndexes: Partial<Record<FtsSearchDocumentEntity, string>> = {};
  let currentRebuildRunId: string | undefined;
  if (rebuildCurrentForGeneration) {
    currentRebuildRunId = existing?.run.id ?? randomUUID();
    for (const entity of trackedEntities) {
      const status = await describeEntityGeneration({ client, entity, namespace, readCheckpoint });
      if (
        status.classification !== 'in_sync' ||
        status.mappingChange !== 'identical' ||
        !status.live
      ) {
        throw new Error(
          `${entity} cannot rebuild its current version while ${status.alias} is ${status.classification} with mapping change ${status.mappingChange ?? 'unknown'}; finish the existing schema migration first`,
        );
      }
      const expectedIndex = getFtsSearchPhysicalIndexName(
        namespace,
        entity,
        schemaVersion,
        currentRebuildRunId,
      );
      const conflicting = status.candidates.find(
        (candidate) =>
          candidate.state === 'open' &&
          candidate.version === schemaVersion &&
          candidate.index !== expectedIndex &&
          candidate.supersededByReindexRunId !== status.live?.reindexRunId,
      );
      if (conflicting) {
        throw new Error(
          `${conflicting.index} is an unfinished same-version generation; resume it with --run-id=${conflicting.reindexRunId ?? '<run-id>'} or retire it before starting another rebuild`,
        );
      }
      const pinned = existing?.progress.find((progress) => progress.entity === entity);
      physicalIndexes[entity] = pinned?.physicalIndex ?? expectedIndex;
    }
  } else if (inPlace) {
    for (const entity of processEntities) {
      const pinned = existing?.progress.find((progress) => progress.entity === entity);
      if (pinned) {
        physicalIndexes[entity] = pinned.physicalIndex;
        continue;
      }
      const status = await describeEntityGeneration({ client, entity, namespace, readCheckpoint });
      const inPlaceIndex = resolveInPlaceTarget(status);
      /**
       * Widen the live index before the checkpoint reserves its base revision. From here on the
       * consumer writes the new fields into this index, so every change newer than the base
       * revision already carries them and the backfill only has to fill in older documents.
       * `prepareIndices` restamps `_meta.reindex_run_id` once the run exists.
       */
      await client.putMapping(inPlaceIndex, {
        _meta: {
          reindex_run_id: status.live!.reindexRunId!,
          schema_fingerprint: status.declared.fingerprint,
          schema_version: status.declared.version,
        },
        properties: FTS_SEARCH_INDEX_DEFINITIONS[entity].mappings.properties,
      });
      physicalIndexes[entity] = inPlaceIndex;
    }
  }
  let mode: 'fresh' | 'rebuild_current' | 'resume' | 'upgrade' | 'upgrade_in_place';
  if (existing) {
    if (freshRunForGeneration) {
      throw new Error(
        `Checkpoint ${existing.run.id} already exists; omit --fresh-run to resume it`,
      );
    }
    mode = 'resume';
  } else if (rebuildCurrentForGeneration) {
    mode = 'rebuild_current';
  } else {
    /**
     * Without a checkpoint, the live aliases decide what this generation is: none exist on a
     * fresh install (which must be confirmed with --fresh-run), all exist when a mapping bump
     * needs a new generation built beside the live one. A mixed state is not something this tool
     * created and needs an operator.
     */
    const aliased = new Set<FtsSearchDocumentEntity>();
    for (const entity of generationEntities) {
      const described = await client.describeGenerations(getFtsSearchIndexAlias(namespace, entity));
      if (described.some((generation) => generation.isWriteIndex)) aliased.add(entity);
    }
    if (aliased.size === 0) {
      if (!freshRunForGeneration) {
        throw new Error(
          `No checkpoint exists in ${stateDirectory} for v${schemaVersion}; pass --fresh-run only for a new, empty Elasticsearch target`,
        );
      }
      mode = 'fresh';
    } else if (aliased.size === generationEntities.length) {
      if (freshRunForGeneration) {
        throw new Error(
          `Aliases already exist for the v${schemaVersion} entities; this is a generation upgrade, omit --fresh-run`,
        );
      }
      mode = inPlace ? 'upgrade_in_place' : 'upgrade';
    } else {
      throw new Error(
        `Only some v${schemaVersion} entities have aliases (${[...aliased].join(', ')}); repair the aliases before continuing`,
      );
    }
  }

  const prepared = await runFtsSearchReindexCommand({
    command: 'apply',
    installCaptureInfrastructure: () => outbox.installCaptureInfrastructure(),
    runWithLockRetry,
    run: async () => {
      console.log(
        JSON.stringify({
          endpointEnvName: urlEnvironmentName,
          endpointHostname,
          expectedHostPrefix: expectedHostPrefix ?? null,
          schemaVersion,
          type: 'reindex_target',
        }),
      );
      return repository.createOrResume(
        namespace,
        schemaVersion,
        runEntities,
        physicalIndexes,
        currentRebuildRunId,
      );
    },
  });
  auditLogger = new FtsSearchReindexFileLogger({
    runId: prepared.run.id,
    sessionId: randomUUID(),
    stateDirectory,
  });
  await auditLogger.append({
    batchSize: batchSize ?? 500,
    bulkConcurrency: bulkConcurrency ?? 1,
    bulkMaxBytes: bulkMaxBytes ?? 50 * 1024 * 1024,
    credentialEnvName: apiKeyEnvironmentName,
    endpointHostname,
    endpointAuthentication: elasticsearchApiKey ? 'api_key' : 'none',
    endpointEnvName: urlEnvironmentName,
    expectedHostPrefix: expectedHostPrefix ?? null,
    batchSizeByEntity,
    entities: processEntities,
    entityConcurrency: entityConcurrency ?? 1,
    rangeConcurrencyByEntity,
    maxBatchesPerEntity: maxBatchesPerEntity ?? null,
    maxRequestRetries: maxRequestRetries ?? 4,
    mode,
    requestTimeoutMs: requestTimeoutMs ?? 30_000,
    retryBaseDelayMs: retryBaseDelayMs ?? 500,
    schemaVersion: prepared.run.schemaVersion,
    type: 'session_started',
  });
  console.log(
    JSON.stringify({
      eventsPath: auditLogger.eventsPath,
      runId: prepared.run.id,
      schemaVersion: prepared.run.schemaVersion,
      stateDirectory,
      summaryPath: auditLogger.summaryPath,
      type: existing ? 'reindex_resumed' : 'reindex_started',
    }),
  );

  const service = new FtsSearchReindexService(
    new FtsSearchDocumentBuilder(db),
    repository,
    client,
    {
      batchSize,
      bulkConcurrency,
      bulkMaxBytes,
      batchSizeByEntity,
      entities: processEntities,
      entityConcurrency,
      rangeConcurrencyByEntity,
      maxBatchesPerEntity,
      maxRequestRetries,
      onProgress: async (event) => {
        if (event.type === 'batch') {
          recordFtsSearchReindexBatch({
            checkpoint: event.checkpoint,
            entity: event.entity,
            failed: event.failed,
            indexed: event.indexed,
            scanned: event.processed,
          });
        }
        if (event.type === 'reconciliation') {
          recordFtsSearchReindexReconciliation(event);
        }
        if (event.type === 'bulk_retry') {
          recordFtsSearchReindexBulkRetry(event.entity);
        }
        if (event.type === 'bulk_completed') {
          recordFtsSearchReindexBulkRequest(event);
        }
        console.log(JSON.stringify(event));
        await auditLogger!.append(event);
      },
      retryBaseDelayMs,
      validateIncrementalSyncSource: () => outbox.assertCaptureInfrastructure(),
    },
  );
  const result = await service.run(namespace, schemaVersion, runEntities, prepared.run.id);
  console.log(JSON.stringify(result));
  const currentStatus = await printStatus();
  await auditLogger.append({
    elapsedMs: Date.now() - executionStartedAt,
    status: result.status,
    type: 'session_completed',
  });
  await auditLogger.writeSummary({
    elapsedMs: Date.now() - executionStartedAt,
    runStatus: result.status,
    status: currentStatus,
  });
};

/** A lock never expires: a paused process must not overlap an automatic replacement worker. */
const runWithMigrationLock = async () => {
  if (command === 'status') return run();
  assertFtsSearchReindexElasticsearchHostname(
    new URL(elasticsearchUrl!).hostname,
    expectedHostPrefix,
  );
  const lock = createMigrationLockClient();
  if (command === 'release-lock') {
    const result = await lock.release(releaseLockOwner!);
    console.log(JSON.stringify({ result, type: 'migration_lock_released' }));
    return;
  }
  if (command === 'startup') {
    await pool.query('SELECT 1');
    const client = createElasticsearchClient();
    for (const entity of FTS_SEARCH_DOCUMENT_ENTITIES) {
      assertFtsSearchStartupGenerationSafe(
        await describeEntityGeneration({ client, entity, namespace, readCheckpoint }),
      );
    }
  }
  return lock.withLock(command, async (handle) => {
    assertMigrationOwnership = () => lock.assertOwner(handle);
    try {
      return await run();
    } finally {
      assertMigrationOwnership = undefined;
    }
  });
};

observeFtsSearchReindexRun(runWithMigrationLock)
  .catch(async (error) => {
    const operationError =
      error instanceof FtsSearchMigrationLockError && error.cause !== undefined
        ? error.cause
        : error;
    const rootError =
      operationError instanceof FtsSearchReindexEntityError ? operationError.cause : operationError;
    logErrorSummary('❌ Elasticsearch reindex failed:', rootError);
    if (operationError !== error) logErrorSummary('Migration lock recovery:', error);
    if (auditLogger) {
      const failure = {
        elapsedMs: Date.now() - executionStartedAt,
        entity:
          operationError instanceof FtsSearchReindexEntityError ? operationError.entity : null,
        errorSummary: summarizeFtsSearchReindexError(rootError),
        errorType: rootError instanceof Error ? rootError.name.slice(0, 128) : 'UnknownError',
        type: 'session_failed' as const,
      };
      await auditLogger
        .append(failure)
        .catch((logError) => logErrorSummary('Failed to append reindex audit event:', logError));
      const currentStatus = await readStatus().catch((statusError) => {
        logErrorSummary('Failed to read reindex status after failure:', statusError);
        return null;
      });
      await auditLogger
        .writeSummary({ failure, status: currentStatus })
        .catch((logError) => logErrorSummary('Failed to write reindex audit summary:', logError));
    }
    process.exitCode = 1;
  })
  .finally(async () => {
    try {
      await pool.end();
    } finally {
      /** Flush terminal run metrics and the root trace before the short-lived CLI exits. */
      if (telemetrySdk) await shutdownSafely(telemetrySdk);
    }
  });