Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(doctor): legacy_token_grant_shape and legacy_token_grant_drift (F3)

garrytan02bdc04b52ff
diff --git a/src/commands/doctor/checks/legacy-token-grants.ts b/src/commands/doctor/checks/legacy-token-grants.ts
new file mode 100644
index 0000000000..a2bcc8ab06
--- /dev/null
+++ b/src/commands/doctor/checks/legacy-token-grants.ts
@@ -0,0 +1,61 @@
+/**
+ * F3 legacy token grants (O-CEO-8, O-DX-9).
+ *
+ * `legacy_token_grant_shape` (informational, status ok): active tokens still
+ * on the JSONB-only grant shape. They keep working; the next grant edit
+ * migrates each one, or `gbrain auth rescope --migrate-legacy` writes the
+ * columns for all of them without changing any grant.
+ *
+ * `legacy_token_grant_drift` (warn): migrated tokens whose `permissions`
+ * mirror disagrees with the grant columns (an older gbrain edited the JSONB).
+ * Each drifted axis denies every request until the operator picks a side.
+ */
+import { grantFromTokenRow, type LegacyGrantAxis } from '../../../core/grants/model.ts';
+import type { Check } from '../../doctor.ts';
+import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
+
+const DOCS = 'docs/mcp/ADMIN.md#legacy-token-grants';
+
+async function runLegacyTokenGrants(ctx: DoctorContext): Promise<Check[]> {
+ const checks: Check[] = [];
+ const rows = await connectedEngine(ctx).executeRaw<Record<string, unknown>>(
+ 'SELECT * FROM access_tokens WHERE revoked_at IS NULL ORDER BY created_at, id');
+ const legacy: Array<{ name: string; id: string; malformed: boolean }> = [];
+ const drift: Array<{ name: string; id: string; axes: LegacyGrantAxis[] }> = [];
+ for (const row of rows) {
+ const grant = grantFromTokenRow(row);
+ const entry = { name: String(row.name), id: String(row.id) };
+ if (grant.shape === 'legacy_permissions') legacy.push({ ...entry, malformed: grant.permissionsMalformed });
+ if (grant.drift.length) drift.push({ ...entry, axes: grant.drift });
+ }
+ const malformed = legacy.filter(t => t.malformed);
+ checks.push({
+ name: 'legacy_token_grant_shape',
+ status: 'ok',
+ message: legacy.length === 0
+ ? 'Every active legacy token uses the unified grant columns.'
+ : `${legacy.length} active legacy token(s) still use the permissions-JSON grant shape; they keep working. `
+ + 'Write the unified columns without changing any grant: gbrain auth rescope --migrate-legacy (preview with --dry-run; no user decision needed).'
+ + (malformed.length ? ` ${malformed.length} have malformed permissions and will be skipped; ask the user which grant each should hold, then run gbrain auth rescope --id <id> with explicit --sources/--takes-holders/--operations (ids in details.malformed).` : '')
+ + ` See ${DOCS}.`,
+ details: { legacy_shape_count: legacy.length, malformed: malformed.map(({ name, id }) => ({ name, id })), docs: DOCS },
+ });
+ checks.push({
+ name: 'legacy_token_grant_drift',
+ status: drift.length ? 'warn' : 'ok',
+ message: drift.length === 0
+ ? 'No legacy token grant has drifted from its permissions mirror.'
+ : `${drift.length} legacy token(s) have grant drift: an older gbrain edited the permissions JSON after migration, so the drifted axes deny every request (fail-closed). `
+ + 'Ask the user which grant is intended, then for each token run gbrain auth rescope --token <name> --adopt-permissions (keep the JSON edit) or --adopt-columns (restore the columns): '
+ + drift.slice(0, 5).map(t => `${t.name} (${t.axes.join(', ')}): gbrain auth rescope --token ${t.name} --adopt-permissions|--adopt-columns`).join('; ')
+ + `${drift.length > 5 ? `; and ${drift.length - 5} more in details.drift` : ''}. See ${DOCS}.`,
+ details: { drift, docs: DOCS },
+ });
+ return checks;
+}
+
+export const legacyTokenGrantsEntry: DoctorEntry = {
+ name: 'legacy_token_grant_shape',
+ emits: ['legacy_token_grant_shape', 'legacy_token_grant_drift'],
+ run: runLegacyTokenGrants,
+};
diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.ts
index e9a036053e..ae3439fd86 100644
--- a/src/commands/doctor/registry.ts
+++ b/src/commands/doctor/registry.ts
@@ -80,6 +80,7 @@ import {
import { queueHealthEntry, indexAuditEntry, imageAssetsEntry } from './checks/queue-assets.ts';
import { globalMaintenanceTimeoutsEntry } from './checks/global-maintenance-timeouts.ts';
import { legacyJobAuthorityEntry } from './checks/legacy-job-authority.ts';
+import { legacyTokenGrantsEntry } from './checks/legacy-token-grants.ts';
import { syncFreshnessEntry, searchModeEntry } from './checks/sync-search.ts';
import { autoChronicleEntry } from './checks/auto-chronicle.ts';
import { factTakeVectorsEntry } from './checks/vector-coverage.ts';
@@ -138,6 +139,7 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [
queueHealthEntry,
globalMaintenanceTimeoutsEntry,
legacyJobAuthorityEntry,
+ legacyTokenGrantsEntry,
indexAuditEntry,
imageAssetsEntry,
syncFreshnessEntry,
diff --git a/src/core/doctor-categories.ts b/src/core/doctor-categories.ts
index 42abaa19e4..b9a0167320 100644
--- a/src/core/doctor-categories.ts
+++ b/src/core/doctor-categories.ts
@@ -236,6 +236,9 @@ export const OPS_CHECK_NAMES: ReadonlySet<string> = new Set([
'global_maintenance_timeouts',
// #5157: queued jobs from before the v0.50 authority cutover block every worker.
'legacy_job_authority',
+ // F3: legacy tokens on the JSONB-only grant shape (info) and grant drift (warn).
+ 'legacy_token_grant_shape',
+ 'legacy_token_grant_drift',
'reranker_health',
'rls',
'rls_event_trigger',
diff --git a/test/fixtures/goldens/doctor/json-pglite-degraded.json b/test/fixtures/goldens/doctor/json-pglite-degraded.json
index 1e43169ad1..7ed1c100e5 100644
--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json
+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json
@@ -383,13 +383,13 @@
},
{
"category": "meta",
- "message": "Version 192 (latest: 192)",
+ "message": "Version 193 (latest: 193)",
"name": "schema_version",
"status": "ok"
},
{
"category": "meta",
- "message": "813 column(s) verified against live schema",
+ "message": "819 column(s) verified against live schema",
"name": "schema_columns",
"status": "ok"
},
@@ -925,6 +925,27 @@
"name": "legacy_job_authority",
"status": "ok"
},
+ {
+ "category": "ops",
+ "details": {
+ "docs": "docs/mcp/ADMIN.md#legacy-token-grants",
+ "legacy_shape_count": 0,
+ "malformed": []
+ },
+ "message": "Every active legacy token uses the unified grant columns.",
+ "name": "legacy_token_grant_shape",
+ "status": "ok"
+ },
+ {
+ "category": "ops",
+ "details": {
+ "docs": "docs/mcp/ADMIN.md#legacy-token-grants",
+ "drift": []
+ },
+ "message": "No legacy token grant has drifted from its permissions mirror.",
+ "name": "legacy_token_grant_drift",
+ "status": "ok"
+ },
{
"category": "brain",
"message": "No image assets indexed yet",
diff --git a/test/fixtures/goldens/doctor/json-pglite-fresh.json b/test/fixtures/goldens/doctor/json-pglite-fresh.json
index 34d18106b9..7b7513b83b 100644
--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json
+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json
@@ -383,13 +383,13 @@
},
{
"category": "meta",
- "message": "Version 192 (latest: 192)",
+ "message": "Version 193 (latest: 193)",
"name": "schema_version",
"status": "ok"
},
{
"category": "meta",
- "message": "813 column(s) verified against live schema",
+ "message": "819 column(s) verified against live schema",
"name": "schema_columns",
"status": "ok"
},
@@ -925,6 +925,27 @@
"name": "legacy_job_authority",
"status": "ok"
},
+ {
+ "category": "ops",
+ "details": {
+ "docs": "docs/mcp/ADMIN.md#legacy-token-grants",
+ "legacy_shape_count": 0,
+ "malformed": []
+ },
+ "message": "Every active legacy token uses the unified grant columns.",
+ "name": "legacy_token_grant_shape",
+ "status": "ok"
+ },
+ {
+ "category": "ops",
+ "details": {
+ "docs": "docs/mcp/ADMIN.md#legacy-token-grants",
+ "drift": []
+ },
+ "message": "No legacy token grant has drifted from its permissions mirror.",
+ "name": "legacy_token_grant_drift",
+ "status": "ok"
+ },
{
"category": "brain",
"message": "No image assets indexed yet",
diff --git a/test/fixtures/goldens/doctor/registry-build-checks.json b/test/fixtures/goldens/doctor/registry-build-checks.json
index c759f9f9e5..7396d98cba 100644
--- a/test/fixtures/goldens/doctor/registry-build-checks.json
+++ b/test/fixtures/goldens/doctor/registry-build-checks.json
@@ -505,6 +505,14 @@
"category": "ops",
"name": "legacy_job_authority"
},
+ {
+ "category": "ops",
+ "name": "legacy_token_grant_shape"
+ },
+ {
+ "category": "ops",
+ "name": "legacy_token_grant_drift"
+ },
{
"category": "ops",
"name": "index_audit"