Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(scale): the pg_stats gate is enforced on PGLite only; on Postgres autovacuum owns statistics, so missing rows are report-only

garrytan06b9fae29ce7
diff --git a/.github/workflows/scale-tier.yml b/.github/workflows/scale-tier.yml
index 167c58c60b..92a4cb9fb1 100644
--- a/.github/workflows/scale-tier.yml
+++ b/.github/workflows/scale-tier.yml
@@ -12,7 +12,7 @@ name: Scale tier
#
# Enforced: import rate, known answers, no-op re-import, no duplicates across
# sources, the import phase timer, planner health (probed after the first
-# timed op, hot tables above 500 rows) and the budgets phase timer
+# timed op, hot tables above 500 rows; PGLite only) and the budgets phase timer
# (PLANNER_HEALTH_ENFORCED in scripts/scale/gates.ts). Interactive ceilings
# and calibrated budgets stay report-only until `bun scripts/scale/trend.ts`
# prints "ceilings stable" and a reviewer sets the repo variable
diff --git a/docs/TESTING.md b/docs/TESTING.md
index 0aa4b2bec6..638d34f94b 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -305,7 +305,7 @@ gates (`PLANNER_HEALTH_ENFORCED` in `scripts/scale/gates.ts`): the Nested Loop
inner-loop gate on the key plans, the budgets phase timer, and planner stats.
Planner stats are probed after the first timed op, because F4b analyzes on the
first planner-sensitive read, and only hot tables above 500 rows must have
-`pg_stats` rows.
+`pg_stats` rows (PGLite only: on Postgres autovacuum owns statistics, so a missing row is report-only).
Interactive ceilings and calibrated budgets (`scripts/scale/budgets.json`,
written by `--calibrate`) stay report-only until
`bun scripts/scale/trend.ts` prints "ceilings stable" over the last five
diff --git a/scripts/scale/gates.ts b/scripts/scale/gates.ts
index a07263037e..1334d71762 100644
--- a/scripts/scale/gates.ts
+++ b/scripts/scale/gates.ts
@@ -121,11 +121,16 @@ export function evaluateScaleGates(report: ScaleReport, policy: GatePolicy): Gat
const { hot_table_stat_rows: statRows, hot_table_rows: tableRows, probed_after: probedAfter } = report.planner;
const missing = HOT_TABLES.filter(t => tableRows[t]! > PLANNER_STATS_MIN_ROWS && !(statRows[t]! > 0));
- add('planner_stats', missing.length === 0, policy.enforcePlanner,
+ // Postgres statistics belong to autovacuum, whose timing gbrain does not control (F4b is PGLite-only):
+ // missing pg_stats rows there are reported, never a failure.
+ add('planner_stats', missing.length === 0, policy.enforcePlanner && report.engine === 'pglite',
missing.length === 0 ? `planner stats: every hot table above ${PLANNER_STATS_MIN_ROWS} rows has pg_stats rows after ${probedAfter}`
: `planner stats: no pg_stats rows after ${probedAfter} for ${missing.map(t => `${t} (${tableRows[t]} rows)`).join(', ')}; `
- + 'the planner is guessing row counts on these tables. Import and the first planner-sensitive read must leave statistics behind (F4b); '
- + `check \`gbrain doctor\` planner_stats_stale and run \`gbrain repair planner-stats --apply\` to confirm. Reproduce: ${repro}`);
+ + 'the planner is guessing row counts on these tables. '
+ + (report.engine === 'pglite'
+ ? 'Import and the first planner-sensitive read must leave statistics behind (F4b); '
+ + `check \`gbrain doctor\` planner_stats_stale and run \`gbrain repair planner-stats --apply\` to confirm. Reproduce: ${repro}`
+ : `Postgres autovacuum collects them on its own schedule, so this is report-only there. Reproduce: ${repro}`));
const loopsLimit = LOOPS_PER_PAGE_MAX * report.pages;
for (const name of KEY_PLAN_OPS) {
diff --git a/test/scripts/scale-gates.test.ts b/test/scripts/scale-gates.test.ts
index 3e62511a3b..5be3b9ed23 100644
--- a/test/scripts/scale-gates.test.ts
+++ b/test/scripts/scale-gates.test.ts
@@ -149,6 +149,15 @@ describe('scale gates under --enforce', () => {
expect(verdict.failures[0]!.message).toContain('gbrain repair planner-stats --apply');
});
+ test('missing pg_stats rows on Postgres are report-only: autovacuum owns its statistics', () => {
+ const report = { ...passingReport(), engine: 'postgres' as const };
+ report.planner.hot_table_stat_rows.facts = 0;
+ const verdict = evaluateScaleGates(report, ENFORCE);
+ expect(verdict.exitCode).toBe(0);
+ expect(verdict.reportOnlyBreaches.map(b => b.gate)).toEqual(['planner_stats']);
+ expect(verdict.reportOnlyBreaches[0]!.message).toContain('autovacuum');
+ });
+
test('without --enforce every run exits 0 and says how many gates would fail', () => {
const report = passingReport();
report.ops[0] = { ...report.ops[0]!, known_answer: 'fail', detail: 'page_count 1 != 10000' };