@everystack/cli 0.4.45 → 0.4.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/package.json +2 -2
  2. package/src/cli/alter-type-dependents.ts +96 -0
  3. package/src/cli/apply-execute.ts +22 -8
  4. package/src/cli/authz-adoption-class.ts +75 -26
  5. package/src/cli/authz-canonical.ts +37 -5
  6. package/src/cli/authz-compile.ts +87 -37
  7. package/src/cli/authz-contract.ts +92 -19
  8. package/src/cli/authz-derive.ts +158 -33
  9. package/src/cli/authz-reconcile.ts +48 -6
  10. package/src/cli/aws.ts +32 -0
  11. package/src/cli/commands/db-apply.ts +60 -14
  12. package/src/cli/commands/db-authz.ts +9 -14
  13. package/src/cli/commands/db-fingerprint.ts +54 -18
  14. package/src/cli/commands/db-generate.ts +11 -17
  15. package/src/cli/commands/db-plan.ts +56 -8
  16. package/src/cli/commands/db-pull.ts +16 -18
  17. package/src/cli/commands/db-reconcile.ts +18 -20
  18. package/src/cli/commands/db-swap.ts +5 -4
  19. package/src/cli/commands/db-sync.ts +8 -5
  20. package/src/cli/db-build.ts +2 -2
  21. package/src/cli/db-source.ts +56 -0
  22. package/src/cli/derived-introspect.ts +27 -26
  23. package/src/cli/derived-lint.ts +7 -8
  24. package/src/cli/edge-plan.ts +112 -16
  25. package/src/cli/git-descent.ts +16 -9
  26. package/src/cli/index.ts +1 -1
  27. package/src/cli/model-render.ts +56 -50
  28. package/src/cli/schema-compile.ts +6 -1
  29. package/src/cli/schema-diff.ts +1 -1
  30. package/src/cli/schema-fingerprint.ts +67 -7
  31. package/src/cli/schema-introspect.ts +44 -17
  32. package/src/cli/schema-source.ts +9 -0
  33. package/src/cli/session.ts +184 -0
  34. package/src/cli/stage-read-consistency.ts +128 -0
  35. package/src/cli/state-apply.ts +4 -2
  36. package/src/cli/swap-execute.ts +4 -3
  37. package/src/cli/search-path.ts +0 -51
@@ -48,6 +48,7 @@ import { fingerprintModels } from '../schema-fingerprint.js';
48
48
  import { compileDrizzleSource } from '../schema-source.js';
49
49
  import type { SourceFile } from '../derived-source.js';
50
50
  import { resolveDbSource, createUrlRunner, connectingVia, type DbSource } from '../db-source.js';
51
+ import { borrowedSessionRunner, type SessionRunner } from '../session.js';
51
52
  import { planBackfills, readBackfillLog } from '../backfill.js';
52
53
  import { resolveModelsPath } from '../models-path.js';
53
54
  import { executeReconcile, buildReconcileReport, type ReconcileRun } from './db-reconcile.js';
@@ -113,26 +114,27 @@ export interface SyncRun {
113
114
  */
114
115
  export async function executeSync(
115
116
  runner: QueryRunner,
117
+ session: SessionRunner,
116
118
  models: ModelDescriptor[],
117
119
  options: SyncOptions = {},
118
120
  hooks: SyncHooks = {},
119
121
  ): Promise<SyncRun> {
120
- const current = await introspectSchema(runner);
121
- const liveAuthz = await introspectContract(runner, contractFunctionRow, FUNCTIONS_SQL);
122
+ const current = await introspectSchema(session);
123
+ const liveAuthz = await introspectContract(session, contractFunctionRow, FUNCTIONS_SQL);
122
124
  const statements = generateMigrationSql(models, current, {
123
125
  allowDrops: options.allowDrops, liveAuthz, sequences: options.sequences,
124
126
  governedRoles: options.governedRoles,
125
127
  });
126
128
  hooks.onStatePlan?.(statements, classifyGeneratedStatements(statements));
127
129
 
128
- const state = await applyStateAndVerify(runner, models, statements, current, liveAuthz, {
130
+ const state = await applyStateAndVerify(runner, session, models, statements, current, liveAuthz, {
129
131
  allowDrops: options.allowDrops, sequences: options.sequences,
130
132
  actor: options.actor, gitRef: options.gitRef, now: options.now,
131
133
  });
132
134
  hooks.onStateDone?.(state);
133
135
 
134
136
  // The declared derived layer is the one compute stream; nothing declared = skipped.
135
- const compute = !options.declared?.length ? null : await executeReconcile(runner, {
137
+ const compute = !options.declared?.length ? null : await executeReconcile(runner, session, {
136
138
  apply: true,
137
139
  baseline: options.baseline,
138
140
  rebaseline: options.rebaseline,
@@ -303,11 +305,12 @@ export async function dbSyncCommand(flags: Record<string, string>): Promise<void
303
305
  }
304
306
 
305
307
  step(connectingVia(dbSource));
306
- const { runner, end } = await createUrlRunner(dbSource.url);
308
+ const { runner, session, end } = await createUrlRunner(dbSource.url);
307
309
 
308
310
  try {
309
311
  const run = await executeSync(
310
312
  runner,
313
+ session,
311
314
  models,
312
315
  {
313
316
  declared: declaredDb?.objects,
@@ -107,10 +107,10 @@ export async function buildIntoDatabase(
107
107
  models: ModelDescriptor[],
108
108
  options: BuildOptions = {},
109
109
  ): Promise<BuildResult> {
110
- const { runner, end } = await createUrlRunner(url);
110
+ const { runner, session, end } = await createUrlRunner(url);
111
111
  try {
112
112
  const createdRoles = await ensureContractRoles(runner, models);
113
- const run = await executeSync(runner, models, {
113
+ const run = await executeSync(runner, session, models, {
114
114
  declared: options.declared,
115
115
  sequences: options.sequences,
116
116
  actor: options.actor ?? 'db-build',
@@ -25,6 +25,7 @@
25
25
  */
26
26
 
27
27
  import type { QueryRunner } from './authz-contract.js';
28
+ import { buildSearchPathSql, type SessionRunner, type SessionResult } from './session.js';
28
29
 
29
30
  export type DbSource =
30
31
  | { kind: 'url'; url: string; from: 'flag' | 'env' | 'admin-env' | 'operator' }
@@ -65,10 +66,61 @@ export function connectingVia(source: Extract<DbSource, { kind: 'url' }>): strin
65
66
 
66
67
  export interface UrlRunner {
67
68
  runner: QueryRunner;
69
+ /**
70
+ * The direct lane's `SessionRunner` — N statements in ONE postgres.js transaction on
71
+ * the single connection. The stage lane's twin is `lambdaSessionRunner` (one invoke,
72
+ * one container); both honor the same ordering, `SET LOCAL` and `allowFailure` rules,
73
+ * so a caller typed against `SessionRunner` reads identically on either venue.
74
+ */
75
+ session: SessionRunner;
68
76
  /** Close the client so the process can exit cleanly. */
69
77
  end: () => Promise<void>;
70
78
  }
71
79
 
80
+ /**
81
+ * Build a `SessionRunner` over a postgres.js client whose pool is a single connection.
82
+ *
83
+ * `sql.begin` holds that one connection for the whole callback, so every statement here
84
+ * shares a session by construction. The `searchPath` pin is `SET LOCAL` (it reverts with
85
+ * the transaction, never outliving its command) and consumes no result slot.
86
+ *
87
+ * An `allowFailure` statement rides `tx.savepoint`, the DRIVER's savepoint — not a
88
+ * hand-issued `SAVEPOINT` / `ROLLBACK TO SAVEPOINT` pair. postgres.js records any query
89
+ * error raised inside a transaction scope and re-throws it after the callback returns, so
90
+ * catching the error ourselves recovers the database and still loses the session. Its
91
+ * savepoint opens a nested scope with its own error bookkeeping, which is the only shape
92
+ * that survives. (Proven on a live PostgreSQL: the hand-rolled version passed against a
93
+ * fake and failed against the driver.)
94
+ */
95
+ export function sessionRunnerOver(sql: any): SessionRunner {
96
+ return async (statements, opts) => {
97
+ const stmts = statements.map((s) => (typeof s === 'string' ? { sql: s } : s));
98
+ const results: SessionResult[] = [];
99
+ await sql.begin(async (tx: any) => {
100
+ if (opts?.isolation) await tx.unsafe(`SET TRANSACTION ISOLATION LEVEL ${opts.isolation}`);
101
+ if (opts?.readOnly) await tx.unsafe('SET TRANSACTION READ ONLY');
102
+ if (opts?.searchPath !== undefined) {
103
+ await tx.unsafe(buildSearchPathSql(String(opts.searchPath)));
104
+ }
105
+ for (const stmt of stmts) {
106
+ if (!stmt.allowFailure) {
107
+ results.push(Array.from(await tx.unsafe(stmt.sql)));
108
+ continue;
109
+ }
110
+ try {
111
+ results.push(
112
+ await tx.savepoint(async (sp: any) => Array.from(await sp.unsafe(stmt.sql))),
113
+ );
114
+ } catch (err: any) {
115
+ results.push({ everystackSessionError: true, message: err?.message || String(err) });
116
+ }
117
+ }
118
+ });
119
+ return results;
120
+ };
121
+ }
122
+
123
+
72
124
  /**
73
125
  * Load the postgres.js driver, or explain how to get it. Shared by every direct-connection
74
126
  * runner below so the missing-driver instructions can never drift between them.
@@ -101,6 +153,7 @@ export async function createUrlRunner(
101
153
  const sql = postgres(url, { max: 1, max_lifetime: null, onnotice: () => {}, ...sslDefaults(url) });
102
154
  return {
103
155
  runner: async (query: string) => Array.from(await sql.unsafe(query)),
156
+ session: sessionRunnerOver(sql),
104
157
  end: () => sql.end({ timeout: 5 }),
105
158
  };
106
159
  }
@@ -135,6 +188,8 @@ export async function createUrlPipelineRunner(
135
188
  export interface UrlProbeRunner {
136
189
  /** Read-only introspection, for the contract pull/diff. */
137
190
  runner: QueryRunner;
191
+ /** N statements on this one connection in one transaction — see sessionRunnerOver. */
192
+ session: SessionRunner;
138
193
  /** The self-reverting red-team probe. See `probe` below. */
139
194
  probe: (setup: string, read: string) => Promise<any[]>;
140
195
  /** Close the client so the process can exit cleanly. */
@@ -166,6 +221,7 @@ export async function createUrlProbeRunner(
166
221
  const sql = postgres(url, { max: 1, max_lifetime: null, onnotice: () => {}, ...sslDefaults(url) });
167
222
  return {
168
223
  runner: async (query: string) => Array.from(await sql.unsafe(query)),
224
+ session: sessionRunnerOver(sql),
169
225
  probe: async (setup: string, read: string) => {
170
226
  let rows: any[] = [];
171
227
  try {
@@ -20,7 +20,7 @@
20
20
  import { createHash } from 'node:crypto';
21
21
  import { IGNORED_SCHEMAS, type QueryRunner } from './authz-contract.js';
22
22
  import { normalizeSql, type DerivedKind } from './derived-source.js';
23
- import { withCanonicalSearchPath } from './search-path.js';
23
+ import { INTROSPECTION_SESSION, rowsOrEmpty, type SessionRunner } from './session.js';
24
24
 
25
25
  export interface LiveObject {
26
26
  kind: DerivedKind;
@@ -591,30 +591,31 @@ export function assembleDerivedCatalog(rows: DerivedRows): DerivedCatalog {
591
591
  * injected runner; a missing provenance table (first run against a database
592
592
  * the reconciler has never touched) folds to an empty claims list.
593
593
  */
594
- export async function introspectDerived(run: QueryRunner): Promise<DerivedCatalog> {
595
- // View/matview/function bodies deparse relative to the search_path (`pg_get_viewdef`
596
- // renders a ref bare while its schema is in scope, qualified once off). Pin the
597
- // canonical baseline so the recorded def hash is stable across runs and paths this
598
- // also subsumes the reconcile apply's pre-introspection reset. See search-path.ts.
599
- return withCanonicalSearchPath(run, async () => {
600
- const [relations, functions, indexes, depends, triggers, grants] = await Promise.all([
601
- run(DERIVED_RELATIONS_SQL), run(DERIVED_FUNCTIONS_SQL), run(MATVIEW_INDEXES_SQL), run(DERIVED_DEPENDS_SQL),
602
- run(DERIVED_TRIGGERS_SQL), run(DERIVED_GRANTS_SQL),
603
- ]);
604
- let provenance: ProvenanceRawRow[] = [];
605
- try {
606
- provenance = (await run(PROVENANCE_SQL)) as ProvenanceRawRow[];
607
- } catch {
608
- // everystack.derived_provenance does not exist yet — nothing has been reconciled.
609
- }
610
- return assembleDerivedCatalog({
611
- relations: relations as RelationRow[],
612
- functions: functions as FunctionRow[],
613
- indexes: indexes as IndexDefRow[],
614
- depends: depends as DependsRow[],
615
- triggers: triggers as TriggerRow[],
616
- grants: grants as GrantAclRow[],
617
- provenance,
618
- });
594
+ export async function introspectDerived(session: SessionRunner): Promise<DerivedCatalog> {
595
+ // ONE session: view/matview/function bodies deparse relative to the search_path
596
+ // (`pg_get_viewdef` renders a ref bare while its schema is in scope, qualified once off),
597
+ // so the recorded def hash is only stable if every body is captured under one pinned path
598
+ // at one moment.
599
+ //
600
+ // The provenance read is `allowFailure`: `everystack.derived_provenance` does not exist
601
+ // until something has been reconciled, and its absence is an ANSWER (no claims), not a
602
+ // failure. Before the session it was a try/catch, which is the same intent expressed the
603
+ // only way a one-statement-at-a-time runner could — by being willing to lose the session.
604
+ const [relations, functions, indexes, depends, triggers, grants, provenance] = await session(
605
+ [
606
+ DERIVED_RELATIONS_SQL, DERIVED_FUNCTIONS_SQL, MATVIEW_INDEXES_SQL, DERIVED_DEPENDS_SQL,
607
+ DERIVED_TRIGGERS_SQL, DERIVED_GRANTS_SQL,
608
+ { sql: PROVENANCE_SQL, allowFailure: true },
609
+ ],
610
+ INTROSPECTION_SESSION,
611
+ );
612
+ return assembleDerivedCatalog({
613
+ relations: relations as RelationRow[],
614
+ functions: functions as FunctionRow[],
615
+ indexes: indexes as IndexDefRow[],
616
+ depends: depends as DependsRow[],
617
+ triggers: triggers as TriggerRow[],
618
+ grants: grants as GrantAclRow[],
619
+ provenance: rowsOrEmpty(provenance) as ProvenanceRawRow[],
619
620
  });
620
621
  }
@@ -26,9 +26,10 @@
26
26
  * grant.
27
27
  */
28
28
 
29
+ import { isColumnAbility } from '@everystack/model';
29
30
  import type {
30
31
  ModelDescriptor, DerivedDescriptor, ViewDescriptor, MaterializedViewDescriptor,
31
- FunctionDescriptor, DependsOnRef, Ability,
32
+ FunctionDescriptor, DependsOnRef,
32
33
  } from '@everystack/model';
33
34
  import { parseQualified } from './derived-source.js';
34
35
 
@@ -47,11 +48,6 @@ function isModelRef(ref: DependsOnRef): ref is ModelDescriptor {
47
48
  return 'table' in ref;
48
49
  }
49
50
 
50
- /** The column-scoped self read — same predicate as the table grant compiler. */
51
- function isColumnRead(a: Ability): boolean {
52
- return a.action === 'read' && Boolean(a.condition.owner) && Boolean(a.condition.columns?.length);
53
- }
54
-
55
51
  /**
56
52
  * Does the table's grant compile give `role` SELECT (full or column-scoped)?
57
53
  *
@@ -65,8 +61,11 @@ function isColumnRead(a: Ability): boolean {
65
61
  */
66
62
  export function tableReaches(m: ModelDescriptor, role: string): boolean {
67
63
  for (const a of m.abilities) {
68
- if (isColumnRead(a)) {
69
- if ((a.condition.role ?? 'authenticated') === role) return true;
64
+ if (isColumnAbility(a)) {
65
+ // Only the READ half reaches: a column-scoped update compiles to `UPDATE (cols)`
66
+ // and no SELECT, so counting it as reach would pass an invoker view the database
67
+ // then refuses at runtime — the false-negative this gate's docstring warns about.
68
+ if (a.action === 'read' && (a.condition.role ?? 'authenticated') === role) return true;
70
69
  continue;
71
70
  }
72
71
  if (a.action !== 'read' && a.action !== 'manage') continue;
@@ -31,7 +31,7 @@ import type { AuthzContract } from './authz-contract.js';
31
31
  import { generateMigrationSql, unmodeledTables } from './migration-generate.js';
32
32
  import { compileTableContract } from './authz-compile.js';
33
33
  import { classifyGeneratedStatements, classifyDestructive, partitionStatements, renderStatementHistogram } from './state-apply.js';
34
- import { fingerprintLive, fingerprintState, fingerprintModels, stableStringify, compareStoredFingerprint, formatChangedMessage, FINGERPRINT_VERSION } from './schema-fingerprint.js';
34
+ import { fingerprintLive, fingerprintState, fingerprintModels, governedRolesForModels, stableStringify, compareStoredFingerprint, formatChangedMessage, FINGERPRINT_VERSION } from './schema-fingerprint.js';
35
35
  import { compileDeclaredState } from './declared-diff.js';
36
36
  import { compileTableRenames, compileTableMoves } from './schema-compile.js';
37
37
 
@@ -67,8 +67,21 @@ export interface EdgePlan {
67
67
  * drift that no edit could explain. Absent on plans minted before v4.
68
68
  */
69
69
  fpVersion?: number;
70
- /** The predicted live fingerprint after apply — exact, unmodeled-aware. */
70
+ /**
71
+ * The predicted live fingerprint after apply — exact, unmodeled-aware, and
72
+ * GOVERNED: hashed through the models' governed-role set (`governedRoles`
73
+ * below), because it is a declared-vs-live claim. Compare it only against
74
+ * `governedLiveFingerprint` computed with the same set — never against the
75
+ * raw `from` flavor.
76
+ */
71
77
  toFingerprint: string;
78
+ /**
79
+ * The governed-role set `to` was hashed under, sorted — carried on the plan
80
+ * so every apply venue verifies with the exact set the mint used. Absent on
81
+ * plans minted before this field existed (those predate the governed `to`
82
+ * and verify raw, as they were minted).
83
+ */
84
+ governedRoles?: string[];
72
85
  /** The models-only fingerprint — context for the strong claim. Equals `to` when nothing is unmodeled. */
73
86
  declaredFingerprint: string;
74
87
  /** The edge, in db:generate's statement grammar (no held drops — mint refuses them). */
@@ -78,6 +91,21 @@ export interface EdgePlan {
78
91
  destructive: number;
79
92
  classification: PlanClassification;
80
93
  notices: number;
94
+ /**
95
+ * WHY each authorization statement exists, one entry per statement — the per-statement half
96
+ * of the aggregate the summary prints.
97
+ *
98
+ * Optional because it is re-derived against LIVE at mint time, so a plan minted without a
99
+ * reachable database (or before this field existed) simply has none. Never trusted as a
100
+ * stored claim: like the counts, it describes the target as it was when the plan was cut.
101
+ */
102
+ adoption?: { table: string; subject: string; cls: string; why: string }[];
103
+ /**
104
+ * Tables this plan leaves with no surviving read path — resolved at mint against the LIVE
105
+ * contract, which is the only moment both halves of that question are in hand. Absent on
106
+ * plans minted before this field existed; the summary falls back to the text-only reading.
107
+ */
108
+ dark?: string[];
81
109
  /** Live tables no model declares — untouched by the edge, riding into `to` as-is. */
82
110
  unmodeled: string[];
83
111
  gitRef: string | null;
@@ -114,8 +142,14 @@ export function predictLiveFingerprint(
114
142
  models: ModelDescriptor[],
115
143
  snapshot: SchemaSnapshot,
116
144
  contract: AuthzContract,
117
- opts: { schema?: string } = {},
145
+ opts: { schema?: string; governedRoles?: ReadonlySet<string> } = {},
118
146
  ): string {
147
+ // The prediction is a DECLARED-vs-live comparison, so it must hash through the
148
+ // models' governed set — the live side of any comparison against it must use
149
+ // the same set (see governedLiveFingerprint). Unfiltered, every ride-through
150
+ // table's foreign grants poison the endpoint and verify-after can never pass
151
+ // on a brownfield target.
152
+ const governedRoles = opts.governedRoles ?? governedRolesForModels(models);
119
153
  const declared = compileDeclaredState(models, { schema: opts.schema });
120
154
  const declaredTables = new Set(declared.snapshot.tables.map((t) => t.table));
121
155
  const currentNames = new Set(snapshot.tables.map((t) => t.table));
@@ -150,7 +184,21 @@ export function predictLiveFingerprint(
150
184
  ...declared.contract.tables,
151
185
  ...contract.tables.filter((t) => ridesThrough(t.table)),
152
186
  ];
153
- return fingerprintState(mergedSnapshot, mergedAuthz).hash;
187
+ return fingerprintState(mergedSnapshot, mergedAuthz, { governedRoles }).hash;
188
+ }
189
+
190
+ /**
191
+ * The live half of every declared-vs-live comparison: the target's state hashed
192
+ * through the SAME governed set as the prediction. `predicted === governedLive`
193
+ * is the declares-test; comparing a prediction against an UNFILTERED live hash
194
+ * is a category error that can never converge on a brownfield database.
195
+ */
196
+ export function governedLiveFingerprint(
197
+ snapshot: SchemaSnapshot,
198
+ contract: AuthzContract,
199
+ governedRoles: ReadonlySet<string>,
200
+ ): string {
201
+ return fingerprintState(snapshot, contract.tables, { governedRoles }).hash;
154
202
  }
155
203
 
156
204
  /**
@@ -193,12 +241,21 @@ export function mintEdgePlan(
193
241
  const breakdown = partitionStatements(classified.executable, { declaredGrantees });
194
242
  const destructive = breakdown.drops.length + breakdown.narrowings.length + breakdown.strips.length;
195
243
 
244
+ // The plan's two endpoints are two different FLAVORS, deliberately:
245
+ // - `from` is the RAW live hash (everything introspected) — the concurrency
246
+ // lock and the backup-stamp chain (backup.fp == plan.from) compare
247
+ // live-vs-live and must stay computable with no models in hand.
248
+ // - `to` is the GOVERNED hash — a declared-vs-live claim, filtered through
249
+ // the models' governed set, which rides ON the plan so every venue
250
+ // (direct, ops Lambda) verifies with the set the mint used.
251
+ const governedRoles = governedRolesForModels(models, opts.governedRoles);
196
252
  return {
197
253
  v: PLAN_VERSION,
198
254
  fromFingerprint: fingerprintLive(snapshot, contract).hash,
199
255
  fpVersion: FINGERPRINT_VERSION,
200
- toFingerprint: predictLiveFingerprint(models, snapshot, contract, { schema: opts.schema }),
256
+ toFingerprint: predictLiveFingerprint(models, snapshot, contract, { schema: opts.schema, governedRoles }),
201
257
  declaredFingerprint: fingerprintModels(models, { schema: opts.schema }).hash,
258
+ governedRoles: [...governedRoles].sort(),
202
259
  statements,
203
260
  executable: classified.executable.length,
204
261
  destructive,
@@ -213,6 +270,10 @@ export function mintEdgePlan(
213
270
  ...(breakdown.unclassified.length > 0 ? { unclassified: breakdown.unclassified.length } : {}),
214
271
  },
215
272
  notices: classified.notices.length,
273
+ // Resolved HERE because this is the only place that holds both the edge and the live
274
+ // contract. The summary is rendered later from the plan alone, and answering "does a read
275
+ // survive?" from statement text is what produced three false positives on a real plan.
276
+ dark: tablesLeftWithoutARead(statements, contract),
216
277
  unmodeled: unmodeledTables(models, snapshot),
217
278
  gitRef: opts.gitRef ?? null,
218
279
  mintedBy: opts.actor ?? null,
@@ -227,25 +288,58 @@ export function mintEdgePlan(
227
288
  * enabled and the grant is still there, so the table returns ZERO rows to that role. On a real
228
289
  * adoption plan that was 11 tables, including the users table, and it printed no notice at all.
229
290
  *
230
- * Deliberately conservative: it only names a table when the plan drops a read-admitting policy
231
- * and adds none back for that table. It cannot know what other policies exist live, so it
232
- * under-reports rather than crying wolf.
291
+ * Deliberately conservative: it names a table only when NO read path survives the plan for any
292
+ * role, so it under-reports rather than crying wolf. Pass the LIVE contract to get that
293
+ * question answered properly without it, a `DROP POLICY` statement does not say which
294
+ * command it policed and the fallback can only count drops.
233
295
  */
234
- export function tablesLeftWithoutARead(statements: readonly string[]): string[] {
235
- const dropped = new Map<string, number>();
236
- const created = new Set<string>();
296
+ export function tablesLeftWithoutARead(statements: readonly string[], live?: AuthzContract): string[] {
297
+ const droppedByTable = new Map<string, Set<string>>();
298
+ const createdRead = new Set<string>();
299
+ const touched = new Set<string>();
300
+
237
301
  for (const statement of statements) {
238
302
  const head = (statement.split('\n').find((l) => l.trim() !== '' && !l.trim().startsWith('--')) ?? '').trim();
239
- let m = /^DROP\s+POLICY\s+(?:IF\s+EXISTS\s+)?\S+\s+ON\s+(\S+?);?$/i.exec(head);
303
+ let m = /^DROP\s+POLICY\s+(?:IF\s+EXISTS\s+)?(\S+)\s+ON\s+(\S+?);?$/i.exec(head);
240
304
  if (m) {
241
- dropped.set(m[1], (dropped.get(m[1]) ?? 0) + 1);
305
+ touched.add(m[2]);
306
+ const names = droppedByTable.get(m[2]) ?? new Set<string>();
307
+ names.add(m[1]);
308
+ droppedByTable.set(m[2], names);
242
309
  continue;
243
310
  }
244
311
  m = /^CREATE\s+POLICY\s+\S+\s+ON\s+(\S+)/i.exec(head);
245
312
  // Only a SELECT-admitting policy restores a read; an INSERT-only policy does not.
246
- if (m && /\bFOR\s+(SELECT|ALL)\b/i.test(head)) created.add(m[1]);
313
+ if (m && /\bFOR\s+(SELECT|ALL)\b/i.test(head)) createdRead.add(m[1]);
314
+ }
315
+
316
+ // Without the live contract this can only count drops, and a DROP POLICY statement does not
317
+ // carry the command it policed. That is the old behaviour, kept for plans minted before the
318
+ // contract was threaded through: it OVER-reports, naming a table whose dropped policy was an
319
+ // UPDATE and whose reads were never touched.
320
+ if (!live) return [...droppedByTable.keys()].filter((t) => !createdRead.has(t)).sort();
321
+
322
+ // With it, ask the real question: does ANY policy admitting SELECT survive this plan? A
323
+ // table is dark only when none does — not merely because some policy on it was dropped.
324
+ //
325
+ // Three false positives on a real adoption plan came from the cheaper question: two tables
326
+ // lost only an UPDATE policy, and one lost a single role-scoped read while anon and
327
+ // authenticated reads survived untouched. A safety detector that cries wolf spends the
328
+ // credibility of its true positives, so this deliberately under-reports instead.
329
+ const byTable = new Map(live.tables.map((t) => [t.table, t]));
330
+ const dark: string[] = [];
331
+ for (const table of touched) {
332
+ if (createdRead.has(table)) continue;
333
+ const contract = byTable.get(table);
334
+ if (!contract) continue; // not a live table — nothing to go dark
335
+ if (!contract.rls?.enabled) continue; // RLS off: policies do not gate the read at all
336
+ const dropped = droppedByTable.get(table) ?? new Set<string>();
337
+ const readSurvives = contract.policies.some(
338
+ (p) => (p.command === 'SELECT' || p.command === 'ALL') && !dropped.has(p.name),
339
+ );
340
+ if (!readSurvives) dark.push(table);
247
341
  }
248
- return [...dropped.keys()].filter((t) => !created.has(t)).sort();
342
+ return dark.sort();
249
343
  }
250
344
 
251
345
  /** The plan's content address — recorded as `plan_ref` on the schema_log row. */
@@ -299,7 +393,9 @@ export function buildPlanSummary(plan: EdgePlan): string[] {
299
393
  // rows, and a plan that drops a table's only read policy leaves that table returning nothing.
300
394
  // 11 tables went dark in a real adoption plan that printed "0 notice(s)".
301
395
  const { authzRemovals, unclassified } = partitionStatements(classifyGeneratedStatements(plan.statements).executable);
302
- const dark = tablesLeftWithoutARead(plan.statements);
396
+ // Prefer what the mint resolved against live. Recomputing here would have only the statement
397
+ // text and would re-introduce the false positives the mint-time answer exists to avoid.
398
+ const dark = plan.dark ?? tablesLeftWithoutARead(plan.statements);
303
399
  if (authzRemovals.length > 0) {
304
400
  lines.push(`! ${authzRemovals.length} statement(s) REMOVE authorization (policies, grants, RLS). No data is lost; who can read it changes.`);
305
401
  if (dark.length > 0) {
@@ -32,7 +32,8 @@ import type { ModelDescriptor } from '@everystack/model';
32
32
  import type { SchemaSnapshot } from './schema-introspect.js';
33
33
  import type { AuthzContract } from './authz-contract.js';
34
34
  import { fingerprintLive } from './schema-fingerprint.js';
35
- import { predictLiveFingerprint } from './edge-plan.js';
35
+ import { predictLiveFingerprint, governedLiveFingerprint } from './edge-plan.js';
36
+ import { governedRolesForModels } from './schema-fingerprint.js';
36
37
 
37
38
  /** Compile a models barrel on disk. The default rides the CLI's runtime (tsx). */
38
39
  export type ModelsLoader = (barrel: string) => Promise<ModelDescriptor[]>;
@@ -174,7 +175,6 @@ export async function verifyDescent(
174
175
  const barrel = path.basename(modelsAbs);
175
176
 
176
177
  const candidates = enumerateModelTrees(modelsDir, repoRoot);
177
- const liveFingerprint = fingerprintLive(live.snapshot, live.contract).hash;
178
178
 
179
179
  const nodeModules = path.join(repoRoot, 'node_modules');
180
180
  const materializeRoot = opts.materializeRoot
@@ -187,29 +187,36 @@ export async function verifyDescent(
187
187
  try {
188
188
  for (const candidate of candidates) {
189
189
  const dest = path.join(materializeRoot, candidate.tree);
190
- let predicted: string;
190
+ let declares: boolean;
191
191
  // A swallowed error here skips the candidate — and if it was the DECLARING tree, the
192
192
  // verdict silently falls through to 'drift'. Real compile failures are deterministic;
193
193
  // a transient materialize/import failure (fd pressure, module-loader contention under
194
194
  // a parallel test run or a busy CI box) is not — so a failed candidate gets exactly
195
195
  // one retry, from a clean materialization, before it is recorded as a compile failure.
196
- const attempt = async (): Promise<string> => {
196
+ //
197
+ // The declares-test is GOVERNED on both sides, per candidate: the commit's
198
+ // models define the governed set, and the live hash is filtered through the
199
+ // SAME set as the prediction — comparing a governed prediction against the
200
+ // raw live hash can never match on a brownfield target with foreign roles.
201
+ const attempt = async (): Promise<boolean> => {
197
202
  if (!fs.existsSync(dest)) materializeTree(candidate.tree, dest, repoRoot);
198
203
  const models = await loader(path.join(dest, barrel));
199
- return predictLiveFingerprint(models, live.snapshot, live.contract, { schema: opts.schema });
204
+ const governedRoles = governedRolesForModels(models);
205
+ const predicted = predictLiveFingerprint(models, live.snapshot, live.contract, { schema: opts.schema, governedRoles });
206
+ return predicted === governedLiveFingerprint(live.snapshot, live.contract, governedRoles);
200
207
  };
201
208
  try {
202
209
  try {
203
- predicted = await attempt();
210
+ declares = await attempt();
204
211
  } catch {
205
212
  fs.rmSync(dest, { recursive: true, force: true });
206
- predicted = await attempt();
213
+ declares = await attempt();
207
214
  }
208
215
  } catch (err: any) {
209
216
  compileFailures.push(`${short(candidate.tree)} (at ${short(candidate.commits[0])}): ${err.message}`);
210
217
  continue;
211
218
  }
212
- if (predicted !== liveFingerprint) continue;
219
+ if (!declares) continue;
213
220
 
214
221
  for (const commit of candidate.commits) {
215
222
  try {
@@ -255,7 +262,7 @@ export async function verifyDescent(
255
262
  scannedTrees: candidates.length,
256
263
  compileFailures,
257
264
  reason:
258
- `no committed models state declares the target's live fingerprint ${short(liveFingerprint)} — ` +
265
+ `no committed models state declares the target's live fingerprint ${short(fingerprintLive(live.snapshot, live.contract).hash)} — ` +
259
266
  `scanned ${candidates.length} historical tree(s) across all refs${failureNote}. ` +
260
267
  'Either the database was hand-edited (drift) or the declaring history was rewritten. ' +
261
268
  'Align the models with the live database first (db:pull, commit), ' +
package/src/cli/index.ts CHANGED
@@ -367,7 +367,7 @@ Usage:
367
367
  everystack db:sync [--database-url <url>] [--models <barrel>] [--schema-out <file.ts>] [--allow-drops] [--overwrite-drift] [--baseline] [--json] Make the database match your checkout — one verb, both layers: apply the state diff (tables+authz, one transaction, verified by re-diff), reconcile the derived layer against the declared descriptors, report the resulting fingerprint vs the models' declared one. Dev databases only (direct connection required); DROPs held back unless --allow-drops; derived drift refuses unless --overwrite-drift; exit 1 when not converged
368
368
  everystack db:diff --from-models <barrel> [--to-models db/models/index.ts] [--allow-drops] [--check] [--json] The state edge between two declared states, NO database: the SQL db:generate would produce, computed purely — CI plan previews (--check exits 1 on a non-empty edge) and computed rollbacks (swap the flags)
369
369
  everystack db:plan [--stage <name> | --database-url <url>] [--models <barrel>] [--allow-drops] [--out db.plan.json | --out -] Mint a verified edge against a target: asks the TARGET its fingerprint, diffs the models, writes ONE reviewable plan (edge + both endpoint fingerprints). Held drops refuse the mint (--allow-drops carries destruction explicitly). Read-only — works via the ops Lambda; plans are ephemeral, never committed
370
- everystack db:apply --plan <file.plan.json> [--database-url <url>] [--stage <name>] [--models <barrel>] [--confirm] [--snapshot-ref <ref>] [--force-descent <snapshot-ref> --confirm] Run a reviewed plan: verify the target is EXACTLY where the plan started (live fingerprint == plan.from, else refuse — the concurrency lock), verify the checkout DESCENDS from the commit declaring the target's state (the fast-forward rule, else refuse — "rebase first"), and for DESTRUCTIVE plans require --confirm always + a snapshot (automatic via db:backup with --stage, else --snapshot-ref) + the stage's approver set when declared (STS identity-verified). Every refusal is recorded in schema_log. Apply as one transaction (plan_ref stamped), verify it landed exactly on plan.to; idempotent when already there; direct connection required
370
+ everystack db:apply --plan <file.plan.json> [--database-url <url>] [--stage <name>] [--models <barrel>] [--confirm] [--snapshot-ref <ref>] [--force-descent <snapshot-ref> --confirm] Run a reviewed plan: verify the target is EXACTLY where the plan started (live fingerprint == plan.from, else refuse — the concurrency lock), verify the checkout DESCENDS from the commit declaring the target's state (the fast-forward rule, else refuse — "rebase first"), and for DESTRUCTIVE plans require --confirm always + an attested --snapshot-ref + the stage's approver set when declared (STS identity-verified). The STAGE lane (--stage without --direct) runs every catalog query in its own ops-Lambda invoke, so one read can be assembled from several containers: it reads the target TWICE and REFUSES when the two disagree (inconsistent containers), reports agreement as a NON-DETECTION (it cannot verify read consistency), and REFUSES a DESTRUCTIVE plan outright — destructive applies go over --database-url (direct) with the full ceremony. Every refusal that reaches the ops Lambda is recorded in schema_log. Apply as one transaction (plan_ref stamped), verify it landed exactly on plan.to; idempotent when already there
371
371
  everystack db:check [--models <barrel>] [--schema-out <file.ts>] [--database-url <url>] [--json] The CI gate, per PR: the merged declared state must COMPOSE (models load, no duplicate tables, descriptors compile), every exposed RLS-enabled table must declare a read path (no force-RLS-with-no-read landmine that goes dark on the superuser drop), and generated artifacts must MATCH regeneration byte-for-byte; with a scratch PostgreSQL it builds the state from scratch on an ephemeral database (created + dropped) and requires fingerprint MATCH. Exit 1 on any failure; never touches a real target
372
372
  everystack db:approvers --stage <name> [--set "cto,arn:..."] [--remove] Declare who can DESTROY: the stage's destructive-approver set (SSM parameter, admin-writable). Destructive db:apply runs are then identity-verified (STS) against it; --set '' disables destructive applies; --remove returns the stage to ceremony-only
373
373
  everystack db:backfill [--database-url <url>] [--dir db/backfills] [--apply] [--mark-applied <file.sql>] [--json] One-shot data jobs in their own lane: plan shows applied (by CONTENT identity — renames/comment edits are no-ops) / pending (in order, unbounded-pass advisories) / blocked (a name that already ran in a different form — one-shot jobs are immutable). --apply runs each pending job as its own transaction, recorded in everystack.backfill_log (a failure rolls back alone, is recorded, stops the run); --mark-applied records without running. Never runs as a schema side effect; direct connection required