|
| 1 | +// ───────────────────────────────────────────────────────────────────────────── |
| 2 | +// Drift detection — Stack G |
| 3 | +// |
| 4 | +// Before each PATCH, GET the current platform payload, hash it, and compare |
| 5 | +// to the `lastPulledHash` recorded in state. If the hashes differ, the |
| 6 | +// dashboard has drifted away from the version we last pulled — refuse to |
| 7 | +// push without `--overwrite` (improvements.md #1, #2, #7). |
| 8 | +// |
| 9 | +// Behavior matrix: |
| 10 | +// - No `lastPulledHash` (e.g., legacy state, first push after Stack F): |
| 11 | +// log "drift unknown — proceeding" and continue. Don't block. |
| 12 | +// - Hashes match: continue silently. |
| 13 | +// - Hashes differ + no --overwrite: refuse the push, return false. |
| 14 | +// - Hashes differ + --overwrite: log "overwriting drift" and continue. |
| 15 | +// |
| 16 | +// The check fires GET against the same endpoint the apply function would |
| 17 | +// PATCH. We don't centralize it inside `vapiRequest` because POST (create) |
| 18 | +// has nothing to compare against — only PATCH (update) is drift-sensitive. |
| 19 | +// ───────────────────────────────────────────────────────────────────────────── |
| 20 | + |
| 21 | +import { hashPayload } from "./state-serialize.ts"; |
| 22 | +import { VAPI_BASE_URL, VAPI_TOKEN } from "./config.ts"; |
| 23 | +import type { ResourceState } from "./types.ts"; |
| 24 | + |
| 25 | +export interface DriftCheckResult { |
| 26 | + ok: boolean; |
| 27 | + reason: "no-baseline" | "match" | "drift-overwritten" | "drift-blocked"; |
| 28 | + message?: string; |
| 29 | + // Hash of the *current* platform payload — caller may want to update |
| 30 | + // state's `lastPulledHash` after a successful push so subsequent pushes |
| 31 | + // start from the platform's current state, not the stale pre-overwrite hash. |
| 32 | + platformHash?: string; |
| 33 | +} |
| 34 | + |
| 35 | +async function fetchPlatformPayload( |
| 36 | + endpoint: string, |
| 37 | +): Promise<unknown | null> { |
| 38 | + // GET against the same path the PATCH would target. 404 means the resource |
| 39 | + // was deleted on the dashboard — let the upsert path handle it (the existing |
| 40 | + // 404 → "stale mapping, drop and skip" recovery in |
| 41 | + // upsertResourceWithStateRecovery covers this case). |
| 42 | + const response = await fetch(`${VAPI_BASE_URL}${endpoint}`, { |
| 43 | + method: "GET", |
| 44 | + headers: { Authorization: `Bearer ${VAPI_TOKEN}` }, |
| 45 | + }); |
| 46 | + if (response.status === 404) return null; |
| 47 | + if (!response.ok) { |
| 48 | + const text = await response.text(); |
| 49 | + throw new Error(`Drift GET ${endpoint} → ${response.status}: ${text}`); |
| 50 | + } |
| 51 | + return response.json(); |
| 52 | +} |
| 53 | + |
| 54 | +// Strip server-managed fields before hashing so the platform's payload hash |
| 55 | +// matches the last-pulled-hash basis (which excluded them via cleanResource). |
| 56 | +const SERVER_FIELDS = new Set([ |
| 57 | + "id", |
| 58 | + "orgId", |
| 59 | + "createdAt", |
| 60 | + "updatedAt", |
| 61 | + "analyticsMetadata", |
| 62 | + "isDeleted", |
| 63 | + "isServerUrlSecretSet", |
| 64 | + "workflowIds", |
| 65 | +]); |
| 66 | + |
| 67 | +function stripServerFields(payload: unknown): unknown { |
| 68 | + if (!payload || typeof payload !== "object" || Array.isArray(payload)) { |
| 69 | + return payload; |
| 70 | + } |
| 71 | + const out: Record<string, unknown> = {}; |
| 72 | + for (const [k, v] of Object.entries(payload as Record<string, unknown>)) { |
| 73 | + if (!SERVER_FIELDS.has(k)) out[k] = v; |
| 74 | + } |
| 75 | + return out; |
| 76 | +} |
| 77 | + |
| 78 | +export async function checkDriftForUpdate(options: { |
| 79 | + endpoint: string; // e.g. "/assistant/<uuid>" |
| 80 | + resourceLabel: string; // for log lines |
| 81 | + resourceId: string; // local resource id |
| 82 | + state: ResourceState; |
| 83 | + overwrite: boolean; |
| 84 | +}): Promise<DriftCheckResult> { |
| 85 | + const { endpoint, resourceLabel, resourceId, state, overwrite } = options; |
| 86 | + |
| 87 | + if (!state.lastPulledHash) { |
| 88 | + return { |
| 89 | + ok: true, |
| 90 | + reason: "no-baseline", |
| 91 | + message: |
| 92 | + ` ⚠️ drift check skipped for ${resourceLabel} ${resourceId}: ` + |
| 93 | + `no lastPulledHash in state. Run \`npm run pull\` to establish a baseline.`, |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + const remote = await fetchPlatformPayload(endpoint); |
| 98 | + if (remote === null) { |
| 99 | + // Resource was deleted on the dashboard — defer to the upsert recovery |
| 100 | + // path. Drift is not the right framing here. |
| 101 | + return { ok: true, reason: "no-baseline" }; |
| 102 | + } |
| 103 | + |
| 104 | + const platformHash = hashPayload(stripServerFields(remote)); |
| 105 | + if (platformHash === state.lastPulledHash) { |
| 106 | + return { ok: true, reason: "match", platformHash }; |
| 107 | + } |
| 108 | + |
| 109 | + if (overwrite) { |
| 110 | + return { |
| 111 | + ok: true, |
| 112 | + reason: "drift-overwritten", |
| 113 | + platformHash, |
| 114 | + message: |
| 115 | + ` ⚠️ drift on ${resourceLabel} ${resourceId}: platform changed since last pull, ` + |
| 116 | + `overwriting (--overwrite).`, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + ok: false, |
| 122 | + reason: "drift-blocked", |
| 123 | + platformHash, |
| 124 | + message: |
| 125 | + ` ❌ drift detected on ${resourceLabel} ${resourceId}: ` + |
| 126 | + `platform hash (${platformHash.slice(0, 8)}...) differs from last-pulled ` + |
| 127 | + `(${state.lastPulledHash.slice(0, 8)}...). ` + |
| 128 | + `Re-run pull, resolve locally, or push with --overwrite to take ownership.`, |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +// Re-export the pure helper from state-serialize so call sites can import |
| 133 | +// from drift.ts but tests can import the pure version directly. |
| 134 | +export { checkPronunciationDictDrop } from "./state-serialize.ts"; |
0 commit comments