Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(grants): access_tokens grant columns, migration f3_access_token_grants (F3)
garrytan05c507f82f89
diff --git a/scripts/build-schema.ts b/scripts/build-schema.tsindex 649d7c3287..83d3fa0dcc 100644--- a/scripts/build-schema.ts+++ b/scripts/build-schema.ts@@ -30,6 +30,7 @@ import { dirname, join, resolve } from 'node:path';import { SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL } from '../src/core/company-brain/receipt-schema.ts';import { FACT_WITHDRAWAL_SCHEMA_STATEMENTS } from '../src/core/facts/withdrawal-schema.ts';import { GRANT_AUDIT_SCHEMA_SQL } from '../src/core/grants/schema.ts';+import { ACCESS_TOKEN_GRANT_SCHEMA_SQL } from '../src/core/grants/access-token-schema.ts';import { LEASE_TOKEN_SCHEMA_SQL } from '../src/core/lease-schema.ts';import { PAGE_PROJECTION_SCHEMA_SQL } from '../src/core/page-state/projection-schema.ts';import { PAGE_STATE_SCHEMA_SQL } from '../src/core/page-state/schema.ts';@@ -68,6 +69,7 @@ export interface Fragment {const persistenceSql = (statements: readonly string[]) => `${statements.join(';\n')};`;export const FRAGMENTS: readonly Fragment[] = [+ { source: 'src/core/grants/access-token-schema.ts', expr: 'ACCESS_TOKEN_GRANT_SCHEMA_SQL', postgres: ACCESS_TOKEN_GRANT_SCHEMA_SQL, pglite: ACCESS_TOKEN_GRANT_SCHEMA_SQL },{ source: 'src/core/grants/schema.ts', expr: 'GRANT_AUDIT_SCHEMA_SQL', postgres: GRANT_AUDIT_SCHEMA_SQL, pglite: GRANT_AUDIT_SCHEMA_SQL },{source: 'src/core/facts/withdrawal-schema.ts',diff --git a/src/core/grants/access-token-schema.ts b/src/core/grants/access-token-schema.tsnew file mode 100644index 0000000000..543094b20e--- /dev/null+++ b/src/core/grants/access-token-schema.ts@@ -0,0 +1,17 @@+/**+ * F3 (O-CEO-8, O-ENG-7): legacy bearer tokens get the OAuth client grant+ * columns. `source_grant` names the source state explicitly; NULL means the+ * row still uses the `permissions` JSONB shape and is read through the lane F+ * parsers until its next write (`auth rescope`, `auth create`, rotation, or+ * `auth rescope --migrate-legacy`). No foreign key on `source_id`: the client+ * FK is ON DELETE RESTRICT, and tokens must not start blocking source removal.+ */+export const ACCESS_TOKEN_GRANT_SCHEMA_SQL = `+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_grant TEXT+ CHECK (source_grant IN ('default', 'scalar', 'federated', 'none'));+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_id TEXT;+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS federated_read TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS allowed_operations TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS takes_holders TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS grant_revision INTEGER NOT NULL DEFAULT 0;+`;diff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.tsindex 24451f0f17..77ca982794 100644--- a/src/core/pglite-schema.generated.ts+++ b/src/core/pglite-schema.generated.ts@@ -284,6 +284,14 @@ CREATE TABLE IF NOT EXISTS access_tokens (CREATE INDEX IF NOT EXISTS idx_access_tokens_hash ON access_tokens (token_hash) WHERE revoked_at IS NULL;+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_grant TEXT+ CHECK (source_grant IN ('default', 'scalar', 'federated', 'none'));+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_id TEXT;+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS federated_read TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS allowed_operations TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS takes_holders TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS grant_revision INTEGER NOT NULL DEFAULT 0;+CREATE TABLE IF NOT EXISTS mcp_request_log (id SERIAL PRIMARY KEY,token_name TEXT,diff --git a/src/core/schema-embedded.generated.ts b/src/core/schema-embedded.generated.tsindex 278e3da555..49f6fc97cd 100644--- a/src/core/schema-embedded.generated.ts+++ b/src/core/schema-embedded.generated.ts@@ -644,6 +644,16 @@ CREATE TABLE IF NOT EXISTS access_tokens (CREATE INDEX IF NOT EXISTS idx_access_tokens_hash ON access_tokens (token_hash) WHERE revoked_at IS NULL;+-- BEGIN GENERATED from src/core/grants/access-token-schema.ts (ACCESS_TOKEN_GRANT_SCHEMA_SQL). Edit that file, then run: bun run build:schema+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_grant TEXT+ CHECK (source_grant IN ('default', 'scalar', 'federated', 'none'));+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_id TEXT;+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS federated_read TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS allowed_operations TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS takes_holders TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS grant_revision INTEGER NOT NULL DEFAULT 0;+-- END GENERATED from src/core/grants/access-token-schema.ts (ACCESS_TOKEN_GRANT_SCHEMA_SQL)+-- ============================================================-- mcp_request_log: usage logging for remote MCP requests-- ============================================================diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.tsindex a15abb5b68..6ebbbee16f 100644--- a/src/core/schema-migrations/registry.generated.ts+++ b/src/core/schema-migrations/registry.generated.ts@@ -193,6 +193,7 @@ import { v189 } from './v189-pages-credential-projection-pending.ts';import { v190 } from './v190-sources-upstream-observation.ts';import { v191 } from './v191-alias-source-cascade.ts';import { v192 } from './v192-take-embedding-identity.ts';+import { v193 } from './v193-f3-access-token-grants.ts';// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); the// runner sorts by version before applying.@@ -384,4 +385,5 @@ export const MIGRATIONS: Migration[] = [v190,v191,v192,+ v193,];diff --git a/src/core/schema-migrations/v193-f3-access-token-grants.ts b/src/core/schema-migrations/v193-f3-access-token-grants.tsnew file mode 100644index 0000000000..a692a4c86a--- /dev/null+++ b/src/core/schema-migrations/v193-f3-access-token-grants.ts@@ -0,0 +1,16 @@+import { ACCESS_TOKEN_GRANT_SCHEMA_SQL } from '../grants/access-token-schema.ts';+import type { Migration } from './types.ts';++// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md+// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.+//+// F3: the unified grant columns on access_tokens (fragment+// src/core/grants/access-token-schema.ts). Additive and metadata-only; every+// existing row keeps source_grant NULL and is migrated lazily on its next grant+// write, so no token is re-issued.+export const v193: Migration = {+ version: 193,+ name: 'f3_access_token_grants',+ idempotent: true,+ sql: ACCESS_TOKEN_GRANT_SCHEMA_SQL,+};diff --git a/src/schema.sql b/src/schema.sqlindex a557d8d739..eeb10dd898 100644--- a/src/schema.sql+++ b/src/schema.sql@@ -640,6 +640,16 @@ CREATE TABLE IF NOT EXISTS access_tokens (CREATE INDEX IF NOT EXISTS idx_access_tokens_hash ON access_tokens (token_hash) WHERE revoked_at IS NULL;+-- BEGIN GENERATED from src/core/grants/access-token-schema.ts (ACCESS_TOKEN_GRANT_SCHEMA_SQL). Edit that file, then run: bun run build:schema+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_grant TEXT+ CHECK (source_grant IN ('default', 'scalar', 'federated', 'none'));+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS source_id TEXT;+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS federated_read TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS allowed_operations TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS takes_holders TEXT[];+ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS grant_revision INTEGER NOT NULL DEFAULT 0;+-- END GENERATED from src/core/grants/access-token-schema.ts (ACCESS_TOKEN_GRANT_SCHEMA_SQL)+-- ============================================================-- mcp_request_log: usage logging for remote MCP requests-- ============================================================diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-default.json b/test/fixtures/goldens/catalog/pglite-engine-init-default.jsonindex 7bb9733603..5ed432ec0f 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-default.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-default.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","budget_ledger.budget_ledger_pkey": "p PRIMARY KEY (scope, resolver_id, local_date)","budget_reservations.budget_reservations_pkey": "p PRIMARY KEY (reservation_id)",@@ -998,7 +999,13 @@"5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL","7 revoked_at timestamp with time zone/timestamptz NULL",- "8 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0",+ "14 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.jsonindex 7447ae5006..69ce2ed945 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","budget_ledger.budget_ledger_pkey": "p PRIMARY KEY (scope, resolver_id, local_date)","budget_reservations.budget_reservations_pkey": "p PRIMARY KEY (reservation_id)",@@ -998,7 +999,13 @@"5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL","7 revoked_at timestamp with time zone/timestamptz NULL",- "8 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0",+ "14 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.jsonindex c228f4da8d..649470899d 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","budget_ledger.budget_ledger_pkey": "p PRIMARY KEY (scope, resolver_id, local_date)","budget_reservations.budget_reservations_pkey": "p PRIMARY KEY (reservation_id)",@@ -994,7 +995,13 @@"5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL","7 revoked_at timestamp with time zone/timestamptz NULL",- "8 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0",+ "14 permissions jsonb/jsonb NOT NULL DEFAULT '{\"takes_holders\": [\"world\"]}'::jsonb"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-default.json b/test/fixtures/goldens/catalog/pglite-schema-blob-default.jsonindex 5713375a5c..30f3bbc135 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-default.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-default.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","calibration_profiles.calibration_profiles_pkey": "p PRIMARY KEY (id)","calibration_profiles.calibration_profiles_source_id_fkey": "f FOREIGN KEY (source_id) REFERENCES sources(id) ON DELETE CASCADE",@@ -757,7 +758,13 @@"4 scopes ARRAY/_text NULL","5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL",- "7 revoked_at timestamp with time zone/timestamptz NULL"+ "7 revoked_at timestamp with time zone/timestamptz NULL",+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json b/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.jsonindex df29c05ed7..f4232fc23f 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","calibration_profiles.calibration_profiles_pkey": "p PRIMARY KEY (id)","calibration_profiles.calibration_profiles_source_id_fkey": "f FOREIGN KEY (source_id) REFERENCES sources(id) ON DELETE CASCADE",@@ -757,7 +758,13 @@"4 scopes ARRAY/_text NULL","5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL",- "7 revoked_at timestamp with time zone/timestamptz NULL"+ "7 revoked_at timestamp with time zone/timestamptz NULL",+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json b/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.jsonindex 3a97f0e912..64c1bbf0d2 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json@@ -2,6 +2,7 @@"golden": {"constraints": {"access_tokens.access_tokens_pkey": "p PRIMARY KEY (id)",+ "access_tokens.access_tokens_source_grant_check": "c CHECK ((source_grant = ANY (ARRAY['default'::text, 'scalar'::text, 'federated'::text, 'none'::text])))","access_tokens.access_tokens_token_hash_key": "u UNIQUE (token_hash)","calibration_profiles.calibration_profiles_pkey": "p PRIMARY KEY (id)","calibration_profiles.calibration_profiles_source_id_fkey": "f FOREIGN KEY (source_id) REFERENCES sources(id) ON DELETE CASCADE",@@ -756,7 +757,13 @@"4 scopes ARRAY/_text NULL","5 created_at timestamp with time zone/timestamptz NULL DEFAULT now()","6 last_used_at timestamp with time zone/timestamptz NULL",- "7 revoked_at timestamp with time zone/timestamptz NULL"+ "7 revoked_at timestamp with time zone/timestamptz NULL",+ "8 source_grant text/text NULL",+ "9 source_id text/text NULL",+ "10 federated_read ARRAY/_text NULL",+ "11 allowed_operations ARRAY/_text NULL",+ "12 takes_holders ARRAY/_text NULL",+ "13 grant_revision integer/int4 NOT NULL DEFAULT 0"],"flags": "relkind=r rls=false force_rls=false"},diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.jsonindex 62e8b3e37c..fda86b40f4 100644--- a/test/fixtures/goldens/migrations/records.json+++ b/test/fixtures/goldens/migrations/records.json@@ -1,6 +1,6 @@{"golden": {- "count": 187,+ "count": 188,"gaps": [17,18,@@ -28,7 +28,7 @@"slugifyPath": "401dbca23580865d8b168dd8d84db4a6c708c6610d7cb61abafc7d0ee9809063","verifyProjectionStatistics": "59d9ccfea1c53384d8607874a51209a9c2dfd51b2b85a2cb6269fa4120faf1f3"},- "latest": 192,+ "latest": 193,"records": [{"handler": {@@ -2852,6 +2852,19 @@"transaction": "absent","verify": "absent","version": 192+ },+ {+ "handler": "absent",+ "idempotent": true,+ "name": "f3_access_token_grants",+ "sql": {+ "length": 526,+ "sha256": "bc3bdf2bd654361bc702eee485a7c80366c1600c83c29d351d32b7491ee7589b"+ },+ "sqlFor": "absent",+ "transaction": "absent",+ "verify": "absent",+ "version": 193}],"versions": [@@ -3041,7 +3054,8 @@189,190,191,- 192+ 192,+ 193]},"normalizer": "migrations-record-v1"