|
| 1 | +/** |
| 2 | + * Reconcile plugin. |
| 3 | + * |
| 4 | + * Replaces the data-repo reconciliation that used to live in |
| 5 | + * `deploy/docker/entrypoint.sh`. Registered AFTER `storePlugin` (so the |
| 6 | + * repository handle is available) and BEFORE `servicesPlugin` (so the |
| 7 | + * in-memory state is built from the post-reconciliation tree). |
| 8 | + * |
| 9 | + * Behavior: |
| 10 | + * - When `CFP_DATA_REMOTE` is unset, reconciliation is skipped entirely |
| 11 | + * (typical for local dev against a sibling working tree with no remote). |
| 12 | + * - Otherwise: calls `reconcileDataRepo` for the configured branch and |
| 13 | + * logs the outcome at the appropriate level: |
| 14 | + * - 'conflict-escaped' → ERROR with the `conflictBranch` field, so |
| 15 | + * operators see a loud line in production logs. |
| 16 | + * - 'fetch-failed' → WARN — non-fatal, the API still boots from |
| 17 | + * local state. |
| 18 | + * - everything else → INFO. |
| 19 | + * - Any other thrown error (corrupt repo, missing branch, etc.) propagates |
| 20 | + * and crashes the boot. k8s will restart the pod and the entrypoint will |
| 21 | + * re-clone if needed. |
| 22 | + * |
| 23 | + * Decorates Fastify with: |
| 24 | + * - `dataRepoLock` — a single-slot async lock callers use to serialize |
| 25 | + * non-`store.transact` git operations (boot reconcile, future webhook). |
| 26 | + * - `reconcileDataRepo({ branch })` — a thin wrapper that acquires the |
| 27 | + * lock and invokes the state-machine function with the current |
| 28 | + * environment. Provided so the future hot-reload webhook (#65) has a |
| 29 | + * single call to make. |
| 30 | + */ |
| 31 | +import type { FastifyInstance } from 'fastify'; |
| 32 | +import fp from 'fastify-plugin'; |
| 33 | + |
| 34 | +import { createDataRepoLock, type DataRepoLock } from '../lib/data-repo-lock.js'; |
| 35 | +import { reconcileDataRepo, type ReconcileResult } from '../store/reconcile.js'; |
| 36 | + |
| 37 | +declare module 'fastify' { |
| 38 | + interface FastifyInstance { |
| 39 | + /** |
| 40 | + * Acquire the data-repo lock. Returns a release function; release is |
| 41 | + * idempotent. See `lib/data-repo-lock.ts` for the contract. |
| 42 | + */ |
| 43 | + dataRepoLock: DataRepoLock; |
| 44 | + /** |
| 45 | + * Reconcile the local working tree against `CFP_DATA_REMOTE` for the |
| 46 | + * given branch under the data-repo lock. Defaults to the configured |
| 47 | + * `CFP_DATA_BRANCH`. |
| 48 | + * |
| 49 | + * Returns the outcome envelope. Throws on unrecoverable filesystem / |
| 50 | + * git errors; soft failures (fetch blip, conflict-escape) return a |
| 51 | + * non-throwing result. |
| 52 | + */ |
| 53 | + reconcileDataRepo: (opts?: { branch?: string }) => Promise<ReconcileResult>; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function reconcilePlugin(fastify: FastifyInstance): Promise<void> { |
| 58 | + const lock = createDataRepoLock(); |
| 59 | + fastify.decorate('dataRepoLock', lock); |
| 60 | + |
| 61 | + const repoPath = fastify.config.CFP_DATA_REPO_PATH; |
| 62 | + const configuredBranch = fastify.config.CFP_DATA_BRANCH; |
| 63 | + const remote = fastify.config.CFP_DATA_REMOTE; |
| 64 | + |
| 65 | + // Expose a Fastify-bound wrapper so the future webhook handler (#65) has |
| 66 | + // a single call to make. Always under the lock. |
| 67 | + fastify.decorate( |
| 68 | + 'reconcileDataRepo', |
| 69 | + async (opts?: { branch?: string }): Promise<ReconcileResult> => { |
| 70 | + const branch = opts?.branch ?? configuredBranch; |
| 71 | + if (!branch) { |
| 72 | + throw new Error( |
| 73 | + 'reconcileDataRepo: no branch specified and CFP_DATA_BRANCH is unset', |
| 74 | + ); |
| 75 | + } |
| 76 | + const release = await lock(); |
| 77 | + try { |
| 78 | + return await reconcileDataRepo({ |
| 79 | + repoPath, |
| 80 | + branch, |
| 81 | + logger: fastify.log, |
| 82 | + }); |
| 83 | + } finally { |
| 84 | + release(); |
| 85 | + } |
| 86 | + }, |
| 87 | + ); |
| 88 | + |
| 89 | + // Boot-time reconcile: skipped when no remote is configured (dev). |
| 90 | + if (!remote) { |
| 91 | + fastify.log.info( |
| 92 | + 'data-repo reconciliation skipped: CFP_DATA_REMOTE unset (dev mode)', |
| 93 | + ); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (!configuredBranch) { |
| 98 | + // Without a branch, we don't know what to reconcile against. Treat as |
| 99 | + // a configuration error — entrypoint should set CFP_DATA_BRANCH |
| 100 | + // alongside CFP_DATA_REMOTE. |
| 101 | + throw new Error( |
| 102 | + 'data-repo reconciliation: CFP_DATA_REMOTE set but CFP_DATA_BRANCH unset; refusing to guess', |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + const release = await lock(); |
| 107 | + let result: ReconcileResult; |
| 108 | + try { |
| 109 | + result = await reconcileDataRepo({ |
| 110 | + repoPath, |
| 111 | + branch: configuredBranch, |
| 112 | + logger: fastify.log, |
| 113 | + }); |
| 114 | + } finally { |
| 115 | + release(); |
| 116 | + } |
| 117 | + |
| 118 | + // Outcome-specific logging so operators get an at-a-glance line in prod. |
| 119 | + switch (result.outcome) { |
| 120 | + case 'conflict-escaped': |
| 121 | + // LOUD: the operator MUST investigate the named branch. |
| 122 | + fastify.log.error( |
| 123 | + { |
| 124 | + branch: configuredBranch, |
| 125 | + conflictBranch: result.conflictBranch, |
| 126 | + oldCommit: result.oldCommit, |
| 127 | + newCommit: result.newCommit, |
| 128 | + ahead: result.ahead, |
| 129 | + behind: result.behind, |
| 130 | + }, |
| 131 | + 'data-repo reconciliation invoked conflict escape hatch', |
| 132 | + ); |
| 133 | + break; |
| 134 | + case 'fetch-failed': |
| 135 | + fastify.log.warn( |
| 136 | + { branch: configuredBranch, commit: result.oldCommit }, |
| 137 | + 'data-repo reconciliation: fetch failed; continuing with local state', |
| 138 | + ); |
| 139 | + break; |
| 140 | + case 'in-sync': |
| 141 | + case 'fast-forwarded': |
| 142 | + case 'pushed-ahead': |
| 143 | + case 'rebased': |
| 144 | + fastify.log.info( |
| 145 | + { |
| 146 | + branch: configuredBranch, |
| 147 | + outcome: result.outcome, |
| 148 | + oldCommit: result.oldCommit, |
| 149 | + newCommit: result.newCommit, |
| 150 | + ahead: result.ahead, |
| 151 | + behind: result.behind, |
| 152 | + }, |
| 153 | + 'data-repo reconciled', |
| 154 | + ); |
| 155 | + break; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export default fp(reconcilePlugin, { |
| 160 | + name: 'reconcile', |
| 161 | + fastify: '5.x', |
| 162 | + dependencies: ['store'], |
| 163 | +}); |
0 commit comments