@pgpm/metaschema-modules 0.26.0 → 0.26.2

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 (42) hide show
  1. package/Makefile +1 -1
  2. package/deploy/schemas/metaschema_modules_public/tables/agent_module/table.sql +83 -0
  3. package/deploy/schemas/metaschema_modules_public/tables/billing_module/table.sql +5 -0
  4. package/deploy/schemas/metaschema_modules_public/tables/compute_log_module/table.sql +44 -0
  5. package/deploy/schemas/metaschema_modules_public/tables/config_secrets_user_module/table.sql +5 -1
  6. package/deploy/schemas/metaschema_modules_public/tables/db_usage_module/table.sql +52 -0
  7. package/deploy/schemas/metaschema_modules_public/tables/entity_type_provision/table.sql +107 -25
  8. package/deploy/schemas/metaschema_modules_public/tables/function_module/table.sql +92 -0
  9. package/deploy/schemas/metaschema_modules_public/tables/graph_module/table.sql +73 -0
  10. package/deploy/schemas/metaschema_modules_public/tables/inference_log_module/table.sql +7 -2
  11. package/deploy/schemas/metaschema_modules_public/tables/memberships_module/table.sql +3 -0
  12. package/deploy/schemas/metaschema_modules_public/tables/merkle_store_module/table.sql +55 -0
  13. package/deploy/schemas/metaschema_modules_public/tables/namespace_module/table.sql +71 -0
  14. package/deploy/schemas/metaschema_modules_public/tables/storage_log_module/table.sql +44 -0
  15. package/deploy/schemas/metaschema_modules_public/tables/storage_module/table.sql +11 -12
  16. package/deploy/schemas/metaschema_modules_public/tables/transfer_log_module/table.sql +44 -0
  17. package/deploy/schemas/metaschema_modules_public/tables/user_auth_module/table.sql +1 -1
  18. package/metaschema-modules.control +1 -1
  19. package/package.json +3 -3
  20. package/pgpm.plan +10 -0
  21. package/revert/schemas/metaschema_modules_public/tables/agent_module/table.sql +7 -0
  22. package/revert/schemas/metaschema_modules_public/tables/compute_log_module/table.sql +3 -0
  23. package/revert/schemas/metaschema_modules_public/tables/db_usage_module/table.sql +3 -0
  24. package/revert/schemas/metaschema_modules_public/tables/function_module/table.sql +7 -0
  25. package/revert/schemas/metaschema_modules_public/tables/graph_module/table.sql +7 -0
  26. package/revert/schemas/metaschema_modules_public/tables/merkle_store_module/table.sql +7 -0
  27. package/revert/schemas/metaschema_modules_public/tables/namespace_module/table.sql +7 -0
  28. package/revert/schemas/metaschema_modules_public/tables/storage_log_module/table.sql +3 -0
  29. package/revert/schemas/metaschema_modules_public/tables/transfer_log_module/table.sql +3 -0
  30. package/sql/{metaschema-modules--0.15.5.sql → metaschema-modules--0.26.1.sql} +760 -173
  31. package/verify/schemas/metaschema_modules_public/tables/agent_module/table.sql +7 -0
  32. package/verify/schemas/metaschema_modules_public/tables/billing_module/table.sql +2 -0
  33. package/verify/schemas/metaschema_modules_public/tables/compute_log_module/table.sql +9 -0
  34. package/verify/schemas/metaschema_modules_public/tables/db_usage_module/table.sql +10 -0
  35. package/verify/schemas/metaschema_modules_public/tables/function_module/table.sql +7 -0
  36. package/verify/schemas/metaschema_modules_public/tables/graph_module/table.sql +7 -0
  37. package/verify/schemas/metaschema_modules_public/tables/inference_log_module/table.sql +3 -2
  38. package/verify/schemas/metaschema_modules_public/tables/merkle_store_module/table.sql +7 -0
  39. package/verify/schemas/metaschema_modules_public/tables/namespace_module/table.sql +12 -0
  40. package/verify/schemas/metaschema_modules_public/tables/profiles_module/table.sql +2 -1
  41. package/verify/schemas/metaschema_modules_public/tables/storage_log_module/table.sql +10 -0
  42. package/verify/schemas/metaschema_modules_public/tables/transfer_log_module/table.sql +10 -0
package/Makefile CHANGED
@@ -1,5 +1,5 @@
1
1
  EXTENSION = metaschema-modules
2
- DATA = sql/metaschema-modules--0.15.5.sql
2
+ DATA = sql/metaschema-modules--0.26.1.sql
3
3
 
4
4
  PG_CONFIG = pg_config
5
5
  PGXS := $(shell $(PG_CONFIG) --pgxs)
@@ -0,0 +1,83 @@
1
+ -- Deploy schemas/metaschema_modules_public/tables/agent_module/table to pg
2
+
3
+ -- requires: schemas/metaschema_modules_public/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE TABLE metaschema_modules_public.agent_module (
8
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
9
+ database_id uuid NOT NULL,
10
+
11
+ -- Schema references (if uuid_nil, resolved from schema name or default)
12
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
13
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
14
+
15
+ -- Generated table IDs (populated by the generator)
16
+ thread_table_id uuid NOT NULL DEFAULT uuid_nil(),
17
+ message_table_id uuid NOT NULL DEFAULT uuid_nil(),
18
+ task_table_id uuid NOT NULL DEFAULT uuid_nil(),
19
+ prompts_table_id uuid NOT NULL DEFAULT uuid_nil(),
20
+ knowledge_table_id uuid DEFAULT NULL,
21
+
22
+ -- Table names (input to the generator)
23
+ thread_table_name text NOT NULL DEFAULT 'agent_thread',
24
+ message_table_name text NOT NULL DEFAULT 'agent_message',
25
+ task_table_name text NOT NULL DEFAULT 'agent_task',
26
+ prompts_table_name text NOT NULL DEFAULT 'agent_prompt',
27
+ knowledge_table_name text NOT NULL DEFAULT 'agent_knowledge',
28
+
29
+ -- Feature flags
30
+ has_knowledge boolean NOT NULL DEFAULT false,
31
+
32
+ -- API routing (get-or-create: if set, schema is added to this API; if NULL, no API is added)
33
+ api_name text DEFAULT 'agent',
34
+
35
+ -- Multi-tenant scope
36
+ membership_type int DEFAULT NULL,
37
+
38
+ -- Module key discriminator: allows multiple agent modules per scope.
39
+ -- 'default' is omitted from table names, any other value becomes
40
+ -- an infix: {prefix}_{key}_agent_thread.
41
+ -- Max 16 chars, lowercase snake_case.
42
+ key text NOT NULL DEFAULT 'default',
43
+
44
+ -- Entity table for RLS (NULL for app-level, entity table for entity-scoped)
45
+ entity_table_id uuid NULL,
46
+
47
+ -- Configurable security policies (NULL = use defaults based on membership_type)
48
+ policies jsonb NULL,
49
+
50
+ -- Knowledge RAG config (dimensions, chunk_size, chunk_strategy, search_indexes, etc.)
51
+ -- NULL = use sensible defaults (768d, 1000 chars, paragraph, bm25)
52
+ knowledge_config jsonb NULL,
53
+
54
+ -- Custom RLS policies for knowledge table (provisions.knowledge.policies)
55
+ -- NULL = use defaults (AuthzEntityMembership or AuthzAppMembership)
56
+ knowledge_policies jsonb NULL,
57
+
58
+ -- Per-table provisions overrides from blueprint config.
59
+ -- Keys are table keys (thread, message, task, prompt, knowledge).
60
+ -- When a key is present, the module trigger skips default security for that table;
61
+ -- secure_table_provision applies the custom grants/policies instead.
62
+ provisions jsonb NULL,
63
+
64
+ -- Constraints
65
+ CONSTRAINT agent_module_db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
66
+ CONSTRAINT agent_module_schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
67
+ CONSTRAINT agent_module_private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
68
+ CONSTRAINT agent_module_thread_table_fkey FOREIGN KEY (thread_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
69
+ CONSTRAINT agent_module_message_table_fkey FOREIGN KEY (message_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
70
+ CONSTRAINT agent_module_task_table_fkey FOREIGN KEY (task_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
71
+ CONSTRAINT agent_module_prompts_table_fkey FOREIGN KEY (prompts_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
72
+ CONSTRAINT agent_module_knowledge_table_fkey FOREIGN KEY (knowledge_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
73
+ CONSTRAINT agent_module_entity_table_fkey FOREIGN KEY (entity_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE
74
+ );
75
+
76
+ CREATE INDEX agent_module_database_id_idx ON metaschema_modules_public.agent_module ( database_id );
77
+
78
+ -- Unique constraint on (database_id, membership_type, key) using COALESCE to handle NULLs.
79
+ -- NULL membership_type = app-level, non-NULL = entity-scoped. key discriminates
80
+ -- multiple agent modules for the same scope (e.g. 'support' + 'internal').
81
+ CREATE UNIQUE INDEX agent_module_unique_scope ON metaschema_modules_public.agent_module ( database_id, COALESCE(membership_type, -1), key );
82
+
83
+ COMMIT;
@@ -31,6 +31,10 @@ CREATE TABLE metaschema_modules_public.billing_module (
31
31
  meter_credits_table_id uuid NOT NULL DEFAULT uuid_nil(),
32
32
  meter_credits_table_name text NOT NULL DEFAULT '',
33
33
 
34
+ -- Meter sources table: maps billing meters to typed daily summary table columns
35
+ meter_sources_table_id uuid NOT NULL DEFAULT uuid_nil(),
36
+ meter_sources_table_name text NOT NULL DEFAULT '',
37
+
34
38
  -- Generated functions
35
39
  record_usage_function text NOT NULL DEFAULT '',
36
40
 
@@ -44,6 +48,7 @@ CREATE TABLE metaschema_modules_public.billing_module (
44
48
  CONSTRAINT ledger_table_fkey FOREIGN KEY (ledger_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
45
49
  CONSTRAINT balances_table_fkey FOREIGN KEY (balances_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
46
50
  CONSTRAINT meter_credits_table_fkey FOREIGN KEY (meter_credits_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
51
+ CONSTRAINT meter_sources_table_fkey FOREIGN KEY (meter_sources_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
47
52
  CONSTRAINT billing_module_database_id_unique UNIQUE (database_id)
48
53
  );
49
54
 
@@ -0,0 +1,44 @@
1
+ -- Deploy schemas/metaschema_modules_public/tables/compute_log_module/table to pg
2
+
3
+ -- requires: schemas/metaschema_modules_public/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE TABLE metaschema_modules_public.compute_log_module (
8
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
9
+ database_id uuid NOT NULL,
10
+
11
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
12
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
13
+
14
+ -- Compute log table (partitioned by completed_at)
15
+ compute_log_table_id uuid NOT NULL DEFAULT uuid_nil(),
16
+ compute_log_table_name text NOT NULL DEFAULT '',
17
+
18
+ -- Pre-aggregated daily rollup table
19
+ usage_daily_table_id uuid NOT NULL DEFAULT uuid_nil(),
20
+ usage_daily_table_name text NOT NULL DEFAULT '',
21
+
22
+ -- Partition lifecycle configuration
23
+ "interval" text NOT NULL DEFAULT '1 month',
24
+ retention text NOT NULL DEFAULT '12 months',
25
+ premake int NOT NULL DEFAULT 2,
26
+
27
+ -- Scope configuration: 'app' = per-app usage (actor_id RLS), 'platform' = tenant metering (database_id RLS)
28
+ scope text NOT NULL DEFAULT 'app',
29
+ actor_fk_table_id uuid NULL,
30
+ entity_fk_table_id uuid NULL,
31
+
32
+ prefix text NULL,
33
+
34
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
35
+ CONSTRAINT schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
36
+ CONSTRAINT private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
37
+ CONSTRAINT compute_log_table_fkey FOREIGN KEY (compute_log_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
38
+ CONSTRAINT usage_daily_table_fkey FOREIGN KEY (usage_daily_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
39
+ CONSTRAINT compute_log_module_database_id_prefix_unique UNIQUE NULLS NOT DISTINCT (database_id, prefix)
40
+ );
41
+
42
+ CREATE INDEX compute_log_module_database_id_idx ON metaschema_modules_public.compute_log_module ( database_id );
43
+
44
+ COMMIT;
@@ -12,11 +12,15 @@ CREATE TABLE metaschema_modules_public.config_secrets_user_module (
12
12
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
13
13
  table_id uuid NOT NULL DEFAULT uuid_nil(),
14
14
  table_name text NOT NULL DEFAULT 'user_secrets',
15
+
16
+ -- Config definitions table ID (populated by the generator)
17
+ config_definitions_table_id uuid NOT NULL DEFAULT uuid_nil(),
15
18
  --
16
19
 
17
20
  CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
18
21
  CONSTRAINT schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
19
- CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE
22
+ CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
23
+ CONSTRAINT config_defs_table_fkey FOREIGN KEY (config_definitions_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE
20
24
  );
21
25
 
22
26
  CREATE INDEX config_secrets_user_module_database_id_idx ON metaschema_modules_public.config_secrets_user_module ( database_id );
@@ -0,0 +1,52 @@
1
+ -- Deploy schemas/metaschema_modules_public/tables/db_usage_module/table to pg
2
+
3
+ -- requires: schemas/metaschema_modules_public/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE TABLE metaschema_modules_public.db_usage_module (
8
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
9
+ database_id uuid NOT NULL,
10
+
11
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
12
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
13
+
14
+ -- DB table stats log (partitioned — per-table reads/writes/size from pg_stat_user_tables)
15
+ table_stats_log_table_id uuid NOT NULL DEFAULT uuid_nil(),
16
+ table_stats_log_table_name text NOT NULL DEFAULT '',
17
+
18
+ -- DB table stats daily rollup
19
+ table_stats_daily_table_id uuid NOT NULL DEFAULT uuid_nil(),
20
+ table_stats_daily_table_name text NOT NULL DEFAULT '',
21
+
22
+ -- DB query stats log (partitioned — query execution time from pg_stat_statements)
23
+ query_stats_log_table_id uuid NOT NULL DEFAULT uuid_nil(),
24
+ query_stats_log_table_name text NOT NULL DEFAULT '',
25
+
26
+ -- DB query stats daily rollup
27
+ query_stats_daily_table_id uuid NOT NULL DEFAULT uuid_nil(),
28
+ query_stats_daily_table_name text NOT NULL DEFAULT '',
29
+
30
+ -- Partition lifecycle configuration
31
+ "interval" text NOT NULL DEFAULT '1 month',
32
+ retention text NOT NULL DEFAULT '12 months',
33
+ premake int NOT NULL DEFAULT 2,
34
+
35
+ -- Scope configuration: 'app' = per-app usage, 'platform' = tenant metering (database_id RLS)
36
+ scope text NOT NULL DEFAULT 'app',
37
+
38
+ prefix text NULL,
39
+
40
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
41
+ CONSTRAINT schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
42
+ CONSTRAINT private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
43
+ CONSTRAINT table_stats_log_table_fkey FOREIGN KEY (table_stats_log_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
44
+ CONSTRAINT table_stats_daily_table_fkey FOREIGN KEY (table_stats_daily_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
45
+ CONSTRAINT query_stats_log_table_fkey FOREIGN KEY (query_stats_log_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
46
+ CONSTRAINT query_stats_daily_table_fkey FOREIGN KEY (query_stats_daily_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
47
+ CONSTRAINT db_usage_module_database_id_prefix_unique UNIQUE NULLS NOT DISTINCT (database_id, prefix)
48
+ );
49
+
50
+ CREATE INDEX db_usage_module_database_id_idx ON metaschema_modules_public.db_usage_module ( database_id );
51
+
52
+ COMMIT;
@@ -47,8 +47,6 @@ CREATE TABLE metaschema_modules_public.entity_type_provision (
47
47
 
48
48
  has_levels boolean NOT NULL DEFAULT false,
49
49
 
50
- has_storage boolean NOT NULL DEFAULT false,
51
-
52
50
  has_invites boolean NOT NULL DEFAULT false,
53
51
 
54
52
  has_invite_achievements boolean NOT NULL DEFAULT false,
@@ -56,11 +54,27 @@ CREATE TABLE metaschema_modules_public.entity_type_provision (
56
54
  -- =========================================================================
57
55
  -- Storage configuration: JSON array of storage module definitions.
58
56
  -- Each element provisions a separate storage module with its own tables,
59
- -- RLS policies, and feature flags. Only used when has_storage = true.
60
- -- NULL = provision a single default storage module with default settings.
57
+ -- RLS policies, and feature flags. Presence triggers provisioning
58
+ -- (same inference model as namespaces, functions, agents).
59
+ -- NULL = do not provision. '[{}]' = provision one default storage module.
60
+ -- =========================================================================
61
+
62
+ storage jsonb DEFAULT NULL,
63
+
64
+ -- =========================================================================
65
+ -- Module configuration arrays: presence triggers provisioning.
66
+ -- Each is a JSON array of module definitions (like storage).
67
+ -- NULL = do not provision. '[{}]' = provision one default instance.
68
+ -- Each element may include "key" (discriminator) and "policies" (override).
61
69
  -- =========================================================================
62
70
 
63
- storage_config jsonb DEFAULT NULL,
71
+ namespaces jsonb DEFAULT NULL,
72
+
73
+ functions jsonb DEFAULT NULL,
74
+
75
+ graphs jsonb DEFAULT NULL,
76
+
77
+ agents jsonb DEFAULT NULL,
64
78
 
65
79
  -- =========================================================================
66
80
  -- Escape hatch: skip default entity table RLS policies
@@ -107,6 +121,32 @@ CREATE TABLE metaschema_modules_public.entity_type_provision (
107
121
 
108
122
  out_invites_module_id uuid DEFAULT NULL,
109
123
 
124
+ out_namespace_module_id uuid DEFAULT NULL,
125
+
126
+ out_namespaces_table_id uuid DEFAULT NULL,
127
+
128
+ out_namespace_events_table_id uuid DEFAULT NULL,
129
+
130
+ out_function_module_id uuid DEFAULT NULL,
131
+
132
+ out_definitions_table_id uuid DEFAULT NULL,
133
+
134
+ out_invocations_table_id uuid DEFAULT NULL,
135
+
136
+ out_execution_logs_table_id uuid DEFAULT NULL,
137
+
138
+ out_secret_definitions_table_id uuid DEFAULT NULL,
139
+
140
+ out_requirements_table_id uuid DEFAULT NULL,
141
+
142
+ out_config_requirements_table_id uuid DEFAULT NULL,
143
+
144
+ out_graph_module_id uuid DEFAULT NULL,
145
+
146
+ out_graphs_table_id uuid DEFAULT NULL,
147
+
148
+ out_agent_module_id uuid DEFAULT NULL,
149
+
110
150
  -- =========================================================================
111
151
  -- Constraints
112
152
  -- =========================================================================
@@ -205,20 +245,14 @@ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_levels IS
205
245
  Levels provide gamification/achievement tracking for members.
206
246
  When true, creates level steps, achievements, and level tables with security.';
207
247
 
208
- COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_storage IS
209
- 'Whether to provision storage_module for this type. Defaults to false.
210
- When true, creates {prefix}_buckets and {prefix}_files tables
211
- with entity-scoped RLS (AuthzEntityMembership) using the entity''s membership_type.
212
- Storage tables get owner_id FK to the entity table, so files are owned by the entity.';
213
-
214
248
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_invites IS
215
249
  'Whether to provision invites_module for this type. Defaults to false.
216
250
  When true, the trigger inserts a row into invites_module which in turn
217
251
  (via insert_invites_module BEFORE INSERT) creates {prefix}_invites and
218
252
  {prefix}_claimed_invites tables plus the submit_{prefix}_invite_code() function.
219
- Symmetric counterpart of has_storage. Re-provisioning is idempotent: the
220
- UNIQUE (database_id, membership_type) constraint on invites_module combined with
221
- ON CONFLICT DO NOTHING in the fan-out makes repeated INSERTs safe.';
253
+ Re-provisioning is idempotent: the UNIQUE (database_id, membership_type) constraint
254
+ on invites_module combined with ON CONFLICT DO NOTHING in the fan-out makes
255
+ repeated INSERTs safe.';
222
256
 
223
257
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_invite_achievements IS
224
258
  'Whether to auto-attach an EventTracker to the claimed_invites table for invite-based
@@ -296,15 +330,17 @@ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_installed_
296
330
  'Output: array of installed module labels (e.g. ARRAY[''permissions_module:data_room'', ''memberships_module:data_room'', ''invites_module:data_room'']).
297
331
  Populated by the trigger. Useful for verifying which modules were provisioned.';
298
332
 
299
- COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.storage_config IS
300
- 'Optional JSON array of storage module definitions. Each element provisions a separate
301
- storage module with its own tables ({prefix}_{storage_key}_buckets/files), RLS policies,
302
- and feature flags. Only used when has_storage = true; ignored otherwise.
303
- NULL = provision a single default storage module with all defaults.
333
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.storage IS
334
+ 'Optional JSON array of storage module definitions. Presence triggers provisioning
335
+ (same inference model as namespaces, functions, agents).
336
+ Each element provisions a separate storage module with its own tables
337
+ ({prefix}_{key}_buckets/files), RLS policies, and feature flags.
338
+ NULL = do not provision storage. ''[{}]'' = provision one default storage module.
304
339
  Each array element recognizes (all optional):
305
- - storage_key (text) module discriminator, max 16 chars, lowercase snake_case.
340
+ - key (text) module discriminator, max 16 chars, lowercase snake_case.
306
341
  Defaults to ''default'' (omitted from table names).
307
342
  Non-default keys become infixes: {prefix}_{key}_buckets.
343
+ (storage_key accepted for backward compat)
308
344
  - upload_url_expiry_seconds (integer) presigned PUT URL expiry override
309
345
  - download_url_expiry_seconds (integer) presigned GET URL expiry override
310
346
  - default_max_file_size (bigint) global max file size in bytes for this module
@@ -322,22 +358,68 @@ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.storage_config
322
358
  - provisions (jsonb object) per-table customization keyed by "files" or "buckets".
323
359
  Each value: { nodes, fields, grants, use_rls, policies }.
324
360
  Example (single module, backward compat):
325
- storage_config := ''[{"buckets": [{"name": "documents"}]}]''::jsonb
361
+ storage := ''[{"buckets": [{"name": "documents"}]}]''::jsonb
326
362
  Example (multi-module):
327
- storage_config := ''[{"has_path_shares": true, "buckets": [{"name": "documents"}]}, {"storage_key": "fn", "has_custom_keys": true, "buckets": [{"name": "functions"}]}]''::jsonb';
363
+ storage := ''[{"has_path_shares": true, "buckets": [{"name": "documents"}]}, {"key": "fn", "has_custom_keys": true, "buckets": [{"name": "functions"}]}]''::jsonb';
328
364
 
329
365
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_storage_module_id IS
330
- 'Output: the UUID of the storage_module row created for this entity type. Populated by the trigger when has_storage=true.';
366
+ 'Output: the UUID of the storage_module row created for this entity type. Populated by the trigger when storage is non-NULL and non-empty.';
331
367
 
332
368
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_buckets_table_id IS
333
- 'Output: the UUID of the generated buckets table (e.g. data_room_buckets). Populated by the trigger when has_storage=true.';
369
+ 'Output: the UUID of the generated buckets table (e.g. data_room_buckets). Populated by the trigger when storage is non-NULL and non-empty.';
334
370
 
335
371
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_files_table_id IS
336
- 'Output: the UUID of the generated files table (e.g. data_room_files). Populated by the trigger when has_storage=true.';
372
+ 'Output: the UUID of the generated files table (e.g. data_room_files). Populated by the trigger when storage is non-NULL and non-empty.';
337
373
 
338
374
  COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_invites_module_id IS
339
375
  'Output: the UUID of the invites_module row created for this entity type. Populated by the trigger when has_invites=true.
340
376
  NULL when has_invites=false, or when re-provisioning hits ON CONFLICT DO NOTHING
341
377
  (i.e. the invites_module row was created in a previous run).';
342
378
 
379
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.namespaces IS
380
+ 'Optional JSON array of namespace module definitions. Presence triggers provisioning.
381
+ NULL = do not provision namespaces. ''[{}]'' = provision one default namespace module.
382
+ Each element recognizes (all optional):
383
+ - key (text) module discriminator. Defaults to ''default''.
384
+ - policies (jsonb array) RLS policy overrides. NULL = apply defaults from apply_namespace_security().
385
+ Creates {prefix}_namespaces (or {prefix}_{key}_namespaces for non-default keys)
386
+ with entity-scoped RLS (AuthzEntityMembership) and a rename proxy trigger.
387
+ Registers manage_namespaces permission bit on first provision.
388
+ Example: namespaces := ''[{}]''::jsonb';
389
+
390
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.functions IS
391
+ 'Optional JSON array of function module definitions. Presence triggers provisioning.
392
+ NULL = do not provision functions. ''[{}]'' = provision one default function module.
393
+ Each element recognizes (all optional):
394
+ - key (text) module discriminator. Defaults to ''default''.
395
+ - policies (jsonb array) RLS policy overrides. NULL = apply defaults from apply_function_security().
396
+ Creates {prefix}_function_definitions (or {prefix}_{key}_function_definitions for non-default keys)
397
+ with entity-scoped RLS and a job trigger dispatching function:provision tasks.
398
+ Registers manage_functions + invoke_functions permission bits on first provision.
399
+ Example: functions := ''[{}]''::jsonb';
400
+
401
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.graphs IS
402
+ 'Optional JSON array of graph module definitions. Presence triggers provisioning.
403
+ NULL = do not provision graphs. ''[{}]'' = provision one default graph module.
404
+ Each element recognizes (all optional):
405
+ - key (text) module discriminator. Defaults to ''default''.
406
+ - policies (jsonb array) RLS policy overrides. NULL = apply defaults from apply_graph_security().
407
+ Registers manage_graphs + execute_graphs permission bits on first provision.
408
+ Graph module requires a merkle_store_module_id dependency, so entity_type_provision
409
+ only registers permissions here. The graph module itself must be provisioned
410
+ separately with the merkle store dependency resolved.
411
+ Example: graphs := ''[{}]''::jsonb';
412
+
413
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_namespace_module_id IS
414
+ 'Output: the UUID of the namespace_module row created (or found) for this entity type.
415
+ Populated by the trigger when namespaces is non-NULL. NULL otherwise.';
416
+
417
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_namespaces_table_id IS
418
+ 'Output: the UUID of the generated namespaces table (e.g. data_room_namespaces).
419
+ Populated by the trigger when namespaces is non-NULL. NULL otherwise.';
420
+
421
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_namespace_events_table_id IS
422
+ 'Output: the UUID of the generated namespace_events partitioned table (e.g. data_room_namespace_events).
423
+ Monthly partitioned, 12-month retention. Populated by the trigger when namespaces is non-NULL. NULL otherwise.';
424
+
343
425
  COMMIT;
@@ -0,0 +1,92 @@
1
+ -- Deploy schemas/metaschema_modules_public/tables/function_module/table to pg
2
+
3
+ -- requires: schemas/metaschema_modules_public/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE TABLE metaschema_modules_public.function_module (
8
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
9
+ database_id uuid NOT NULL,
10
+
11
+ -- Schema references (if uuid_nil, resolved from schema name or default)
12
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
13
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
14
+
15
+ -- Optional schema name overrides (used when schema IDs are not provided)
16
+ public_schema_name text,
17
+ private_schema_name text,
18
+
19
+ -- Generated table IDs (populated by the generator)
20
+ definitions_table_id uuid NOT NULL DEFAULT uuid_nil(),
21
+ invocations_table_id uuid NOT NULL DEFAULT uuid_nil(),
22
+ execution_logs_table_id uuid NOT NULL DEFAULT uuid_nil(),
23
+ secret_definitions_table_id uuid NOT NULL DEFAULT uuid_nil(),
24
+ requirements_table_id uuid NOT NULL DEFAULT uuid_nil(),
25
+ config_definitions_table_id uuid NOT NULL DEFAULT uuid_nil(),
26
+ config_requirements_table_id uuid NOT NULL DEFAULT uuid_nil(),
27
+
28
+ -- Table names (input to the generator — bare names without scope prefix).
29
+ -- The trigger prepends the scope prefix automatically.
30
+ definitions_table_name text NOT NULL DEFAULT 'function_definitions',
31
+ invocations_table_name text NOT NULL DEFAULT 'function_invocations',
32
+ execution_logs_table_name text NOT NULL DEFAULT 'function_execution_logs',
33
+ secret_definitions_table_name text NOT NULL DEFAULT 'secret_definitions',
34
+ requirements_table_name text NOT NULL DEFAULT 'function_secret_requirements',
35
+ config_requirements_table_name text NOT NULL DEFAULT 'function_config_requirements',
36
+
37
+ -- API routing (get-or-create: if set, schema is added to this API; if NULL, no API is added)
38
+ api_name text,
39
+ private_api_name text,
40
+
41
+ -- Multi-tenant function identity
42
+ membership_type int DEFAULT NULL, -- NULL = database-root (AuthzMembership via app_sprt), non-NULL = entity-scoped (AuthzEntityMembership)
43
+
44
+ -- Scope prefix for table naming. Auto-derived from membership_type when
45
+ -- NULL: NULL/1 → 'app', 2 → 'org'. Can be overridden explicitly.
46
+ -- The trigger prepends this to all bare table names
47
+ -- (e.g. prefix='app' + 'function_definitions' → 'app_function_definitions').
48
+ prefix text NULL,
49
+
50
+ -- Module key discriminator: allows multiple function modules per scope.
51
+ -- 'default' is omitted from table names, any other value becomes
52
+ -- an infix: {prefix}_{key}_function_definitions.
53
+ -- Max 16 chars, lowercase snake_case.
54
+ key text NOT NULL DEFAULT 'default',
55
+
56
+ -- Entity table for RLS (NULL for app-level functions, entity table for entity-scoped functions)
57
+ entity_table_id uuid NULL,
58
+
59
+ -- Configurable security policies (NULL = use defaults based on membership_type).
60
+ -- When provided, replaces the default policy set in apply_function_security.
61
+ -- Accepts a JSON array of policy objects:
62
+ -- {"$type": "AuthzEntityMembership", "privileges": ["select", "update"], "data": {...}}
63
+ policies jsonb NULL,
64
+
65
+ -- Per-table provisions overrides from blueprint config.
66
+ -- Keys are table keys (definitions, invocations, execution_logs, secret_definitions, requirements).
67
+ -- When a key is present, the module trigger skips default security for that table;
68
+ -- secure_table_provision applies the custom grants/policies instead.
69
+ provisions jsonb NULL,
70
+
71
+ -- Constraints
72
+ CONSTRAINT function_module_db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
73
+ CONSTRAINT function_module_schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
74
+ CONSTRAINT function_module_private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
75
+ CONSTRAINT function_module_definitions_table_fkey FOREIGN KEY (definitions_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
76
+ CONSTRAINT function_module_invocations_table_fkey FOREIGN KEY (invocations_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
77
+ CONSTRAINT function_module_execution_logs_table_fkey FOREIGN KEY (execution_logs_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
78
+ CONSTRAINT function_module_secret_defs_table_fkey FOREIGN KEY (secret_definitions_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
79
+ CONSTRAINT function_module_requirements_table_fkey FOREIGN KEY (requirements_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
80
+ CONSTRAINT function_module_config_defs_table_fkey FOREIGN KEY (config_definitions_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
81
+ CONSTRAINT function_module_config_reqs_table_fkey FOREIGN KEY (config_requirements_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
82
+ CONSTRAINT function_module_entity_table_fkey FOREIGN KEY (entity_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE
83
+ );
84
+
85
+ CREATE INDEX function_module_database_id_idx ON metaschema_modules_public.function_module ( database_id );
86
+
87
+ -- Unique constraint on (database_id, membership_type, key) using COALESCE to handle NULLs.
88
+ -- NULL membership_type = app-level, non-NULL = entity-scoped. key discriminates
89
+ -- multiple function modules for the same scope (e.g. 'webhooks' + 'automations').
90
+ CREATE UNIQUE INDEX function_module_unique_scope ON metaschema_modules_public.function_module ( database_id, COALESCE(membership_type, -1), key );
91
+
92
+ COMMIT;
@@ -0,0 +1,73 @@
1
+ -- Deploy schemas/metaschema_modules_public/tables/graph_module/table to pg
2
+
3
+ -- requires: schemas/metaschema_modules_public/schema
4
+ -- requires: schemas/metaschema_modules_public/tables/merkle_store_module/table
5
+
6
+ BEGIN;
7
+
8
+ CREATE TABLE metaschema_modules_public.graph_module (
9
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
10
+ database_id uuid NOT NULL,
11
+
12
+ -- Schema references (if uuid_nil, resolved from schema name or default)
13
+ public_schema_id uuid NOT NULL DEFAULT uuid_nil(),
14
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
15
+
16
+ -- Optional schema name overrides (used when schema IDs are not provided)
17
+ public_schema_name text,
18
+ private_schema_name text,
19
+
20
+ -- Table/function prefix (e.g., 'pipeline' -> pipeline_function_graphs, ...)
21
+ -- Stored normalized (no trailing underscore); underscore added at generation time
22
+ prefix text NOT NULL DEFAULT '',
23
+
24
+ -- Reference to the Merkle store this graph module depends on
25
+ merkle_store_module_id uuid NOT NULL,
26
+
27
+ -- Generated table IDs (populated by BEFORE INSERT trigger)
28
+ graphs_table_id uuid NOT NULL DEFAULT uuid_nil(),
29
+ executions_table_id uuid NOT NULL DEFAULT uuid_nil(),
30
+ outputs_table_id uuid NOT NULL DEFAULT uuid_nil(),
31
+
32
+ -- API routing (get-or-create: if set, schema is added to this API; if NULL, no API is added)
33
+ api_name text,
34
+ private_api_name text,
35
+
36
+ -- Scope field name (column used for multi-tenant isolation)
37
+ scope_field text NOT NULL DEFAULT 'scope_id',
38
+
39
+ -- Multi-tenant scoping (entity-aware module pattern)
40
+ membership_type int DEFAULT NULL, -- NULL = database-root, non-NULL = entity-scoped
41
+ entity_table_id uuid NULL, -- Entity table for entity-scoped RLS
42
+
43
+ -- Configurable security policies (NULL = use defaults).
44
+ -- Accepts a JSON array of policy objects:
45
+ -- {"$type": "AuthzEntityMembership", "privileges": ["select", "update"], "data": {...}}
46
+ policies jsonb NULL,
47
+
48
+ -- Per-table provisions overrides from blueprint config.
49
+ -- Keys are table keys (graphs, executions, outputs).
50
+ -- When a key is present, the module trigger skips default security for that table;
51
+ -- secure_table_provision applies the custom grants/policies instead.
52
+ provisions jsonb NULL,
53
+
54
+ -- Timestamps
55
+ created_at timestamptz NOT NULL DEFAULT now(),
56
+
57
+ -- Constraints
58
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
59
+ CONSTRAINT public_schema_fkey FOREIGN KEY (public_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
60
+ CONSTRAINT private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
61
+ CONSTRAINT merkle_store_fkey FOREIGN KEY (merkle_store_module_id) REFERENCES metaschema_modules_public.merkle_store_module (id) ON DELETE CASCADE,
62
+ CONSTRAINT graphs_table_fkey FOREIGN KEY (graphs_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
63
+ CONSTRAINT executions_table_fkey FOREIGN KEY (executions_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
64
+ CONSTRAINT outputs_table_fkey FOREIGN KEY (outputs_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
65
+ CONSTRAINT graph_module_entity_table_fkey FOREIGN KEY (entity_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
66
+
67
+ -- Only one graph module per database + merkle store combination
68
+ CONSTRAINT graph_module_database_merkle_unique UNIQUE (database_id, merkle_store_module_id)
69
+ );
70
+
71
+ CREATE INDEX graph_module_database_id_idx ON metaschema_modules_public.graph_module ( database_id );
72
+
73
+ COMMIT;
@@ -21,9 +21,14 @@ CREATE TABLE metaschema_modules_public.inference_log_module (
21
21
 
22
22
  -- Partition lifecycle configuration
23
23
  "interval" text NOT NULL DEFAULT '1 month',
24
- retention text NULL,
24
+ retention text NOT NULL DEFAULT '12 months',
25
25
  premake int NOT NULL DEFAULT 2,
26
26
 
27
+ -- Scope configuration: 'app' = per-app usage (actor_id RLS), 'platform' = tenant metering (database_id RLS)
28
+ scope text NOT NULL DEFAULT 'app',
29
+ actor_fk_table_id uuid NULL,
30
+ entity_fk_table_id uuid NULL,
31
+
27
32
  prefix text NULL,
28
33
 
29
34
  CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
@@ -31,7 +36,7 @@ CREATE TABLE metaschema_modules_public.inference_log_module (
31
36
  CONSTRAINT private_schema_fkey FOREIGN KEY (private_schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
32
37
  CONSTRAINT inference_log_table_fkey FOREIGN KEY (inference_log_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
33
38
  CONSTRAINT usage_daily_table_fkey FOREIGN KEY (usage_daily_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
34
- CONSTRAINT inference_log_module_database_id_unique UNIQUE (database_id)
39
+ CONSTRAINT inference_log_module_database_id_prefix_unique UNIQUE NULLS NOT DISTINCT (database_id, prefix)
35
40
  );
36
41
 
37
42
  CREATE INDEX inference_log_module_database_id_idx ON metaschema_modules_public.inference_log_module ( database_id );
@@ -50,6 +50,9 @@ CREATE TABLE metaschema_modules_public.memberships_module (
50
50
 
51
51
  prefix text NULL,
52
52
 
53
+ -- Populated by memberships_module generator when get_organization_id is created
54
+ get_org_fn text NULL,
55
+
53
56
  --
54
57
 
55
58
  actor_mask_check text NOT NULL DEFAULT '',