|
| 1 | +/** |
| 2 | + * oxlint plugin: trigger-runops — fast in-editor fences for the run-ops DB split. |
| 3 | + * Name-based ports of two detectors from the authoritative type-aware guard |
| 4 | + * (apps/webapp/scripts/runOpsLegacyGuard.ts, run as `--check` in CI). Scoped to |
| 5 | + * apps/webapp/app via .oxlintrc.json overrides, matching the guard's scope. |
| 6 | + */ |
| 7 | + |
| 8 | +// The 16 run-graph delegates (camelCased run-ops models). Authoritative source is |
| 9 | +// internal-packages/run-ops-database/prisma/schema.prisma, which the tsx guard |
| 10 | +// parses; kept as a literal here so a missing schema can never disable linting. |
| 11 | +const RUN_GRAPH_DELEGATES = new Set([ |
| 12 | + "taskRun", |
| 13 | + "taskRunExecutionSnapshot", |
| 14 | + "taskRunCheckpoint", |
| 15 | + "waitpoint", |
| 16 | + "taskRunWaitpoint", |
| 17 | + "waitpointRunConnection", |
| 18 | + "completedWaitpoint", |
| 19 | + "waitpointTag", |
| 20 | + "taskRunTag", |
| 21 | + "taskRunDependency", |
| 22 | + "taskRunAttempt", |
| 23 | + "batchTaskRun", |
| 24 | + "batchTaskRunItem", |
| 25 | + "batchTaskRunError", |
| 26 | + "checkpoint", |
| 27 | + "checkpointRestoreEvent", |
| 28 | +]); |
| 29 | + |
| 30 | +// Global control-plane client exports (from ~/db.server). |
| 31 | +const CONTROL_PLANE_GLOBALS = new Set(["prisma", "$replica"]); |
| 32 | + |
| 33 | +// Read-through config slots that MUST carry a run-ops client. Kept in sync with |
| 34 | +// RUN_OPS_READTHROUGH_SLOTS in scripts/runOpsLegacyGuard.ts. `controlPlaneReplica` |
| 35 | +// is intentionally absent — it is meant to be control-plane. |
| 36 | +const RUN_OPS_READTHROUGH_SLOTS = new Set([ |
| 37 | + "newClient", |
| 38 | + "newReplica", |
| 39 | + "runOpsNew", |
| 40 | + "legacyReplica", |
| 41 | + "runOpsLegacyReplica", |
| 42 | +]); |
| 43 | +const CONTROL_PLANE_CLIENT_IDENTIFIERS = new Set(["$replica", "prisma"]); |
| 44 | +const CONTROL_PLANE_THIS_FIELDS = new Set(["_replica", "_prisma"]); |
| 45 | + |
| 46 | +// Parentheses are not nodes in oxlint's ESTree, so only type-only wrappers unwrap. |
| 47 | +function unwrap(node) { |
| 48 | + let current = node; |
| 49 | + while ( |
| 50 | + current && |
| 51 | + (current.type === "TSAsExpression" || |
| 52 | + current.type === "TSSatisfiesExpression" || |
| 53 | + current.type === "TSNonNullExpression") |
| 54 | + ) { |
| 55 | + current = current.expression; |
| 56 | + } |
| 57 | + return current; |
| 58 | +} |
| 59 | + |
| 60 | +function valueIsControlPlaneClient(node) { |
| 61 | + const expr = unwrap(node); |
| 62 | + if (!expr) return false; |
| 63 | + if (expr.type === "Identifier") return CONTROL_PLANE_CLIENT_IDENTIFIERS.has(expr.name); |
| 64 | + if ( |
| 65 | + expr.type === "MemberExpression" && |
| 66 | + !expr.computed && |
| 67 | + expr.object.type === "ThisExpression" && |
| 68 | + expr.property.type === "Identifier" |
| 69 | + ) { |
| 70 | + return CONTROL_PLANE_THIS_FIELDS.has(expr.property.name); |
| 71 | + } |
| 72 | + return false; |
| 73 | +} |
| 74 | + |
| 75 | +function propertyKeyName(property) { |
| 76 | + const key = property.key; |
| 77 | + if (!key) return undefined; |
| 78 | + if (key.type === "Identifier" && !property.computed) return key.name; |
| 79 | + if (key.type === "Literal" && typeof key.value === "string") return key.value; |
| 80 | + return undefined; |
| 81 | +} |
| 82 | + |
| 83 | +/** @type {import("eslint").Rule.RuleModule} */ |
| 84 | +const noControlPlaneRunGraphAccess = { |
| 85 | + meta: { |
| 86 | + type: "problem", |
| 87 | + docs: { |
| 88 | + description: |
| 89 | + "Disallow reaching a run-graph table through the global control-plane Prisma client; route through the RunStore.", |
| 90 | + }, |
| 91 | + messages: { |
| 92 | + leak: 'Run-graph table "{{delegate}}" is reached through the control-plane client "{{client}}". Once run-ops is a separate database this reads/writes the wrong DB — route through the RunStore instead.', |
| 93 | + }, |
| 94 | + schema: [], |
| 95 | + }, |
| 96 | + create(context) { |
| 97 | + return { |
| 98 | + MemberExpression(node) { |
| 99 | + if (node.computed || node.property.type !== "Identifier") return; |
| 100 | + if (!RUN_GRAPH_DELEGATES.has(node.property.name)) return; |
| 101 | + if (node.object.type !== "Identifier" || !CONTROL_PLANE_GLOBALS.has(node.object.name)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + context.report({ |
| 105 | + node, |
| 106 | + messageId: "leak", |
| 107 | + data: { delegate: node.property.name, client: node.object.name }, |
| 108 | + }); |
| 109 | + }, |
| 110 | + }; |
| 111 | + }, |
| 112 | +}; |
| 113 | + |
| 114 | +/** @type {import("eslint").Rule.RuleModule} */ |
| 115 | +const noControlPlaneInRunOpsSlot = { |
| 116 | + meta: { |
| 117 | + type: "problem", |
| 118 | + docs: { |
| 119 | + description: |
| 120 | + "Disallow assigning a control-plane Prisma client into a run-ops read-through config slot (routes run-ops reads at the wrong database).", |
| 121 | + }, |
| 122 | + messages: { |
| 123 | + misroute: |
| 124 | + 'Run-ops read-through slot "{{slot}}" is assigned the control-plane client "{{client}}". This routes run-ops reads at the control-plane database. Assign the run-ops client instead.', |
| 125 | + }, |
| 126 | + schema: [], |
| 127 | + }, |
| 128 | + create(context) { |
| 129 | + return { |
| 130 | + Property(node) { |
| 131 | + const slot = propertyKeyName(node); |
| 132 | + if (!slot || !RUN_OPS_READTHROUGH_SLOTS.has(slot)) return; |
| 133 | + if (node.shorthand) return; // `{ legacyReplica }` binds a same-named local, not a cp export. |
| 134 | + if (!valueIsControlPlaneClient(node.value)) return; |
| 135 | + const client = |
| 136 | + node.value.type === "Identifier" |
| 137 | + ? node.value.name |
| 138 | + : context.sourceCode.getText(unwrap(node.value)); |
| 139 | + context.report({ node: node.value, messageId: "misroute", data: { slot, client } }); |
| 140 | + }, |
| 141 | + }; |
| 142 | + }, |
| 143 | +}; |
| 144 | + |
| 145 | +/** @type {import("eslint").ESLint.Plugin} */ |
| 146 | +const plugin = { |
| 147 | + // Distinct namespace: oxlint keys plugins by meta.name, so this cannot reuse |
| 148 | + // "trigger" without clobbering no-thrown-unawaited-redirect. |
| 149 | + meta: { name: "trigger-runops" }, |
| 150 | + rules: { |
| 151 | + "no-control-plane-run-graph-access": noControlPlaneRunGraphAccess, |
| 152 | + "no-control-plane-in-runops-slot": noControlPlaneInRunOpsSlot, |
| 153 | + }, |
| 154 | +}; |
| 155 | + |
| 156 | +export default plugin; |
0 commit comments