@opengeni/db 3.6.0 → 3.7.3-canary.0
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.
- package/dist/canonical-human-identities.js +3 -3
- package/dist/{chunk-744DFC65.js → chunk-4NAR7FBA.js} +92 -6
- package/dist/chunk-4NAR7FBA.js.map +1 -0
- package/dist/{chunk-IW6O6G7R.js → chunk-5NNCGFQY.js} +2 -2
- package/dist/{chunk-G3732M3T.js → chunk-DZCYW2WT.js} +2 -2
- package/dist/{chunk-WW7AJ5IC.js → chunk-F6PBS6WE.js} +5 -7
- package/dist/chunk-F6PBS6WE.js.map +1 -0
- package/dist/{chunk-UJ2EW3QI.js → chunk-O56DM5GZ.js} +340 -85
- package/dist/chunk-O56DM5GZ.js.map +1 -0
- package/dist/{chunk-CDMMSUWJ.js → chunk-S3TDDCS2.js} +3 -3
- package/dist/{chunk-Q5YUGQVK.js → chunk-SZAWDXQC.js} +3 -3
- package/dist/{chunk-UGDVSQ6U.js → chunk-U4WPPEBH.js} +2 -2
- package/dist/{chunk-2NT476HD.js → chunk-US4IS6IV.js} +2 -2
- package/dist/codex-token-resolver.d.ts +12 -1
- package/dist/company-profile-agent-admin.d.ts +4 -2
- package/dist/company-profile-agent-policy.d.ts +20 -0
- package/dist/company-profile-schema.d.ts +573 -0
- package/dist/editable-artifact-durable-export.js +2 -2
- package/dist/editable-artifacts.js +3 -3
- package/dist/index.d.ts +382 -18
- package/dist/index.js +4513 -2065
- package/dist/index.js.map +1 -1
- package/dist/managed-auth-session-sets.js +3 -3
- package/dist/managed-human-provisioning.d.ts +3 -3
- package/dist/memory-domain.d.ts +5 -3
- package/dist/provision-roles.js +1 -1
- package/dist/runtime-posture.d.ts +5 -5
- package/dist/schema.d.ts +725 -4
- package/dist/schema.js +13 -1
- package/dist/session-background-commands.js +3 -3
- package/dist/session-control.d.ts +40 -5
- package/dist/session-queue-commands.d.ts +2 -0
- package/dist/session-tenancy.js +3 -3
- package/dist/user-resource-authority.d.ts +8 -0
- package/dist/video-generation.js +3 -3
- package/drizzle/0369_local_human_personal_authority.sql +350 -0
- package/drizzle/0370_workspace_member_management_scope.sql +98 -0
- package/drizzle/0371_workspace_member_candidate_inventory.sql +128 -0
- package/drizzle/0372_ordered_variable_set_runtime_authority.sql +287 -0
- package/drizzle/0373_sandbox_shared_preparation.sql +59 -0
- package/drizzle/0374_session_event_cursors.sql +315 -0
- package/drizzle/0375_session_recovery_observability.sql +73 -0
- package/drizzle/0376_organization_workspace_inventory.sql +88 -0
- package/drizzle/0377_scoped_rig_health_projection.sql +80 -0
- package/drizzle/0378_scoped_rig_health_audit_timestamp.sql +77 -0
- package/drizzle/0379_session_event_raw_lane_activation.sql +518 -0
- package/drizzle/0380_autonomous_company_profile_agent_policy.sql +829 -0
- package/drizzle/0381_organization_codex_subscription_inheritance.sql +720 -0
- package/drizzle/0382_organization_api_key_provenance.sql +91 -0
- package/drizzle/0383_codex_cooldown_reconciliation.sql +51 -0
- package/drizzle/0384_codex_cooldown_revision_guard_privileges.sql +6 -0
- package/drizzle/0385_connected_machine_legacy_tilde_workdirs.sql +25 -0
- package/drizzle/0386_local_organization_administration.sql +157 -0
- package/package.json +6 -6
- package/src/codex-token-resolver.ts +26 -1
- package/src/company-brain-governed-writes.ts +30 -1
- package/src/company-profile-agent-admin.ts +68 -1
- package/src/company-profile-agent-policy.ts +126 -0
- package/src/company-profile-schema.ts +115 -1
- package/src/database.ts +3 -5
- package/src/index.ts +3626 -456
- package/src/managed-human-provisioning.ts +3 -3
- package/src/memory-domain.ts +9 -4
- package/src/organization-membership-lifecycle.ts +0 -1
- package/src/provision-roles.ts +83 -4
- package/src/runtime-posture.ts +22 -0
- package/src/schema.ts +161 -8
- package/src/session-control.ts +177 -59
- package/src/session-queue-commands.ts +146 -40
- package/src/session-tool-call-settlement.ts +34 -7
- package/src/user-resource-authority.ts +56 -0
- package/dist/chunk-744DFC65.js.map +0 -1
- package/dist/chunk-UJ2EW3QI.js.map +0 -1
- package/dist/chunk-WW7AJ5IC.js.map +0 -1
- /package/dist/{chunk-IW6O6G7R.js.map → chunk-5NNCGFQY.js.map} +0 -0
- /package/dist/{chunk-G3732M3T.js.map → chunk-DZCYW2WT.js.map} +0 -0
- /package/dist/{chunk-CDMMSUWJ.js.map → chunk-S3TDDCS2.js.map} +0 -0
- /package/dist/{chunk-Q5YUGQVK.js.map → chunk-SZAWDXQC.js.map} +0 -0
- /package/dist/{chunk-UGDVSQ6U.js.map → chunk-U4WPPEBH.js.map} +0 -0
- /package/dist/{chunk-2NT476HD.js.map → chunk-US4IS6IV.js.map} +0 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Workspace member managers may enumerate only active same-organization humans
|
|
3
|
+
-- who do not already have access to their exact shared workspace.
|
|
4
|
+
|
|
5
|
+
SET LOCAL lock_timeout = '5s';
|
|
6
|
+
SET LOCAL statement_timeout = '10min';
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION list_workspace_member_management_candidates(
|
|
9
|
+
p_account_id uuid,
|
|
10
|
+
p_workspace_id uuid,
|
|
11
|
+
p_actor_subject_id text
|
|
12
|
+
) RETURNS jsonb
|
|
13
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
14
|
+
SET search_path FROM CURRENT
|
|
15
|
+
AS $body$
|
|
16
|
+
DECLARE
|
|
17
|
+
previous_lifecycle text := current_setting('opengeni.organization_tenancy_lifecycle', true);
|
|
18
|
+
result jsonb;
|
|
19
|
+
BEGIN
|
|
20
|
+
IF p_account_id IS NULL OR p_workspace_id IS NULL
|
|
21
|
+
OR nullif(pg_catalog.btrim(p_actor_subject_id), '') IS NULL
|
|
22
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
23
|
+
OR p_workspace_id IS DISTINCT FROM opengeni_private.current_workspace_id()
|
|
24
|
+
OR p_actor_subject_id IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
25
|
+
THEN
|
|
26
|
+
RAISE EXCEPTION 'workspace member management authority required' USING ERRCODE = '42501';
|
|
27
|
+
END IF;
|
|
28
|
+
|
|
29
|
+
PERFORM pg_catalog.set_config(
|
|
30
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
31
|
+
'organization_membership_lifecycle', true
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
IF NOT EXISTS (
|
|
35
|
+
SELECT 1 FROM workspaces workspace
|
|
36
|
+
WHERE workspace.id = p_workspace_id AND workspace.account_id = p_account_id
|
|
37
|
+
) THEN
|
|
38
|
+
RAISE EXCEPTION 'workspace not found' USING ERRCODE = 'P0002';
|
|
39
|
+
END IF;
|
|
40
|
+
|
|
41
|
+
IF EXISTS (
|
|
42
|
+
SELECT 1 FROM organization_memberships membership
|
|
43
|
+
WHERE membership.account_id = p_account_id
|
|
44
|
+
AND membership.personal_workspace_id = p_workspace_id
|
|
45
|
+
) THEN
|
|
46
|
+
RAISE EXCEPTION 'Personal workspace membership is owner-only' USING ERRCODE = '42501';
|
|
47
|
+
END IF;
|
|
48
|
+
|
|
49
|
+
IF NOT EXISTS (
|
|
50
|
+
SELECT 1
|
|
51
|
+
FROM organization_memberships organization_membership
|
|
52
|
+
JOIN workspace_memberships workspace_membership
|
|
53
|
+
ON workspace_membership.account_id = organization_membership.account_id
|
|
54
|
+
AND workspace_membership.subject_id = organization_membership.subject_id
|
|
55
|
+
AND workspace_membership.workspace_id = p_workspace_id
|
|
56
|
+
WHERE organization_membership.account_id = p_account_id
|
|
57
|
+
AND organization_membership.subject_id = p_actor_subject_id
|
|
58
|
+
AND organization_membership.status = 'active'
|
|
59
|
+
AND (
|
|
60
|
+
workspace_membership.permissions ? 'members:manage'
|
|
61
|
+
OR workspace_membership.permissions ? 'workspace:admin'
|
|
62
|
+
)
|
|
63
|
+
) THEN
|
|
64
|
+
RAISE EXCEPTION 'workspace member management authority required' USING ERRCODE = '42501';
|
|
65
|
+
END IF;
|
|
66
|
+
|
|
67
|
+
IF (
|
|
68
|
+
SELECT count(*)
|
|
69
|
+
FROM organization_memberships membership
|
|
70
|
+
WHERE membership.account_id = p_account_id
|
|
71
|
+
AND membership.status = 'active'
|
|
72
|
+
AND membership.subject_id LIKE 'user:%'
|
|
73
|
+
AND NOT EXISTS (
|
|
74
|
+
SELECT 1 FROM workspace_memberships access
|
|
75
|
+
WHERE access.account_id = p_account_id
|
|
76
|
+
AND access.workspace_id = p_workspace_id
|
|
77
|
+
AND access.subject_id = membership.subject_id
|
|
78
|
+
)
|
|
79
|
+
) > 1000 THEN
|
|
80
|
+
RAISE EXCEPTION 'workspace member candidate inventory exceeds the bounded projection'
|
|
81
|
+
USING ERRCODE = '54000';
|
|
82
|
+
END IF;
|
|
83
|
+
|
|
84
|
+
SELECT coalesce(pg_catalog.jsonb_agg(
|
|
85
|
+
pg_catalog.jsonb_build_object(
|
|
86
|
+
'organizationMembershipId', membership.id,
|
|
87
|
+
'subjectId', membership.subject_id,
|
|
88
|
+
'name', auth_user.name,
|
|
89
|
+
'email', auth_user.email,
|
|
90
|
+
'organizationRole', membership.role
|
|
91
|
+
) ORDER BY lower(coalesce(auth_user.name, auth_user.email, membership.subject_id)), membership.id
|
|
92
|
+
), '[]'::jsonb) INTO result
|
|
93
|
+
FROM organization_memberships membership
|
|
94
|
+
LEFT JOIN auth_users auth_user
|
|
95
|
+
ON membership.subject_id = 'user:' || auth_user.id
|
|
96
|
+
WHERE membership.account_id = p_account_id
|
|
97
|
+
AND membership.status = 'active'
|
|
98
|
+
AND membership.subject_id LIKE 'user:%'
|
|
99
|
+
AND NOT EXISTS (
|
|
100
|
+
SELECT 1 FROM workspace_memberships access
|
|
101
|
+
WHERE access.account_id = p_account_id
|
|
102
|
+
AND access.workspace_id = p_workspace_id
|
|
103
|
+
AND access.subject_id = membership.subject_id
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
PERFORM pg_catalog.set_config(
|
|
107
|
+
'opengeni.organization_tenancy_lifecycle', COALESCE(previous_lifecycle, ''), true
|
|
108
|
+
);
|
|
109
|
+
RETURN result;
|
|
110
|
+
EXCEPTION WHEN OTHERS THEN
|
|
111
|
+
PERFORM pg_catalog.set_config(
|
|
112
|
+
'opengeni.organization_tenancy_lifecycle', COALESCE(previous_lifecycle, ''), true
|
|
113
|
+
);
|
|
114
|
+
RAISE;
|
|
115
|
+
END
|
|
116
|
+
$body$;
|
|
117
|
+
|
|
118
|
+
REVOKE ALL ON FUNCTION list_workspace_member_management_candidates(uuid, uuid, text)
|
|
119
|
+
FROM PUBLIC;
|
|
120
|
+
DO $workspace_member_candidate_inventory_role$
|
|
121
|
+
BEGIN
|
|
122
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
|
|
123
|
+
GRANT EXECUTE ON FUNCTION list_workspace_member_management_candidates(
|
|
124
|
+
uuid, uuid, text
|
|
125
|
+
) TO opengeni_app;
|
|
126
|
+
END IF;
|
|
127
|
+
END
|
|
128
|
+
$workspace_member_candidate_inventory_role$;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Migration 0352 activated ordered session Variable Set selections and extended
|
|
3
|
+
-- accepted personal-resource grants, but four runtime authorization seams
|
|
4
|
+
-- still consulted only the legacy final-entry `sessions.variable_set_id` alias.
|
|
5
|
+
-- A selected set earlier in `variable_set_ids` could therefore be accepted and
|
|
6
|
+
-- snapshotted correctly, then rejected during materialization or an exact agent
|
|
7
|
+
-- secret read. Keep every public signature, return type, grant, search path, and
|
|
8
|
+
-- audit behavior unchanged while switching those selection checks to the full
|
|
9
|
+
-- ordered session selection.
|
|
10
|
+
|
|
11
|
+
SET LOCAL lock_timeout = '5s';
|
|
12
|
+
SET LOCAL statement_timeout = '5min';
|
|
13
|
+
|
|
14
|
+
DO $repair_ordered_variable_set_runtime_authority$
|
|
15
|
+
DECLARE
|
|
16
|
+
data_schema text := pg_catalog.current_schema();
|
|
17
|
+
patch_record record;
|
|
18
|
+
function_oid regprocedure;
|
|
19
|
+
definition text;
|
|
20
|
+
patched text;
|
|
21
|
+
occurrences integer;
|
|
22
|
+
BEGIN
|
|
23
|
+
FOR patch_record IN
|
|
24
|
+
SELECT * FROM (VALUES
|
|
25
|
+
(
|
|
26
|
+
'materialize_scoped_variable_set_for_attempt(uuid,uuid,uuid,uuid,uuid,integer,uuid)',
|
|
27
|
+
'session_value.variable_set_id = variable_set_row.id',
|
|
28
|
+
'coalesce(session_value.variable_set_ids, ''[]''::jsonb) ? variable_set_row.id::text'
|
|
29
|
+
),
|
|
30
|
+
(
|
|
31
|
+
'read_scoped_variable_set_secret(uuid,uuid,uuid,text,text,text,uuid,uuid,uuid,integer)',
|
|
32
|
+
'session_value.variable_set_id = variable_set_row.id',
|
|
33
|
+
'coalesce(session_value.variable_set_ids, ''[]''::jsonb) ? variable_set_row.id::text'
|
|
34
|
+
),
|
|
35
|
+
(
|
|
36
|
+
'authorize_session_attempt_personal_resource_reads(uuid,uuid,uuid)',
|
|
37
|
+
'variable_set.id = session_value.variable_set_id',
|
|
38
|
+
'coalesce(session_value.variable_set_ids, ''[]''::jsonb) ? variable_set.id::text'
|
|
39
|
+
)
|
|
40
|
+
) AS patches(function_signature, old_anchor, new_anchor)
|
|
41
|
+
LOOP
|
|
42
|
+
function_oid := pg_catalog.to_regprocedure(
|
|
43
|
+
pg_catalog.quote_ident(data_schema) || '.' || patch_record.function_signature
|
|
44
|
+
);
|
|
45
|
+
IF function_oid IS NULL THEN
|
|
46
|
+
RAISE EXCEPTION '0372 required function is unavailable: %',
|
|
47
|
+
patch_record.function_signature
|
|
48
|
+
USING ERRCODE = '55000';
|
|
49
|
+
END IF;
|
|
50
|
+
|
|
51
|
+
definition := pg_catalog.pg_get_functiondef(function_oid);
|
|
52
|
+
occurrences := (
|
|
53
|
+
pg_catalog.length(definition)
|
|
54
|
+
- pg_catalog.length(pg_catalog.replace(definition, patch_record.old_anchor, ''))
|
|
55
|
+
) / pg_catalog.length(patch_record.old_anchor);
|
|
56
|
+
IF occurrences <> 1 OR pg_catalog.strpos(definition, patch_record.new_anchor) > 0 THEN
|
|
57
|
+
RAISE EXCEPTION '0372 ordered Variable Set authority definition drift: %',
|
|
58
|
+
patch_record.function_signature
|
|
59
|
+
USING ERRCODE = '55000';
|
|
60
|
+
END IF;
|
|
61
|
+
|
|
62
|
+
patched := pg_catalog.replace(
|
|
63
|
+
definition,
|
|
64
|
+
patch_record.old_anchor,
|
|
65
|
+
patch_record.new_anchor
|
|
66
|
+
);
|
|
67
|
+
IF pg_catalog.strpos(patched, patch_record.new_anchor) = 0
|
|
68
|
+
OR pg_catalog.strpos(patched, patch_record.old_anchor) > 0
|
|
69
|
+
THEN
|
|
70
|
+
RAISE EXCEPTION '0372 ordered Variable Set authority patch failed: %',
|
|
71
|
+
patch_record.function_signature
|
|
72
|
+
USING ERRCODE = '55000';
|
|
73
|
+
END IF;
|
|
74
|
+
EXECUTE patched;
|
|
75
|
+
END LOOP;
|
|
76
|
+
END
|
|
77
|
+
$repair_ordered_variable_set_runtime_authority$;
|
|
78
|
+
|
|
79
|
+
-- The direct session-attach lane has no attempt snapshot to resolve. Admit a
|
|
80
|
+
-- user-scoped set only through the exact live owner membership, resource
|
|
81
|
+
-- authority, and session/always grant that accepted work issued for this
|
|
82
|
+
-- session. Every mutable authority row is locked before ciphertext egress so a
|
|
83
|
+
-- concurrent revocation wins before a later attach or attach retry.
|
|
84
|
+
CREATE OR REPLACE FUNCTION materialize_scoped_variable_set_for_session(
|
|
85
|
+
p_account_id uuid,
|
|
86
|
+
p_workspace_id uuid,
|
|
87
|
+
p_session_id uuid,
|
|
88
|
+
p_variable_set_id uuid
|
|
89
|
+
) RETURNS TABLE (
|
|
90
|
+
variable_set_id uuid,
|
|
91
|
+
variable_set_name text,
|
|
92
|
+
variable_set_description text,
|
|
93
|
+
authority_scope text,
|
|
94
|
+
variable_set_generation bigint,
|
|
95
|
+
variable_name text,
|
|
96
|
+
value_encrypted text
|
|
97
|
+
)
|
|
98
|
+
LANGUAGE plpgsql
|
|
99
|
+
SECURITY DEFINER
|
|
100
|
+
SET search_path FROM CURRENT
|
|
101
|
+
AS $$
|
|
102
|
+
DECLARE
|
|
103
|
+
selected_row record;
|
|
104
|
+
variable_set_row workspace_variable_sets%ROWTYPE;
|
|
105
|
+
session_authority_epoch integer;
|
|
106
|
+
session_authority_visibility text;
|
|
107
|
+
session_owner_membership uuid;
|
|
108
|
+
audit_subject text;
|
|
109
|
+
causal_human text;
|
|
110
|
+
BEGIN
|
|
111
|
+
IF p_account_id IS DISTINCT FROM nullif(
|
|
112
|
+
pg_catalog.current_setting('opengeni.account_id', true), ''
|
|
113
|
+
)::uuid
|
|
114
|
+
OR p_workspace_id IS DISTINCT FROM nullif(
|
|
115
|
+
pg_catalog.current_setting('opengeni.workspace_id', true), ''
|
|
116
|
+
)::uuid
|
|
117
|
+
THEN
|
|
118
|
+
RAISE EXCEPTION 'variable-set session materialization scope mismatch'
|
|
119
|
+
USING ERRCODE = '42501';
|
|
120
|
+
END IF;
|
|
121
|
+
|
|
122
|
+
audit_subject := coalesce(
|
|
123
|
+
nullif(pg_catalog.current_setting('opengeni.subject_id', true), ''),
|
|
124
|
+
'service:session'
|
|
125
|
+
);
|
|
126
|
+
causal_human := coalesce(
|
|
127
|
+
nullif(pg_catalog.current_setting('opengeni.initiating_human_subject_id', true), ''),
|
|
128
|
+
CASE WHEN audit_subject LIKE 'user:%' THEN audit_subject END
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
INSERT INTO opengeni_private.variable_set_authority_capabilities (
|
|
132
|
+
backend_pid, transaction_id, capability_kind
|
|
133
|
+
) VALUES (pg_catalog.pg_backend_pid(), pg_catalog.pg_current_xact_id(), 'materialize')
|
|
134
|
+
ON CONFLICT DO NOTHING;
|
|
135
|
+
INSERT INTO opengeni_private.personal_resource_delegation_capabilities (
|
|
136
|
+
backend_pid, transaction_id, capability_kind
|
|
137
|
+
) VALUES (pg_catalog.pg_backend_pid(), pg_catalog.pg_current_xact_id(), 'resolve')
|
|
138
|
+
ON CONFLICT DO NOTHING;
|
|
139
|
+
|
|
140
|
+
SELECT variable_set AS selected_set,
|
|
141
|
+
session_value.authority_epoch AS session_epoch,
|
|
142
|
+
session_value.visibility AS session_visibility,
|
|
143
|
+
session_value.owner_organization_membership_id AS session_owner_membership
|
|
144
|
+
INTO STRICT selected_row
|
|
145
|
+
FROM sessions session_value
|
|
146
|
+
JOIN workspace_variable_sets variable_set
|
|
147
|
+
ON variable_set.id = p_variable_set_id
|
|
148
|
+
AND variable_set.account_id = session_value.account_id
|
|
149
|
+
WHERE session_value.id = p_session_id
|
|
150
|
+
AND session_value.account_id = p_account_id
|
|
151
|
+
AND session_value.workspace_id = p_workspace_id
|
|
152
|
+
AND coalesce(session_value.variable_set_ids, '[]'::jsonb) ? p_variable_set_id::text
|
|
153
|
+
AND session_value.status IN (
|
|
154
|
+
'queued', 'running', 'idle', 'requires_action', 'recovering', 'waiting_capacity'
|
|
155
|
+
)
|
|
156
|
+
AND variable_set.status = 'active'
|
|
157
|
+
AND (
|
|
158
|
+
variable_set.authority_scope IN ('organization', 'user')
|
|
159
|
+
OR (
|
|
160
|
+
variable_set.authority_scope = 'workspace'
|
|
161
|
+
AND variable_set.workspace_id = p_workspace_id
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
FOR SHARE OF session_value, variable_set;
|
|
165
|
+
variable_set_row := selected_row.selected_set;
|
|
166
|
+
session_authority_epoch := selected_row.session_epoch;
|
|
167
|
+
session_authority_visibility := selected_row.session_visibility;
|
|
168
|
+
session_owner_membership := selected_row.session_owner_membership;
|
|
169
|
+
|
|
170
|
+
IF variable_set_row.authority_scope = 'user' THEN
|
|
171
|
+
PERFORM 1
|
|
172
|
+
FROM organization_memberships membership
|
|
173
|
+
JOIN organization_user_resource_authorities authority
|
|
174
|
+
ON authority.id = variable_set_row.authority_id
|
|
175
|
+
AND authority.account_id = membership.account_id
|
|
176
|
+
AND authority.organization_membership_id = membership.id
|
|
177
|
+
AND authority.resource_kind = 'variable_set'
|
|
178
|
+
AND authority.resource_id = variable_set_row.id
|
|
179
|
+
AND authority.origin_workspace_id IS NOT DISTINCT FROM variable_set_row.origin_workspace_id
|
|
180
|
+
AND authority.generation = variable_set_row.generation
|
|
181
|
+
AND authority.status = 'active'
|
|
182
|
+
AND authority.revoked_at IS NULL
|
|
183
|
+
JOIN organization_user_resource_grants grant_value
|
|
184
|
+
ON grant_value.account_id = authority.account_id
|
|
185
|
+
AND grant_value.authority_id = authority.id
|
|
186
|
+
AND grant_value.owner_organization_membership_id = membership.id
|
|
187
|
+
AND grant_value.workspace_id = p_workspace_id
|
|
188
|
+
AND grant_value.action = 'variable_set.use'
|
|
189
|
+
AND grant_value.context = session_authority_visibility
|
|
190
|
+
AND grant_value.status = 'active'
|
|
191
|
+
AND (grant_value.expires_at IS NULL OR grant_value.expires_at > clock_timestamp())
|
|
192
|
+
AND (
|
|
193
|
+
(
|
|
194
|
+
grant_value.mode = 'session'
|
|
195
|
+
AND grant_value.session_id = p_session_id
|
|
196
|
+
AND grant_value.authority_epoch = session_authority_epoch
|
|
197
|
+
)
|
|
198
|
+
OR (
|
|
199
|
+
grant_value.mode = 'always'
|
|
200
|
+
AND grant_value.session_id IS NULL
|
|
201
|
+
AND grant_value.authority_epoch IS NULL
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
WHERE membership.id = variable_set_row.owner_organization_membership_id
|
|
205
|
+
AND membership.account_id = p_account_id
|
|
206
|
+
AND membership.subject_id = causal_human
|
|
207
|
+
AND membership.status = 'active'
|
|
208
|
+
AND membership.revoked_at IS NULL
|
|
209
|
+
AND membership.authorization_revision > 0
|
|
210
|
+
AND (
|
|
211
|
+
membership.personal_workspace_id = p_workspace_id
|
|
212
|
+
OR EXISTS (
|
|
213
|
+
SELECT 1
|
|
214
|
+
FROM workspace_memberships workspace_membership
|
|
215
|
+
WHERE workspace_membership.account_id = membership.account_id
|
|
216
|
+
AND workspace_membership.workspace_id = p_workspace_id
|
|
217
|
+
AND workspace_membership.subject_id = membership.subject_id
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
AND (
|
|
221
|
+
session_authority_visibility = 'workspace_shared'
|
|
222
|
+
OR session_owner_membership = membership.id
|
|
223
|
+
)
|
|
224
|
+
ORDER BY grant_value.id
|
|
225
|
+
FOR SHARE OF membership, authority, grant_value;
|
|
226
|
+
IF NOT FOUND THEN
|
|
227
|
+
RAISE EXCEPTION 'personal variable-set session grant is not exact or current'
|
|
228
|
+
USING ERRCODE = '42501';
|
|
229
|
+
END IF;
|
|
230
|
+
END IF;
|
|
231
|
+
|
|
232
|
+
INSERT INTO audit_events (
|
|
233
|
+
account_id, workspace_id, subject_id, action, target_type, target_id,
|
|
234
|
+
metadata, metadata_codec_version
|
|
235
|
+
) VALUES (
|
|
236
|
+
p_account_id, p_workspace_id, audit_subject,
|
|
237
|
+
'variable_set.materialized', 'workspace_variable_set',
|
|
238
|
+
variable_set_row.id::text,
|
|
239
|
+
pg_catalog.jsonb_build_object(
|
|
240
|
+
'variableSetId', variable_set_row.id,
|
|
241
|
+
'scope', variable_set_row.authority_scope,
|
|
242
|
+
'generation', variable_set_row.generation,
|
|
243
|
+
'actorKind', 'session_attach',
|
|
244
|
+
'sessionId', p_session_id,
|
|
245
|
+
'causalHumanSubjectId', causal_human,
|
|
246
|
+
'authorityEpoch', session_authority_epoch,
|
|
247
|
+
'authorityVisibility', session_authority_visibility,
|
|
248
|
+
'authorityOwnerOrganizationMembershipId', session_owner_membership,
|
|
249
|
+
'ownerAuthorityId', variable_set_row.authority_id,
|
|
250
|
+
'ownerOrganizationMembershipId', variable_set_row.owner_organization_membership_id,
|
|
251
|
+
'originWorkspaceId', variable_set_row.origin_workspace_id
|
|
252
|
+
),
|
|
253
|
+
1
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
RETURN QUERY
|
|
257
|
+
SELECT selected.id, selected.name, selected.description,
|
|
258
|
+
selected.authority_scope, selected.generation,
|
|
259
|
+
variable.name, variable.value_encrypted
|
|
260
|
+
FROM workspace_variable_sets selected
|
|
261
|
+
LEFT JOIN workspace_variable_set_variables variable
|
|
262
|
+
ON variable.account_id = selected.account_id
|
|
263
|
+
AND variable.variable_set_id = selected.id
|
|
264
|
+
WHERE selected.id = variable_set_row.id
|
|
265
|
+
ORDER BY variable.name;
|
|
266
|
+
|
|
267
|
+
DELETE FROM opengeni_private.personal_resource_delegation_capabilities
|
|
268
|
+
WHERE backend_pid = pg_catalog.pg_backend_pid()
|
|
269
|
+
AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned()
|
|
270
|
+
AND capability_kind = 'resolve';
|
|
271
|
+
DELETE FROM opengeni_private.variable_set_authority_capabilities
|
|
272
|
+
WHERE backend_pid = pg_catalog.pg_backend_pid()
|
|
273
|
+
AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned()
|
|
274
|
+
AND capability_kind = 'materialize';
|
|
275
|
+
RETURN;
|
|
276
|
+
EXCEPTION WHEN OTHERS THEN
|
|
277
|
+
DELETE FROM opengeni_private.personal_resource_delegation_capabilities
|
|
278
|
+
WHERE backend_pid = pg_catalog.pg_backend_pid()
|
|
279
|
+
AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned()
|
|
280
|
+
AND capability_kind = 'resolve';
|
|
281
|
+
DELETE FROM opengeni_private.variable_set_authority_capabilities
|
|
282
|
+
WHERE backend_pid = pg_catalog.pg_backend_pid()
|
|
283
|
+
AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned()
|
|
284
|
+
AND capability_kind = 'materialize';
|
|
285
|
+
RAISE;
|
|
286
|
+
END
|
|
287
|
+
$$;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Durable single-flight coordination for immutable, lease-scoped sandbox setup.
|
|
3
|
+
|
|
4
|
+
SET LOCAL lock_timeout = '5s';
|
|
5
|
+
SET LOCAL statement_timeout = '10min';
|
|
6
|
+
|
|
7
|
+
ALTER TABLE sandbox_leases
|
|
8
|
+
ADD COLUMN shared_preparation_lease_epoch integer,
|
|
9
|
+
ADD COLUMN shared_preparation_instance_id text,
|
|
10
|
+
ADD COLUMN shared_preparation_spec_hash text,
|
|
11
|
+
ADD COLUMN shared_preparation_status text,
|
|
12
|
+
ADD COLUMN shared_preparation_claim_id uuid,
|
|
13
|
+
ADD COLUMN shared_preparation_owner_attempt_id uuid,
|
|
14
|
+
ADD COLUMN shared_preparation_attempt integer,
|
|
15
|
+
ADD COLUMN shared_preparation_revision integer NOT NULL DEFAULT 0,
|
|
16
|
+
ADD COLUMN shared_preparation_started_at timestamptz,
|
|
17
|
+
ADD COLUMN shared_preparation_deadline_at timestamptz,
|
|
18
|
+
ADD COLUMN shared_preparation_settled_at timestamptz;
|
|
19
|
+
|
|
20
|
+
ALTER TABLE sandbox_leases
|
|
21
|
+
ADD CONSTRAINT sandbox_leases_shared_preparation_check CHECK (
|
|
22
|
+
(
|
|
23
|
+
shared_preparation_status IS NULL
|
|
24
|
+
AND shared_preparation_lease_epoch IS NULL
|
|
25
|
+
AND shared_preparation_instance_id IS NULL
|
|
26
|
+
AND shared_preparation_spec_hash IS NULL
|
|
27
|
+
AND shared_preparation_claim_id IS NULL
|
|
28
|
+
AND shared_preparation_owner_attempt_id IS NULL
|
|
29
|
+
AND shared_preparation_attempt IS NULL
|
|
30
|
+
AND shared_preparation_started_at IS NULL
|
|
31
|
+
AND shared_preparation_deadline_at IS NULL
|
|
32
|
+
AND shared_preparation_settled_at IS NULL
|
|
33
|
+
) OR (
|
|
34
|
+
shared_preparation_status IN ('running', 'completed', 'failed')
|
|
35
|
+
AND shared_preparation_lease_epoch IS NOT NULL
|
|
36
|
+
AND shared_preparation_lease_epoch >= 0
|
|
37
|
+
AND nullif(btrim(shared_preparation_instance_id), '') IS NOT NULL
|
|
38
|
+
AND shared_preparation_spec_hash ~ '^sha256:[0-9a-f]{64}$'
|
|
39
|
+
AND shared_preparation_claim_id IS NOT NULL
|
|
40
|
+
AND shared_preparation_owner_attempt_id IS NOT NULL
|
|
41
|
+
AND shared_preparation_attempt IS NOT NULL
|
|
42
|
+
AND shared_preparation_attempt > 0
|
|
43
|
+
AND shared_preparation_started_at IS NOT NULL
|
|
44
|
+
AND shared_preparation_deadline_at > shared_preparation_started_at
|
|
45
|
+
AND (
|
|
46
|
+
(shared_preparation_status = 'running' AND shared_preparation_settled_at IS NULL)
|
|
47
|
+
OR (shared_preparation_status IN ('completed', 'failed')
|
|
48
|
+
AND shared_preparation_settled_at IS NOT NULL)
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
) NOT VALID;
|
|
52
|
+
|
|
53
|
+
ALTER TABLE sandbox_leases
|
|
54
|
+
VALIDATE CONSTRAINT sandbox_leases_shared_preparation_check;
|
|
55
|
+
|
|
56
|
+
COMMENT ON COLUMN sandbox_leases.shared_preparation_spec_hash IS
|
|
57
|
+
'Non-secret immutable setup specification coordinated once for the exact lease epoch and provider instance.';
|
|
58
|
+
COMMENT ON COLUMN sandbox_leases.shared_preparation_revision IS
|
|
59
|
+
'Monotonic durable join cursor. Waiters treat notifications as hints and always re-read this lease row.';
|