@opengeni/db 0.12.1 → 0.12.6
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/{chunk-VUKRIBO5.js → chunk-CYA6BHV6.js} +150 -7
- package/dist/chunk-CYA6BHV6.js.map +1 -0
- package/dist/chunk-XRVNI2GB.js +896 -0
- package/dist/chunk-XRVNI2GB.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1166 -265
- package/dist/index.js.map +1 -1
- package/dist/provision-roles.d.ts +324 -38
- package/dist/provision-roles.js +1 -1
- package/dist/{schema-BejThLcd.d.ts → schema-CeXWlYcG.d.ts} +431 -2
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +1 -1
- package/drizzle/0122_codex_capacity_same_turn.sql +59 -0
- package/drizzle/0123_session_tool_policy_version.sql +14 -0
- package/drizzle/0124_session_event_duplicate_lookup.sql +4 -0
- package/drizzle/0125_document_drops_visibility.sql +29 -0
- package/drizzle/0126_document_access_constraints.sql +138 -0
- package/drizzle/0127_document_default_base_index.sql +5 -0
- package/drizzle/0128_github_installation_authority.sql +69 -0
- package/drizzle/0129_retained_process_reconciliation.sql +356 -0
- package/package.json +4 -3
- package/src/event-payload-sanitizer.ts +167 -55
- package/src/index.ts +1494 -258
- package/src/new-session-drafts.ts +127 -6
- package/src/provision-roles.ts +184 -2
- package/src/runtime-posture-cli.ts +57 -0
- package/src/runtime-posture.ts +760 -0
- package/src/schema.ts +156 -3
- package/src/session-control.ts +1 -0
- package/dist/chunk-BMFDXFPA.js +0 -155
- package/dist/chunk-BMFDXFPA.js.map +0 -1
- package/dist/chunk-VUKRIBO5.js.map +0 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Document access and knowledge-drop state hardening.
|
|
3
|
+
--
|
|
4
|
+
-- Existing raced Default rows are consolidated before the unique index is
|
|
5
|
+
-- installed by the following concurrent migration. This migration also
|
|
6
|
+
-- repairs legacy document state before adding validated checks, so an upgrade
|
|
7
|
+
-- cannot strand an otherwise readable database behind an invalid old value.
|
|
8
|
+
|
|
9
|
+
DO $$
|
|
10
|
+
DECLARE
|
|
11
|
+
duplicate RECORD;
|
|
12
|
+
BEGIN
|
|
13
|
+
FOR duplicate IN
|
|
14
|
+
SELECT workspace_id, min(id::text)::uuid AS keep_id
|
|
15
|
+
FROM document_bases
|
|
16
|
+
WHERE lower(btrim(name)) = 'default'
|
|
17
|
+
GROUP BY workspace_id
|
|
18
|
+
HAVING count(*) > 1
|
|
19
|
+
LOOP
|
|
20
|
+
-- A file can only occur once in a base. Keep the lexicographically first
|
|
21
|
+
-- Default row's document when an old race created the same file twice.
|
|
22
|
+
DELETE FROM documents duplicate_document
|
|
23
|
+
WHERE duplicate_document.workspace_id = duplicate.workspace_id
|
|
24
|
+
AND duplicate_document.base_id <> duplicate.keep_id
|
|
25
|
+
AND lower(btrim((SELECT name FROM document_bases WHERE id = duplicate_document.base_id))) = 'default'
|
|
26
|
+
AND EXISTS (
|
|
27
|
+
SELECT 1
|
|
28
|
+
FROM documents retained_document
|
|
29
|
+
WHERE retained_document.workspace_id = duplicate.workspace_id
|
|
30
|
+
AND retained_document.base_id = duplicate.keep_id
|
|
31
|
+
AND retained_document.file_id = duplicate_document.file_id
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
UPDATE documents
|
|
35
|
+
SET base_id = duplicate.keep_id,
|
|
36
|
+
updated_at = now()
|
|
37
|
+
WHERE workspace_id = duplicate.workspace_id
|
|
38
|
+
AND base_id <> duplicate.keep_id
|
|
39
|
+
AND lower(btrim((SELECT name FROM document_bases WHERE id = base_id))) = 'default';
|
|
40
|
+
|
|
41
|
+
UPDATE document_chunks chunk
|
|
42
|
+
SET base_id = duplicate.keep_id
|
|
43
|
+
WHERE chunk.workspace_id = duplicate.workspace_id
|
|
44
|
+
AND chunk.base_id <> duplicate.keep_id
|
|
45
|
+
AND EXISTS (
|
|
46
|
+
SELECT 1
|
|
47
|
+
FROM documents document
|
|
48
|
+
WHERE document.id = chunk.document_id
|
|
49
|
+
AND document.base_id = duplicate.keep_id
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
DELETE FROM document_bases base
|
|
53
|
+
WHERE base.workspace_id = duplicate.workspace_id
|
|
54
|
+
AND base.id <> duplicate.keep_id
|
|
55
|
+
AND lower(btrim(base.name)) = 'default';
|
|
56
|
+
END LOOP;
|
|
57
|
+
END
|
|
58
|
+
$$;
|
|
59
|
+
|
|
60
|
+
-- Rows written before these fields were governed may contain values outside
|
|
61
|
+
-- the new state machine. Preserve the document and its retrieval posture while
|
|
62
|
+
-- normalizing only states that have no safe interpretation.
|
|
63
|
+
UPDATE "documents"
|
|
64
|
+
SET "visibility" = 'workspace'
|
|
65
|
+
WHERE "visibility" IS NULL OR "visibility" NOT IN ('workspace', 'private');
|
|
66
|
+
|
|
67
|
+
UPDATE "documents"
|
|
68
|
+
SET "visibility" = 'workspace', "created_by" = NULL
|
|
69
|
+
WHERE "visibility" = 'private' AND NULLIF(btrim("created_by"), '') IS NULL;
|
|
70
|
+
|
|
71
|
+
UPDATE "documents"
|
|
72
|
+
SET "curation_status" = 'none'
|
|
73
|
+
WHERE "curation_status" IS NULL
|
|
74
|
+
OR "curation_status" NOT IN ('none', 'pending', 'suggested', 'auto_filed', 'failed');
|
|
75
|
+
|
|
76
|
+
UPDATE "documents"
|
|
77
|
+
SET "topics" = '[]'::jsonb
|
|
78
|
+
WHERE "topics" IS NULL OR jsonb_typeof("topics") <> 'array';
|
|
79
|
+
|
|
80
|
+
UPDATE "documents"
|
|
81
|
+
SET "curation" = NULL
|
|
82
|
+
WHERE "curation" IS NOT NULL AND jsonb_typeof("curation") <> 'object';
|
|
83
|
+
|
|
84
|
+
DO $$
|
|
85
|
+
BEGIN
|
|
86
|
+
IF NOT EXISTS (
|
|
87
|
+
SELECT 1 FROM pg_constraint
|
|
88
|
+
WHERE conname = 'documents_visibility_chk'
|
|
89
|
+
AND conrelid = 'documents'::regclass
|
|
90
|
+
) THEN
|
|
91
|
+
ALTER TABLE "documents"
|
|
92
|
+
ADD CONSTRAINT "documents_visibility_chk"
|
|
93
|
+
CHECK ("visibility" IN ('workspace', 'private')) NOT VALID;
|
|
94
|
+
END IF;
|
|
95
|
+
IF NOT EXISTS (
|
|
96
|
+
SELECT 1 FROM pg_constraint
|
|
97
|
+
WHERE conname = 'documents_curation_status_chk'
|
|
98
|
+
AND conrelid = 'documents'::regclass
|
|
99
|
+
) THEN
|
|
100
|
+
ALTER TABLE "documents"
|
|
101
|
+
ADD CONSTRAINT "documents_curation_status_chk"
|
|
102
|
+
CHECK ("curation_status" IN ('none', 'pending', 'suggested', 'auto_filed', 'failed')) NOT VALID;
|
|
103
|
+
END IF;
|
|
104
|
+
IF NOT EXISTS (
|
|
105
|
+
SELECT 1 FROM pg_constraint
|
|
106
|
+
WHERE conname = 'documents_private_creator_chk'
|
|
107
|
+
AND conrelid = 'documents'::regclass
|
|
108
|
+
) THEN
|
|
109
|
+
ALTER TABLE "documents"
|
|
110
|
+
ADD CONSTRAINT "documents_private_creator_chk"
|
|
111
|
+
CHECK ("visibility" <> 'private' OR NULLIF(btrim("created_by"), '') IS NOT NULL) NOT VALID;
|
|
112
|
+
END IF;
|
|
113
|
+
IF NOT EXISTS (
|
|
114
|
+
SELECT 1 FROM pg_constraint
|
|
115
|
+
WHERE conname = 'documents_topics_array_chk'
|
|
116
|
+
AND conrelid = 'documents'::regclass
|
|
117
|
+
) THEN
|
|
118
|
+
ALTER TABLE "documents"
|
|
119
|
+
ADD CONSTRAINT "documents_topics_array_chk"
|
|
120
|
+
CHECK (jsonb_typeof("topics") = 'array') NOT VALID;
|
|
121
|
+
END IF;
|
|
122
|
+
IF NOT EXISTS (
|
|
123
|
+
SELECT 1 FROM pg_constraint
|
|
124
|
+
WHERE conname = 'documents_curation_object_chk'
|
|
125
|
+
AND conrelid = 'documents'::regclass
|
|
126
|
+
) THEN
|
|
127
|
+
ALTER TABLE "documents"
|
|
128
|
+
ADD CONSTRAINT "documents_curation_object_chk"
|
|
129
|
+
CHECK ("curation" IS NULL OR jsonb_typeof("curation") = 'object') NOT VALID;
|
|
130
|
+
END IF;
|
|
131
|
+
END
|
|
132
|
+
$$;
|
|
133
|
+
|
|
134
|
+
ALTER TABLE "documents" VALIDATE CONSTRAINT "documents_visibility_chk";
|
|
135
|
+
ALTER TABLE "documents" VALIDATE CONSTRAINT "documents_curation_status_chk";
|
|
136
|
+
ALTER TABLE "documents" VALIDATE CONSTRAINT "documents_private_creator_chk";
|
|
137
|
+
ALTER TABLE "documents" VALIDATE CONSTRAINT "documents_topics_array_chk";
|
|
138
|
+
ALTER TABLE "documents" VALIDATE CONSTRAINT "documents_curation_object_chk";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
|
|
3
|
+
ALTER TABLE "github_installations"
|
|
4
|
+
ADD COLUMN IF NOT EXISTS "github_account_id" bigint,
|
|
5
|
+
ADD COLUMN IF NOT EXISTS "github_actor_id" bigint,
|
|
6
|
+
ADD COLUMN IF NOT EXISTS "github_actor_login" text,
|
|
7
|
+
ADD COLUMN IF NOT EXISTS "authority_kind" text,
|
|
8
|
+
ADD COLUMN IF NOT EXISTS "authority_checked_at" timestamptz,
|
|
9
|
+
ADD COLUMN IF NOT EXISTS "authority_expires_at" timestamptz,
|
|
10
|
+
ADD COLUMN IF NOT EXISTS "authority_nonce" text;
|
|
11
|
+
|
|
12
|
+
DO $$
|
|
13
|
+
BEGIN
|
|
14
|
+
IF NOT EXISTS (
|
|
15
|
+
SELECT 1 FROM pg_constraint
|
|
16
|
+
WHERE conname = 'github_installations_authority_kind_check'
|
|
17
|
+
AND conrelid = 'github_installations'::regclass
|
|
18
|
+
) THEN
|
|
19
|
+
ALTER TABLE "github_installations"
|
|
20
|
+
ADD CONSTRAINT "github_installations_authority_kind_check"
|
|
21
|
+
CHECK (
|
|
22
|
+
(
|
|
23
|
+
"github_account_id" IS NULL
|
|
24
|
+
AND "github_actor_id" IS NULL
|
|
25
|
+
AND "github_actor_login" IS NULL
|
|
26
|
+
AND "authority_kind" IS NULL
|
|
27
|
+
AND "authority_checked_at" IS NULL
|
|
28
|
+
AND "authority_expires_at" IS NULL
|
|
29
|
+
AND "authority_nonce" IS NULL
|
|
30
|
+
)
|
|
31
|
+
OR (
|
|
32
|
+
"github_account_id" IS NOT NULL
|
|
33
|
+
AND "github_account_id" > 0
|
|
34
|
+
AND "github_actor_id" IS NOT NULL
|
|
35
|
+
AND "github_actor_id" > 0
|
|
36
|
+
AND "github_actor_login" IS NOT NULL
|
|
37
|
+
AND length("github_actor_login") > 0
|
|
38
|
+
AND "account_login" IS NOT NULL
|
|
39
|
+
AND length("account_login") > 0
|
|
40
|
+
AND "account_type" IS NOT NULL
|
|
41
|
+
AND "linked_by_subject_id" IS NOT NULL
|
|
42
|
+
AND length("linked_by_subject_id") > 0
|
|
43
|
+
AND "authority_kind" IS NOT NULL
|
|
44
|
+
AND "authority_checked_at" IS NOT NULL
|
|
45
|
+
AND "authority_expires_at" IS NOT NULL
|
|
46
|
+
AND "authority_checked_at" < "authority_expires_at"
|
|
47
|
+
AND "authority_expires_at" <= "authority_checked_at" + interval '10 minutes'
|
|
48
|
+
AND "authority_nonce" IS NOT NULL
|
|
49
|
+
AND length("authority_nonce") > 0
|
|
50
|
+
AND "repository_scope" = 'selected'
|
|
51
|
+
AND (
|
|
52
|
+
(
|
|
53
|
+
"authority_kind" = 'personal_owner'
|
|
54
|
+
AND "account_type" = 'User'
|
|
55
|
+
AND "github_actor_id" = "github_account_id"
|
|
56
|
+
)
|
|
57
|
+
OR (
|
|
58
|
+
"authority_kind" = 'organization_owner'
|
|
59
|
+
AND "account_type" = 'Organization'
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
END IF;
|
|
65
|
+
END $$;
|
|
66
|
+
|
|
67
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "github_installations_authority_nonce_uq"
|
|
68
|
+
ON "github_installations" ("authority_nonce")
|
|
69
|
+
WHERE "authority_nonce" IS NOT NULL;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Terminal owners do not prove provider quiescence. Persist only a
|
|
3
|
+
-- bounded/fair reconciliation claim here; the worker must still obtain an exact
|
|
4
|
+
-- provider exit/loss proof before invoking canonical retained-process settlement.
|
|
5
|
+
|
|
6
|
+
ALTER TABLE "sandbox_retained_processes"
|
|
7
|
+
ADD COLUMN IF NOT EXISTS "reconcile_after" timestamptz NOT NULL DEFAULT now(),
|
|
8
|
+
ADD COLUMN IF NOT EXISTS "reconcile_claim_id" uuid,
|
|
9
|
+
ADD COLUMN IF NOT EXISTS "reconcile_claimed_at" timestamptz,
|
|
10
|
+
ADD COLUMN IF NOT EXISTS "reconcile_attempts" integer NOT NULL DEFAULT 0,
|
|
11
|
+
ADD COLUMN IF NOT EXISTS "last_reconcile_outcome" text,
|
|
12
|
+
ADD COLUMN IF NOT EXISTS "reconcile_proof_outcome" text,
|
|
13
|
+
ADD COLUMN IF NOT EXISTS "reconcile_proof_exit_code" integer,
|
|
14
|
+
ADD COLUMN IF NOT EXISTS "reconcile_proof_reason" text,
|
|
15
|
+
ADD COLUMN IF NOT EXISTS "reconcile_proof_observed_at" timestamptz;
|
|
16
|
+
|
|
17
|
+
ALTER TABLE "sandbox_retained_processes"
|
|
18
|
+
ADD CONSTRAINT "sandbox_retained_processes_reconcile_claim_check"
|
|
19
|
+
CHECK (
|
|
20
|
+
("reconcile_claim_id" IS NULL AND "reconcile_claimed_at" IS NULL)
|
|
21
|
+
OR ("reconcile_claim_id" IS NOT NULL AND "reconcile_claimed_at" IS NOT NULL)
|
|
22
|
+
),
|
|
23
|
+
ADD CONSTRAINT "sandbox_retained_processes_reconcile_attempts_check"
|
|
24
|
+
CHECK ("reconcile_attempts" >= 0),
|
|
25
|
+
ADD CONSTRAINT "sandbox_retained_processes_reconcile_outcome_check"
|
|
26
|
+
CHECK (
|
|
27
|
+
"last_reconcile_outcome" IS NULL
|
|
28
|
+
OR octet_length("last_reconcile_outcome") BETWEEN 1 AND 64
|
|
29
|
+
),
|
|
30
|
+
ADD CONSTRAINT "sandbox_retained_processes_reconcile_proof_check"
|
|
31
|
+
CHECK (
|
|
32
|
+
(
|
|
33
|
+
"reconcile_proof_outcome" IS NULL
|
|
34
|
+
AND "reconcile_proof_exit_code" IS NULL
|
|
35
|
+
AND "reconcile_proof_reason" IS NULL
|
|
36
|
+
AND "reconcile_proof_observed_at" IS NULL
|
|
37
|
+
) OR (
|
|
38
|
+
"reconcile_proof_outcome" = 'exited'
|
|
39
|
+
AND "reconcile_proof_exit_code" IS NOT NULL
|
|
40
|
+
AND "reconcile_proof_reason" = 'provider_exit_banner'
|
|
41
|
+
AND "reconcile_proof_observed_at" IS NOT NULL
|
|
42
|
+
) OR (
|
|
43
|
+
"reconcile_proof_outcome" = 'lost'
|
|
44
|
+
AND "reconcile_proof_exit_code" IS NULL
|
|
45
|
+
AND "reconcile_proof_reason" IN (
|
|
46
|
+
'provider_session_lost_banner', 'provider_instance_not_found'
|
|
47
|
+
)
|
|
48
|
+
AND "reconcile_proof_observed_at" IS NOT NULL
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
CREATE INDEX "sandbox_retained_processes_reconcile_due_idx"
|
|
53
|
+
ON "sandbox_retained_processes" ("reconcile_after", "started_at", "id")
|
|
54
|
+
WHERE "state" = 'active';
|
|
55
|
+
|
|
56
|
+
CREATE INDEX "sandbox_retained_processes_active_inventory_idx"
|
|
57
|
+
ON "sandbox_retained_processes" (
|
|
58
|
+
"owner_actor_kind", "workspace_id", "owner_turn_id", "owner_attempt_id"
|
|
59
|
+
)
|
|
60
|
+
WHERE "state" = 'active';
|
|
61
|
+
|
|
62
|
+
CREATE INDEX "sandbox_leases_expired_draining_inventory_idx"
|
|
63
|
+
ON "sandbox_leases" ("expires_at", "backend")
|
|
64
|
+
WHERE "liveness" = 'draining';
|
|
65
|
+
|
|
66
|
+
-- The data schema is selected by the migration caller and may be public or a
|
|
67
|
+
-- dedicated embed schema. Bind it into every privileged statement at creation
|
|
68
|
+
-- time, then expose only pg_catalog at execution time. pg_temp and the caller's
|
|
69
|
+
-- search_path can therefore resolve neither relations nor helper functions.
|
|
70
|
+
DO $privileged_functions$
|
|
71
|
+
DECLARE data_schema text := current_schema();
|
|
72
|
+
BEGIN
|
|
73
|
+
-- The claim update fires this pre-existing deferred trigger while the claim
|
|
74
|
+
-- function's pg_catalog-only path is active. Replace its 0117 body in place so
|
|
75
|
+
-- the transitive identity check is equally independent of caller/pg_temp path.
|
|
76
|
+
EXECUTE format($create$
|
|
77
|
+
CREATE OR REPLACE FUNCTION opengeni_private.validate_sandbox_retained_process_v2()
|
|
78
|
+
RETURNS trigger
|
|
79
|
+
LANGUAGE plpgsql
|
|
80
|
+
SET search_path = pg_catalog
|
|
81
|
+
AS $function$
|
|
82
|
+
BEGIN
|
|
83
|
+
IF NOT EXISTS (
|
|
84
|
+
SELECT 1
|
|
85
|
+
FROM %1$I.sandbox_workspace_mutation_admissions admission
|
|
86
|
+
WHERE admission.id = NEW.parent_admission_id
|
|
87
|
+
AND admission.account_id = NEW.account_id
|
|
88
|
+
AND admission.workspace_id = NEW.workspace_id
|
|
89
|
+
AND admission.session_id = NEW.session_id
|
|
90
|
+
AND admission.lease_id = NEW.lease_id
|
|
91
|
+
AND admission.sandbox_group_id = NEW.sandbox_group_id
|
|
92
|
+
AND admission.actor_kind = NEW.owner_actor_kind
|
|
93
|
+
AND admission.actor_id = NEW.owner_actor_id
|
|
94
|
+
AND admission.turn_id IS NOT DISTINCT FROM NEW.owner_turn_id
|
|
95
|
+
AND admission.attempt_id IS NOT DISTINCT FROM NEW.owner_attempt_id
|
|
96
|
+
AND admission.execution_generation IS NOT DISTINCT FROM NEW.owner_execution_generation
|
|
97
|
+
AND admission.lease_epoch = NEW.lease_epoch
|
|
98
|
+
AND admission.provider_backend = NEW.provider_backend
|
|
99
|
+
AND admission.provider_instance_id = NEW.provider_instance_id
|
|
100
|
+
AND admission.route_kind = NEW.route_kind
|
|
101
|
+
AND admission.route_target_id IS NOT DISTINCT FROM NEW.route_target_id
|
|
102
|
+
AND admission.route_epoch = NEW.route_epoch
|
|
103
|
+
AND (
|
|
104
|
+
(
|
|
105
|
+
NEW.state = 'active'
|
|
106
|
+
AND admission.provider_outcome = 'retained'
|
|
107
|
+
AND admission.settled_at IS NULL
|
|
108
|
+
AND EXISTS (
|
|
109
|
+
SELECT 1 FROM %1$I.sandbox_lease_holders holder
|
|
110
|
+
WHERE holder.lease_id = NEW.lease_id
|
|
111
|
+
AND holder.account_id = NEW.account_id
|
|
112
|
+
AND holder.workspace_id = NEW.workspace_id
|
|
113
|
+
AND holder.kind = 'process'
|
|
114
|
+
AND holder.holder_id = NEW.holder_id
|
|
115
|
+
AND holder.subject_id = NEW.session_id
|
|
116
|
+
)
|
|
117
|
+
) OR (
|
|
118
|
+
NEW.state IN ('exited', 'lost')
|
|
119
|
+
AND admission.provider_outcome IN ('resolved', 'rejected')
|
|
120
|
+
AND admission.settled_at IS NOT NULL
|
|
121
|
+
AND NOT EXISTS (
|
|
122
|
+
SELECT 1 FROM %1$I.sandbox_lease_holders holder
|
|
123
|
+
WHERE holder.lease_id = NEW.lease_id
|
|
124
|
+
AND holder.kind = 'process'
|
|
125
|
+
AND holder.holder_id = NEW.holder_id
|
|
126
|
+
)
|
|
127
|
+
AND NOT EXISTS (
|
|
128
|
+
SELECT 1 FROM %1$I.sandbox_pty_sessions pty
|
|
129
|
+
WHERE pty.retained_process_id = NEW.id
|
|
130
|
+
AND pty.status = 'open'
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
) THEN
|
|
135
|
+
RAISE EXCEPTION 'retained process does not match its parent admission and holder state'
|
|
136
|
+
USING ERRCODE = '55000';
|
|
137
|
+
END IF;
|
|
138
|
+
RETURN NEW;
|
|
139
|
+
END;
|
|
140
|
+
$function$;
|
|
141
|
+
$create$, data_schema);
|
|
142
|
+
|
|
143
|
+
-- Cross-workspace claim for the sole control-worker reaper. The index-backed
|
|
144
|
+
-- candidate window is locked and limited before owner eligibility joins. A
|
|
145
|
+
-- live owner is deferred so it cannot permanently occupy the oldest due edge.
|
|
146
|
+
-- Claim expiry is represented by reconcile_after; it remains coordination
|
|
147
|
+
-- recovery only and is never physical-exit proof.
|
|
148
|
+
EXECUTE format($create$
|
|
149
|
+
CREATE OR REPLACE FUNCTION opengeni_private.claim_terminal_retained_processes(
|
|
150
|
+
p_claim_id uuid,
|
|
151
|
+
p_limit integer,
|
|
152
|
+
p_claim_ttl_ms bigint
|
|
153
|
+
)
|
|
154
|
+
RETURNS TABLE (
|
|
155
|
+
account_id uuid,
|
|
156
|
+
workspace_id uuid,
|
|
157
|
+
session_id uuid,
|
|
158
|
+
process_id uuid,
|
|
159
|
+
claim_id uuid,
|
|
160
|
+
owner_state text,
|
|
161
|
+
owner_attempt_outcome text
|
|
162
|
+
)
|
|
163
|
+
LANGUAGE plpgsql
|
|
164
|
+
SECURITY DEFINER
|
|
165
|
+
SET search_path = pg_catalog
|
|
166
|
+
AS $function$
|
|
167
|
+
BEGIN
|
|
168
|
+
IF p_limit < 1 OR p_limit > 100 THEN
|
|
169
|
+
RAISE EXCEPTION 'retained-process reconciliation limit must be between 1 and 100'
|
|
170
|
+
USING ERRCODE = '22023';
|
|
171
|
+
END IF;
|
|
172
|
+
IF p_claim_ttl_ms < 0 OR p_claim_ttl_ms > 3600000 THEN
|
|
173
|
+
RAISE EXCEPTION 'retained-process reconciliation claim TTL is invalid'
|
|
174
|
+
USING ERRCODE = '22023';
|
|
175
|
+
END IF;
|
|
176
|
+
|
|
177
|
+
PERFORM pg_catalog.set_config('opengeni.sandbox_recovery_protocol_v2', '1', true);
|
|
178
|
+
|
|
179
|
+
RETURN QUERY
|
|
180
|
+
WITH candidate_window AS MATERIALIZED (
|
|
181
|
+
SELECT process.id,
|
|
182
|
+
process.workspace_id,
|
|
183
|
+
process.owner_actor_kind,
|
|
184
|
+
process.owner_turn_id,
|
|
185
|
+
process.owner_attempt_id,
|
|
186
|
+
process.reconcile_after AS due_at,
|
|
187
|
+
process.started_at
|
|
188
|
+
FROM %1$I.sandbox_retained_processes process
|
|
189
|
+
WHERE process.state = 'active'
|
|
190
|
+
AND process.reconcile_after <= pg_catalog.now()
|
|
191
|
+
ORDER BY process.reconcile_after, process.started_at, process.id
|
|
192
|
+
FOR UPDATE OF process SKIP LOCKED
|
|
193
|
+
LIMIT p_limit
|
|
194
|
+
), classified AS MATERIALIZED (
|
|
195
|
+
SELECT candidate.id,
|
|
196
|
+
candidate.due_at,
|
|
197
|
+
candidate.started_at,
|
|
198
|
+
candidate.owner_actor_kind = 'direct' OR attempt.state = 'closed' AS eligible,
|
|
199
|
+
CASE
|
|
200
|
+
WHEN candidate.owner_actor_kind = 'direct' THEN 'direct'
|
|
201
|
+
ELSE COALESCE(turn_row.status, 'missing')
|
|
202
|
+
END AS owner_state,
|
|
203
|
+
attempt.outcome AS owner_attempt_outcome
|
|
204
|
+
FROM candidate_window candidate
|
|
205
|
+
LEFT JOIN LATERAL (
|
|
206
|
+
SELECT source_turn.status
|
|
207
|
+
FROM %1$I.session_turns source_turn
|
|
208
|
+
WHERE source_turn.workspace_id = candidate.workspace_id
|
|
209
|
+
AND source_turn.id = candidate.owner_turn_id
|
|
210
|
+
LIMIT 1
|
|
211
|
+
) turn_row ON true
|
|
212
|
+
LEFT JOIN LATERAL (
|
|
213
|
+
SELECT source_attempt.state, source_attempt.outcome
|
|
214
|
+
FROM %1$I.session_turn_attempts source_attempt
|
|
215
|
+
WHERE source_attempt.workspace_id = candidate.workspace_id
|
|
216
|
+
AND source_attempt.id = candidate.owner_attempt_id
|
|
217
|
+
LIMIT 1
|
|
218
|
+
) attempt ON true
|
|
219
|
+
), inspected AS (
|
|
220
|
+
UPDATE %1$I.sandbox_retained_processes process SET
|
|
221
|
+
reconcile_after = CASE
|
|
222
|
+
WHEN classified.eligible THEN pg_catalog.now()
|
|
223
|
+
+ pg_catalog.make_interval(secs => p_claim_ttl_ms / 1000.0)
|
|
224
|
+
ELSE pg_catalog.now() + interval '30 seconds'
|
|
225
|
+
END,
|
|
226
|
+
reconcile_claim_id = CASE WHEN classified.eligible THEN p_claim_id ELSE NULL END,
|
|
227
|
+
reconcile_claimed_at = CASE
|
|
228
|
+
WHEN classified.eligible THEN pg_catalog.now() ELSE NULL
|
|
229
|
+
END,
|
|
230
|
+
reconcile_attempts = process.reconcile_attempts
|
|
231
|
+
+ CASE WHEN classified.eligible THEN 1 ELSE 0 END,
|
|
232
|
+
last_reconcile_outcome = CASE
|
|
233
|
+
WHEN classified.eligible THEN 'claimed' ELSE 'owner_active'
|
|
234
|
+
END
|
|
235
|
+
FROM classified
|
|
236
|
+
WHERE process.id = classified.id
|
|
237
|
+
AND process.state = 'active'
|
|
238
|
+
RETURNING process.account_id,
|
|
239
|
+
process.workspace_id,
|
|
240
|
+
process.session_id,
|
|
241
|
+
process.id,
|
|
242
|
+
process.reconcile_claim_id,
|
|
243
|
+
classified.due_at,
|
|
244
|
+
classified.started_at,
|
|
245
|
+
classified.eligible,
|
|
246
|
+
classified.owner_state,
|
|
247
|
+
classified.owner_attempt_outcome
|
|
248
|
+
)
|
|
249
|
+
SELECT inspected.account_id,
|
|
250
|
+
inspected.workspace_id,
|
|
251
|
+
inspected.session_id,
|
|
252
|
+
inspected.id,
|
|
253
|
+
inspected.reconcile_claim_id,
|
|
254
|
+
inspected.owner_state,
|
|
255
|
+
inspected.owner_attempt_outcome
|
|
256
|
+
FROM inspected
|
|
257
|
+
WHERE inspected.eligible
|
|
258
|
+
ORDER BY inspected.due_at, inspected.started_at, inspected.id;
|
|
259
|
+
|
|
260
|
+
-- Fire the exact data-schema constraint trigger while the definer still
|
|
261
|
+
-- owns cross-workspace visibility, then restore deferred mode.
|
|
262
|
+
SET CONSTRAINTS %1$I.sandbox_retained_processes_identity_v2 IMMEDIATE;
|
|
263
|
+
SET CONSTRAINTS %1$I.sandbox_retained_processes_identity_v2 DEFERRED;
|
|
264
|
+
END;
|
|
265
|
+
$function$;
|
|
266
|
+
$create$, data_schema);
|
|
267
|
+
|
|
268
|
+
-- Fixed-shape global inventories use covering partial indexes over only the
|
|
269
|
+
-- active/draining subsets; retained history and cold/warm lease inventory are
|
|
270
|
+
-- not scanned by a reaper tick. The application normalizes legacy values into
|
|
271
|
+
-- its finite label sets.
|
|
272
|
+
EXECUTE format($create$
|
|
273
|
+
CREATE OR REPLACE FUNCTION opengeni_private.count_active_retained_processes_by_owner_state()
|
|
274
|
+
RETURNS TABLE (owner_state text, active_count bigint, terminal_owner_count bigint)
|
|
275
|
+
LANGUAGE sql
|
|
276
|
+
SECURITY DEFINER
|
|
277
|
+
SET search_path = pg_catalog
|
|
278
|
+
AS $function$
|
|
279
|
+
SELECT inventory.owner_state,
|
|
280
|
+
pg_catalog.count(*)::bigint AS active_count,
|
|
281
|
+
pg_catalog.count(*) FILTER (WHERE inventory.terminal_owner)::bigint
|
|
282
|
+
AS terminal_owner_count
|
|
283
|
+
FROM (
|
|
284
|
+
SELECT 'direct'::text AS owner_state, true AS terminal_owner
|
|
285
|
+
FROM %1$I.sandbox_retained_processes process
|
|
286
|
+
WHERE process.state = 'active'
|
|
287
|
+
AND process.owner_actor_kind = 'direct'
|
|
288
|
+
UNION ALL
|
|
289
|
+
SELECT COALESCE(turn_row.status, 'missing') AS owner_state,
|
|
290
|
+
attempt.state = 'closed' AS terminal_owner
|
|
291
|
+
FROM %1$I.sandbox_retained_processes process
|
|
292
|
+
LEFT JOIN LATERAL (
|
|
293
|
+
SELECT source_turn.status
|
|
294
|
+
FROM %1$I.session_turns source_turn
|
|
295
|
+
WHERE source_turn.workspace_id = process.workspace_id
|
|
296
|
+
AND source_turn.id = process.owner_turn_id
|
|
297
|
+
LIMIT 1
|
|
298
|
+
) turn_row ON true
|
|
299
|
+
LEFT JOIN LATERAL (
|
|
300
|
+
SELECT source_attempt.state
|
|
301
|
+
FROM %1$I.session_turn_attempts source_attempt
|
|
302
|
+
WHERE source_attempt.workspace_id = process.workspace_id
|
|
303
|
+
AND source_attempt.id = process.owner_attempt_id
|
|
304
|
+
LIMIT 1
|
|
305
|
+
) attempt ON true
|
|
306
|
+
WHERE process.state = 'active'
|
|
307
|
+
AND process.owner_actor_kind = 'turn'
|
|
308
|
+
) inventory
|
|
309
|
+
GROUP BY inventory.owner_state;
|
|
310
|
+
$function$;
|
|
311
|
+
$create$, data_schema);
|
|
312
|
+
|
|
313
|
+
EXECUTE format($create$
|
|
314
|
+
CREATE OR REPLACE FUNCTION opengeni_private.count_expired_draining_sandbox_leases()
|
|
315
|
+
RETURNS TABLE (backend text, age_bucket text, count bigint)
|
|
316
|
+
LANGUAGE sql
|
|
317
|
+
SECURITY DEFINER
|
|
318
|
+
SET search_path = pg_catalog
|
|
319
|
+
AS $function$
|
|
320
|
+
SELECT lease.backend,
|
|
321
|
+
CASE
|
|
322
|
+
WHEN pg_catalog.now() - lease.expires_at < interval '5 minutes' THEN 'lt_5m'
|
|
323
|
+
WHEN pg_catalog.now() - lease.expires_at < interval '1 hour' THEN '5m_1h'
|
|
324
|
+
WHEN pg_catalog.now() - lease.expires_at < interval '1 day' THEN '1h_1d'
|
|
325
|
+
ELSE 'gte_1d'
|
|
326
|
+
END AS age_bucket,
|
|
327
|
+
pg_catalog.count(*)::bigint
|
|
328
|
+
FROM %1$I.sandbox_leases lease
|
|
329
|
+
WHERE lease.liveness = 'draining'
|
|
330
|
+
AND lease.expires_at < pg_catalog.now()
|
|
331
|
+
GROUP BY lease.backend, age_bucket;
|
|
332
|
+
$function$;
|
|
333
|
+
$create$, data_schema);
|
|
334
|
+
END $privileged_functions$;
|
|
335
|
+
|
|
336
|
+
-- PostgreSQL grants new functions to PUBLIC by default. These functions bypass
|
|
337
|
+
-- workspace RLS, so only the migration owner and explicit application role may
|
|
338
|
+
-- execute them.
|
|
339
|
+
REVOKE ALL ON FUNCTION opengeni_private.claim_terminal_retained_processes(uuid, integer, bigint)
|
|
340
|
+
FROM PUBLIC;
|
|
341
|
+
REVOKE ALL ON FUNCTION opengeni_private.count_active_retained_processes_by_owner_state()
|
|
342
|
+
FROM PUBLIC;
|
|
343
|
+
REVOKE ALL ON FUNCTION opengeni_private.count_expired_draining_sandbox_leases()
|
|
344
|
+
FROM PUBLIC;
|
|
345
|
+
|
|
346
|
+
DO $$
|
|
347
|
+
BEGIN
|
|
348
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
|
|
349
|
+
GRANT EXECUTE ON FUNCTION opengeni_private.claim_terminal_retained_processes(uuid, integer, bigint)
|
|
350
|
+
TO opengeni_app;
|
|
351
|
+
GRANT EXECUTE ON FUNCTION opengeni_private.count_active_retained_processes_by_owner_state()
|
|
352
|
+
TO opengeni_app;
|
|
353
|
+
GRANT EXECUTE ON FUNCTION opengeni_private.count_expired_draining_sandbox_leases()
|
|
354
|
+
TO opengeni_app;
|
|
355
|
+
END IF;
|
|
356
|
+
END $$;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeni/db",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.6",
|
|
4
4
|
"description": "OpenGeni persistence: Drizzle schema, RLS-scoped query layer, the SQL migration runner, and role provisioning.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -44,14 +44,15 @@
|
|
|
44
44
|
"generate": "drizzle-kit generate",
|
|
45
45
|
"migrate": "bun src/migrate.ts",
|
|
46
46
|
"provision-roles": "bun src/provision-roles.ts",
|
|
47
|
+
"assert-runtime-posture": "bun src/runtime-posture-cli.ts",
|
|
47
48
|
"typecheck": "tsgo --noEmit",
|
|
48
49
|
"build": "tsup",
|
|
49
50
|
"prepublishOnly": "bash ../../scripts/prepublish-guard"
|
|
50
51
|
},
|
|
51
52
|
"dependencies": {
|
|
52
53
|
"@opengeni/codex": "^0.2.7",
|
|
53
|
-
"@opengeni/config": "^0.7.
|
|
54
|
-
"@opengeni/contracts": "^0.19.
|
|
54
|
+
"@opengeni/config": "^0.7.6",
|
|
55
|
+
"@opengeni/contracts": "^0.19.4",
|
|
55
56
|
"@opengeni/network": "^0.1.1",
|
|
56
57
|
"drizzle-orm": "^0.45.2",
|
|
57
58
|
"postgres": "^3.4.7"
|