@opengeni/db 3.4.0-canary.2 → 3.6.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.
Files changed (67) hide show
  1. package/dist/canonical-human-identities.d.ts +20 -0
  2. package/dist/canonical-human-identities.js +7 -3
  3. package/dist/{chunk-ZWRIPCTR.js → chunk-2NT476HD.js} +2 -2
  4. package/dist/{chunk-PYMY3AJW.js → chunk-744DFC65.js} +202 -7
  5. package/dist/chunk-744DFC65.js.map +1 -0
  6. package/dist/{chunk-O6FTELKH.js → chunk-CDMMSUWJ.js} +3 -3
  7. package/dist/chunk-G3732M3T.js +299 -0
  8. package/dist/chunk-G3732M3T.js.map +1 -0
  9. package/dist/{chunk-TRBOWUG3.js → chunk-IW6O6G7R.js} +2 -2
  10. package/dist/{chunk-HY3F5Q4E.js → chunk-Q5YUGQVK.js} +3 -3
  11. package/dist/{chunk-V3TRUXFJ.js → chunk-UGDVSQ6U.js} +72 -19
  12. package/dist/chunk-UGDVSQ6U.js.map +1 -0
  13. package/dist/{chunk-C2AFP7SB.js → chunk-UJ2EW3QI.js} +6664 -6051
  14. package/dist/chunk-UJ2EW3QI.js.map +1 -0
  15. package/dist/{chunk-H7MXYG7R.js → chunk-WW7AJ5IC.js} +2 -2
  16. package/dist/editable-artifact-durable-export.js +2 -2
  17. package/dist/editable-artifacts.js +3 -3
  18. package/dist/index.d.ts +44 -6
  19. package/dist/index.js +2914 -1535
  20. package/dist/index.js.map +1 -1
  21. package/dist/managed-auth-session-set-schema.d.ts +1399 -0
  22. package/dist/managed-auth-session-sets.d.ts +145 -0
  23. package/dist/managed-auth-session-sets.js +50 -0
  24. package/dist/managed-auth-session-sets.js.map +1 -0
  25. package/dist/organization-recovery-schema.d.ts +2144 -0
  26. package/dist/organization-recovery.d.ts +186 -0
  27. package/dist/provision-roles.js +1 -1
  28. package/dist/runtime-posture.d.ts +7 -4
  29. package/dist/schema.d.ts +9 -6
  30. package/dist/schema.js +43 -1
  31. package/dist/session-background-commands.js +3 -3
  32. package/dist/session-control.d.ts +5 -3
  33. package/dist/session-queue-commands.d.ts +2 -0
  34. package/dist/session-tenancy.js +3 -3
  35. package/dist/video-generation.js +3 -3
  36. package/dist/work-claims-schema.d.ts +975 -0
  37. package/dist/work-claims.d.ts +83 -0
  38. package/drizzle/0362_managed_auth_session_sets.sql +2347 -0
  39. package/drizzle/0363_organization_recovery_custody.sql +2418 -0
  40. package/drizzle/0364_workspace_learning_policy_snapshot_lock_order.sql +221 -0
  41. package/drizzle/0365_permission_scoped_work_claims.sql +1306 -0
  42. package/drizzle/0366_session_discovery_title_search_index.sql +5 -0
  43. package/drizzle/0367_session_discovery_goal_search_index.sql +5 -0
  44. package/drizzle/0368_session_discovery_claim_search_index.sql +6 -0
  45. package/package.json +10 -6
  46. package/src/canonical-human-identities.ts +105 -27
  47. package/src/index.ts +1471 -433
  48. package/src/managed-auth-session-set-schema.ts +176 -0
  49. package/src/managed-auth-session-sets.ts +430 -0
  50. package/src/organization-recovery-schema.ts +277 -0
  51. package/src/organization-recovery.ts +424 -0
  52. package/src/provision-roles.ts +67 -0
  53. package/src/runtime-posture.ts +144 -6
  54. package/src/schema.ts +3 -0
  55. package/src/session-control.ts +255 -16
  56. package/src/session-queue-commands.ts +10 -1
  57. package/src/session-tool-call-settlement.ts +6 -1
  58. package/src/work-claims-schema.ts +148 -0
  59. package/src/work-claims.ts +200 -0
  60. package/dist/chunk-C2AFP7SB.js.map +0 -1
  61. package/dist/chunk-PYMY3AJW.js.map +0 -1
  62. package/dist/chunk-V3TRUXFJ.js.map +0 -1
  63. /package/dist/{chunk-ZWRIPCTR.js.map → chunk-2NT476HD.js.map} +0 -0
  64. /package/dist/{chunk-O6FTELKH.js.map → chunk-CDMMSUWJ.js.map} +0 -0
  65. /package/dist/{chunk-TRBOWUG3.js.map → chunk-IW6O6G7R.js.map} +0 -0
  66. /package/dist/{chunk-HY3F5Q4E.js.map → chunk-Q5YUGQVK.js.map} +0 -0
  67. /package/dist/{chunk-H7MXYG7R.js.map → chunk-WW7AJ5IC.js.map} +0 -0
@@ -0,0 +1,221 @@
1
+ -- deployment-mode: rolling
2
+ -- Migration 0364: keep accepted-attempt learning-policy snapshots on the
3
+ -- canonical workspace -> session -> turn -> attempt lock order.
4
+ --
5
+ -- The original snapshot function locked attempt, turn, and session rows in one
6
+ -- joined SELECT ... FOR SHARE statement. PostgreSQL may acquire row locks in
7
+ -- plan order rather than SQL join order. An attempt-id lookup therefore took a
8
+ -- turn SHARE lock before reaching the session row, while an ordinary lifecycle
9
+ -- writer already held the session and waited for the turn. The two transactions
10
+ -- formed a session/turn cycle and PostgreSQL aborted the snapshot with 40P01.
11
+ --
12
+ -- Acquire each row in the same explicit order as the session lifecycle:
13
+ -- workspaces KEY SHARE, sessions SHARE, session_turns SHARE, then
14
+ -- session_turn_attempts SHARE. A fresh statement revalidates the complete
15
+ -- authority tuple and interruption state after all locks are held, preserving
16
+ -- the accepted-attempt invariant without a planner-dependent lock order.
17
+
18
+ SET LOCAL lock_timeout = '5s';
19
+ SET LOCAL statement_timeout = '5min';
20
+
21
+ DO $snapshot_lock_order$
22
+ DECLARE target_schema text := current_schema();
23
+ BEGIN
24
+ EXECUTE format($ddl$
25
+ CREATE OR REPLACE FUNCTION %I.workspace_learning_policy_get_or_create_snapshot(
26
+ p_account_id uuid,
27
+ p_workspace_id uuid,
28
+ p_session_id uuid,
29
+ p_turn_id uuid,
30
+ p_attempt_id uuid,
31
+ p_execution_generation integer
32
+ ) RETURNS TABLE (snapshot_id uuid)
33
+ LANGUAGE plpgsql SECURITY DEFINER SET search_path = %I, pg_catalog
34
+ AS $body$
35
+ DECLARE
36
+ accepted_at timestamptz;
37
+ canonical jsonb;
38
+ existing_snapshot record;
39
+ snapshot_revision_id uuid;
40
+ snapshot_revision bigint;
41
+ snapshot_policy_hash text;
42
+ snapshot_activation_version bigint;
43
+ snapshot_activated_at timestamptz;
44
+ snapshot_workspace_mode text;
45
+ snapshot_source_overrides jsonb;
46
+ BEGIN
47
+ IF NULLIF(current_setting('opengeni.account_id', true), '')::uuid IS DISTINCT FROM p_account_id
48
+ OR NULLIF(current_setting('opengeni.workspace_id', true), '')::uuid IS DISTINCT FROM p_workspace_id
49
+ THEN
50
+ RAISE EXCEPTION 'learning-policy snapshot requires exact workspace authority'
51
+ USING ERRCODE = '42501';
52
+ END IF;
53
+
54
+ SELECT * INTO existing_snapshot
55
+ FROM "workspace_learning_policy_snapshots" snapshot
56
+ WHERE snapshot."account_id" = p_account_id
57
+ AND snapshot."workspace_id" = p_workspace_id
58
+ AND snapshot."attempt_id" = p_attempt_id;
59
+ IF FOUND THEN
60
+ IF existing_snapshot."session_id" IS DISTINCT FROM p_session_id
61
+ OR existing_snapshot."turn_id" IS DISTINCT FROM p_turn_id
62
+ OR existing_snapshot."execution_generation" IS DISTINCT FROM p_execution_generation
63
+ THEN
64
+ RAISE EXCEPTION 'learning-policy snapshot attempt identity conflicted'
65
+ USING ERRCODE = '23514';
66
+ END IF;
67
+ snapshot_id := existing_snapshot."id";
68
+ RETURN NEXT;
69
+ RETURN;
70
+ END IF;
71
+
72
+ PERFORM 1
73
+ FROM "workspaces" workspace
74
+ WHERE workspace."id" = p_workspace_id
75
+ AND workspace."account_id" = p_account_id
76
+ FOR KEY SHARE OF workspace;
77
+ IF NOT FOUND THEN
78
+ RAISE EXCEPTION 'learning-policy snapshot requires an exact active attempt'
79
+ USING ERRCODE = '23514';
80
+ END IF;
81
+
82
+ PERFORM 1
83
+ FROM "sessions" session
84
+ WHERE session."account_id" = p_account_id
85
+ AND session."workspace_id" = p_workspace_id
86
+ AND session."id" = p_session_id
87
+ AND session."active_turn_id" = p_turn_id
88
+ FOR SHARE OF session;
89
+ IF NOT FOUND THEN
90
+ RAISE EXCEPTION 'learning-policy snapshot requires an exact active attempt'
91
+ USING ERRCODE = '23514';
92
+ END IF;
93
+
94
+ SELECT turn."created_at" INTO accepted_at
95
+ FROM "session_turns" turn
96
+ WHERE turn."account_id" = p_account_id
97
+ AND turn."workspace_id" = p_workspace_id
98
+ AND turn."session_id" = p_session_id
99
+ AND turn."id" = p_turn_id
100
+ AND turn."execution_generation" = p_execution_generation
101
+ AND turn."active_attempt_id" = p_attempt_id
102
+ AND turn."status" IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
103
+ FOR SHARE OF turn;
104
+ IF NOT FOUND THEN
105
+ RAISE EXCEPTION 'learning-policy snapshot requires an exact active attempt'
106
+ USING ERRCODE = '23514';
107
+ END IF;
108
+
109
+ PERFORM 1
110
+ FROM "session_turn_attempts" attempt
111
+ WHERE attempt."id" = p_attempt_id
112
+ AND attempt."account_id" = p_account_id
113
+ AND attempt."workspace_id" = p_workspace_id
114
+ AND attempt."session_id" = p_session_id
115
+ AND attempt."turn_id" = p_turn_id
116
+ AND attempt."execution_generation" = p_execution_generation
117
+ AND attempt."state" IN ('claimed', 'running')
118
+ FOR SHARE OF attempt;
119
+ IF NOT FOUND THEN
120
+ RAISE EXCEPTION 'learning-policy snapshot requires an exact active attempt'
121
+ USING ERRCODE = '23514';
122
+ END IF;
123
+
124
+ -- This attempt SHARE lock is also the interruption-creation fence. Every
125
+ -- supported interruption writer takes the exact attempt FOR UPDATE before
126
+ -- inserting session_attempt_interruptions. Row locks live until transaction
127
+ -- end, so no such writer can cross the revalidation -> insert -> commit
128
+ -- interval below.
129
+ -- A lock acquisition may wait behind Pause, Steer, replacement, or
130
+ -- settlement. Re-evaluate the complete tuple in a fresh READ COMMITTED
131
+ -- statement after the canonical rows are locked so a pre-wait snapshot
132
+ -- cannot miss the change that just released the lock.
133
+ PERFORM 1
134
+ FROM "sessions" session
135
+ JOIN "session_turns" turn
136
+ ON turn."account_id" = session."account_id"
137
+ AND turn."workspace_id" = session."workspace_id"
138
+ AND turn."session_id" = session."id"
139
+ JOIN "session_turn_attempts" attempt
140
+ ON attempt."account_id" = turn."account_id"
141
+ AND attempt."workspace_id" = turn."workspace_id"
142
+ AND attempt."session_id" = turn."session_id"
143
+ AND attempt."turn_id" = turn."id"
144
+ WHERE session."account_id" = p_account_id
145
+ AND session."workspace_id" = p_workspace_id
146
+ AND session."id" = p_session_id
147
+ AND session."active_turn_id" = p_turn_id
148
+ AND turn."id" = p_turn_id
149
+ AND turn."execution_generation" = p_execution_generation
150
+ AND turn."active_attempt_id" = p_attempt_id
151
+ AND turn."status" IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
152
+ AND attempt."id" = p_attempt_id
153
+ AND attempt."execution_generation" = p_execution_generation
154
+ AND attempt."state" IN ('claimed', 'running')
155
+ AND NOT EXISTS (
156
+ SELECT 1
157
+ FROM "session_attempt_interruptions" interruption
158
+ WHERE interruption."workspace_id" = p_workspace_id
159
+ AND interruption."attempt_id" = p_attempt_id
160
+ AND interruption."state" IN ('pending', 'delivered', 'acknowledged')
161
+ );
162
+ IF NOT FOUND THEN
163
+ RAISE EXCEPTION 'learning-policy snapshot requires an exact active attempt'
164
+ USING ERRCODE = '23514';
165
+ END IF;
166
+
167
+ canonical := workspace_learning_policy_canonical_at(
168
+ p_account_id,
169
+ p_workspace_id,
170
+ accepted_at
171
+ );
172
+ snapshot_revision_id := (canonical->>'revisionId')::uuid;
173
+ snapshot_revision := (canonical->>'revision')::bigint;
174
+ snapshot_policy_hash := canonical->>'policyHash';
175
+ snapshot_activation_version := (canonical->>'activationVersion')::bigint;
176
+ snapshot_activated_at := (canonical->>'activatedAt')::timestamptz;
177
+ snapshot_workspace_mode := canonical->>'workspaceMode';
178
+ snapshot_source_overrides := canonical->'sourceOverrides';
179
+
180
+ INSERT INTO "workspace_learning_policy_snapshots" (
181
+ "account_id", "workspace_id", "session_id", "turn_id", "attempt_id",
182
+ "execution_generation", "revision_id", "revision", "policy_hash",
183
+ "activation_version", "activated_at", "workspace_mode", "source_overrides",
184
+ "snapshot_hash"
185
+ ) VALUES (
186
+ p_account_id, p_workspace_id, p_session_id, p_turn_id, p_attempt_id,
187
+ p_execution_generation, snapshot_revision_id, snapshot_revision, snapshot_policy_hash,
188
+ snapshot_activation_version, snapshot_activated_at, snapshot_workspace_mode,
189
+ snapshot_source_overrides,
190
+ workspace_learning_policy_snapshot_hash(
191
+ snapshot_revision_id,
192
+ snapshot_revision,
193
+ snapshot_policy_hash,
194
+ snapshot_activation_version,
195
+ snapshot_activated_at,
196
+ snapshot_workspace_mode,
197
+ snapshot_source_overrides
198
+ )
199
+ )
200
+ ON CONFLICT ("account_id", "workspace_id", "attempt_id") DO NOTHING
201
+ RETURNING "id" INTO snapshot_id;
202
+
203
+ IF snapshot_id IS NULL THEN
204
+ SELECT snapshot."id" INTO snapshot_id
205
+ FROM "workspace_learning_policy_snapshots" snapshot
206
+ WHERE snapshot."account_id" = p_account_id
207
+ AND snapshot."workspace_id" = p_workspace_id
208
+ AND snapshot."attempt_id" = p_attempt_id
209
+ AND snapshot."session_id" = p_session_id
210
+ AND snapshot."turn_id" = p_turn_id
211
+ AND snapshot."execution_generation" = p_execution_generation;
212
+ IF NOT FOUND THEN
213
+ RAISE EXCEPTION 'learning-policy snapshot concurrent identity conflicted'
214
+ USING ERRCODE = '23514';
215
+ END IF;
216
+ END IF;
217
+ RETURN NEXT;
218
+ END
219
+ $body$
220
+ $ddl$, target_schema, target_schema);
221
+ END $snapshot_lock_order$;