@capybara-hq/kernel 0.1.0-rc.124 → 0.1.0-rc.126
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/agents/onboarding-checklist.d.ts +1 -0
- package/dist/agents/onboarding-checklist.js +14 -1
- package/dist/catalog/catalog.module.js +0 -3
- package/dist/catalog/org-catalog.service.d.ts +0 -2
- package/dist/catalog/org-catalog.service.js +0 -4
- package/dist/catalog/org-extension.controller.d.ts +5 -9
- package/dist/catalog/org-extension.controller.js +22 -22
- package/dist/catalog/org-extension.schemas.d.ts +0 -1
- package/dist/catalog/org-extension.schemas.js +0 -2
- package/dist/catalog/plugin-install.d.ts +4 -1
- package/dist/catalog/plugin-install.js +4 -1
- package/dist/conversations/events.d.ts +10 -154
- package/dist/conversations/events.js +8 -83
- package/dist/devices/conversation-devices.d.ts +1 -2
- package/dist/devices/devices.service.d.ts +8 -1
- package/dist/devices/devices.service.js +1 -1
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +5 -0
- package/dist/evals/recorded-permission-context.js +1 -3
- package/dist/evals/recorded-tools.d.ts +3 -4
- package/dist/evals/recorded-tools.js +6 -7
- package/dist/extensions/org-config.d.ts +35 -8
- package/dist/extensions/org-config.js +61 -14
- package/dist/extensions/plugin-items.d.ts +16 -0
- package/dist/extensions/plugin-items.js +35 -0
- package/dist/platform/operation.d.ts +0 -2
- package/dist/platform/ops/extensions.ops.d.ts +4 -6
- package/dist/platform/ops/extensions.ops.js +17 -34
- package/dist/runtime/context/build.d.ts +1 -1
- package/dist/runtime/context/build.js +1 -10
- package/dist/runtime/context/device-paths.d.ts +5 -82
- package/dist/runtime/context/device-paths.js +3 -174
- package/dist/runtime/context/permission-profile.d.ts +3 -13
- package/dist/runtime/context/permission-profile.js +9 -26
- package/dist/runtime/permission-context.service.js +3 -4
- package/dist/runtime/run-worker.js +6 -5
- package/package.json +1 -1
- package/src/migrations/0210_normalize_conversation_history.sql +1012 -0
- package/dist/catalog/org-extension-switch.d.ts +0 -41
- package/dist/catalog/org-extension-switch.js +0 -123
|
@@ -0,0 +1,1012 @@
|
|
|
1
|
+
-- Freeze the #1180 history inventory at the reader in 7f9219a07. The helpers are local to
|
|
2
|
+
-- this migration's connection. Surviving event identities and replay coordinates stay intact.
|
|
3
|
+
-- Convert a JSON number as the JavaScript reader does. Infinity cannot pass a Zod number
|
|
4
|
+
-- validator. Catch PostgreSQL's underflow error for values JavaScript rounds to zero.
|
|
5
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_number(value jsonb)
|
|
6
|
+
RETURNS double precision LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
7
|
+
BEGIN
|
|
8
|
+
IF jsonb_typeof(value) <> 'number' THEN RETURN NULL; END IF;
|
|
9
|
+
RETURN (value #>> '{}')::double precision;
|
|
10
|
+
EXCEPTION WHEN numeric_value_out_of_range THEN
|
|
11
|
+
IF abs((value #>> '{}')::numeric) < 1 THEN RETURN 0; END IF;
|
|
12
|
+
RETURN NULL;
|
|
13
|
+
END
|
|
14
|
+
$$;
|
|
15
|
+
|
|
16
|
+
-- Match canonicalPermissionJson for the sandbox shape. Its only numeric field is a small
|
|
17
|
+
-- integer. An unrecognized value inside the removed location can be hashed only if its
|
|
18
|
+
-- number spelling is also provable. Returning NULL preserves the original digest otherwise.
|
|
19
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_canonical_sandbox_json(value jsonb)
|
|
20
|
+
RETURNS text LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
21
|
+
DECLARE
|
|
22
|
+
n double precision;
|
|
23
|
+
joined text;
|
|
24
|
+
BEGIN
|
|
25
|
+
CASE jsonb_typeof(value)
|
|
26
|
+
WHEN 'object' THEN
|
|
27
|
+
SELECT CASE WHEN count(*) = count(part) THEN '{' || coalesce(
|
|
28
|
+
string_agg(to_jsonb(key)::text || ':' || part, ',' ORDER BY key COLLATE "C"), ''
|
|
29
|
+
) || '}' END INTO joined
|
|
30
|
+
FROM (
|
|
31
|
+
SELECT key, pg_temp.history_canonical_sandbox_json(item) AS part
|
|
32
|
+
FROM jsonb_each(value) AS entry(key, item)
|
|
33
|
+
) AS fields;
|
|
34
|
+
RETURN joined;
|
|
35
|
+
WHEN 'array' THEN
|
|
36
|
+
SELECT CASE WHEN count(*) = count(part) THEN '[' || coalesce(
|
|
37
|
+
string_agg(part, ',' ORDER BY position), ''
|
|
38
|
+
) || ']' END INTO joined
|
|
39
|
+
FROM (
|
|
40
|
+
SELECT position, pg_temp.history_canonical_sandbox_json(item) AS part
|
|
41
|
+
FROM jsonb_array_elements(value) WITH ORDINALITY AS entry(item, position)
|
|
42
|
+
) AS items;
|
|
43
|
+
RETURN joined;
|
|
44
|
+
WHEN 'number' THEN
|
|
45
|
+
n := pg_temp.history_number(value);
|
|
46
|
+
IF n = trunc(n) AND abs(n) <= 9007199254740991 THEN RETURN (n::bigint)::text; END IF;
|
|
47
|
+
RETURN NULL;
|
|
48
|
+
ELSE RETURN value::text;
|
|
49
|
+
END CASE;
|
|
50
|
+
END
|
|
51
|
+
$$;
|
|
52
|
+
|
|
53
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_sandbox_digest(sandbox jsonb)
|
|
54
|
+
RETURNS text LANGUAGE sql IMMUTABLE STRICT AS $$
|
|
55
|
+
SELECT encode(sha256(convert_to(
|
|
56
|
+
'["permission_sandbox",2,' || pg_temp.history_canonical_sandbox_json(sandbox) || ']', 'UTF8'
|
|
57
|
+
)), 'hex');
|
|
58
|
+
$$;
|
|
59
|
+
|
|
60
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_normalized(event_type text, payload jsonb)
|
|
61
|
+
RETURNS jsonb LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
62
|
+
DECLARE
|
|
63
|
+
scope jsonb;
|
|
64
|
+
location jsonb;
|
|
65
|
+
BEGIN
|
|
66
|
+
IF event_type = 'permission_context' AND jsonb_typeof(payload) = 'object' THEN
|
|
67
|
+
IF pg_temp.history_number(payload->'version') = 1 AND NOT payload ? 'sandbox' THEN
|
|
68
|
+
payload := payload || '{"version":2,"sandbox":null}';
|
|
69
|
+
END IF;
|
|
70
|
+
scope := payload->'scope';
|
|
71
|
+
IF jsonb_typeof(scope) = 'object' AND scope ? 'location' THEN
|
|
72
|
+
location := CASE scope->>'location'
|
|
73
|
+
WHEN 'local' THEN '"operation"'::jsonb WHEN 'cloud' THEN '"instance"'::jsonb
|
|
74
|
+
ELSE scope->'location' END;
|
|
75
|
+
scope := jsonb_build_object('boundary', location) || (scope - 'location');
|
|
76
|
+
IF jsonb_typeof(scope->'filesystem') = 'object' THEN
|
|
77
|
+
scope := scope #- '{filesystem,readDirectories}';
|
|
78
|
+
END IF;
|
|
79
|
+
payload := jsonb_set(payload, '{scope}', scope);
|
|
80
|
+
END IF;
|
|
81
|
+
IF jsonb_typeof(payload->'sandbox') = 'object' THEN
|
|
82
|
+
payload := payload #- '{sandbox,location}';
|
|
83
|
+
END IF;
|
|
84
|
+
ELSIF event_type = 'permission_reviewed' AND jsonb_typeof(payload->'permissionScope') = 'object' THEN
|
|
85
|
+
payload := payload #- '{permissionScope,readDirectories}';
|
|
86
|
+
ELSIF event_type = 'approval_requested'
|
|
87
|
+
AND jsonb_typeof(payload#>'{command,permissionScope}') = 'object' THEN
|
|
88
|
+
payload := payload #- '{command,permissionScope,readDirectories}';
|
|
89
|
+
END IF;
|
|
90
|
+
RETURN payload;
|
|
91
|
+
END
|
|
92
|
+
$$;
|
|
93
|
+
|
|
94
|
+
-- JavaScript string bounds count UTF-16 code units. PostgreSQL text counts code points.
|
|
95
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_utf16_length(value text)
|
|
96
|
+
RETURNS integer LANGUAGE sql IMMUTABLE STRICT AS $$
|
|
97
|
+
SELECT length(value) + length(regexp_replace(value COLLATE "C", '[^' || chr(65536) || '-' ||
|
|
98
|
+
chr(1114111) || ']', '', 'g'));
|
|
99
|
+
$$;
|
|
100
|
+
|
|
101
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_path(value text)
|
|
102
|
+
RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
103
|
+
DECLARE
|
|
104
|
+
part text;
|
|
105
|
+
token text;
|
|
106
|
+
decoded text;
|
|
107
|
+
bytes bytea;
|
|
108
|
+
offset_in_part integer;
|
|
109
|
+
BEGIN
|
|
110
|
+
IF left(value, 1) <> '/' OR (value <> '/' AND right(value, 1) = '/')
|
|
111
|
+
OR strpos(value, '//') > 0 OR strpos(value, chr(92)) > 0
|
|
112
|
+
OR value COLLATE "C" ~ '[[:cntrl:]]' THEN
|
|
113
|
+
RETURN false;
|
|
114
|
+
END IF;
|
|
115
|
+
FOREACH part IN ARRAY string_to_array(value, '/') LOOP
|
|
116
|
+
IF part IN ('.', '..') THEN RETURN false; END IF;
|
|
117
|
+
IF strpos(part, '%') = 0 THEN CONTINUE; END IF;
|
|
118
|
+
bytes := ''::bytea;
|
|
119
|
+
offset_in_part := 1;
|
|
120
|
+
WHILE offset_in_part <= length(part) LOOP
|
|
121
|
+
token := substr(part, offset_in_part, 1);
|
|
122
|
+
IF token = '%' THEN
|
|
123
|
+
token := substr(part, offset_in_part + 1, 2);
|
|
124
|
+
-- decodeURIComponent leaves the whole segment literal if decoding fails.
|
|
125
|
+
IF token COLLATE "C" !~ '^[0-9a-fA-F]{2}$' THEN
|
|
126
|
+
bytes := NULL;
|
|
127
|
+
EXIT;
|
|
128
|
+
END IF;
|
|
129
|
+
-- PostgreSQL cannot put NUL in text. Its presence in a decoded segment is allowed by
|
|
130
|
+
-- the old check and is distinct from the three rejected decoded forms.
|
|
131
|
+
bytes := bytes || decode(CASE WHEN token = '00' THEN '01' ELSE token END, 'hex');
|
|
132
|
+
offset_in_part := offset_in_part + 3;
|
|
133
|
+
ELSE
|
|
134
|
+
bytes := bytes || convert_to(token, 'UTF8');
|
|
135
|
+
offset_in_part := offset_in_part + 1;
|
|
136
|
+
END IF;
|
|
137
|
+
END LOOP;
|
|
138
|
+
IF bytes IS NULL THEN CONTINUE; END IF;
|
|
139
|
+
BEGIN
|
|
140
|
+
decoded := convert_from(bytes, 'UTF8');
|
|
141
|
+
EXCEPTION WHEN character_not_in_repertoire OR untranslatable_character THEN
|
|
142
|
+
CONTINUE;
|
|
143
|
+
END;
|
|
144
|
+
IF decoded = '..' OR strpos(decoded, '/') > 0 OR strpos(decoded, chr(92)) > 0 THEN
|
|
145
|
+
RETURN false;
|
|
146
|
+
END IF;
|
|
147
|
+
END LOOP;
|
|
148
|
+
RETURN true;
|
|
149
|
+
END
|
|
150
|
+
$$;
|
|
151
|
+
|
|
152
|
+
-- Structural constraints below are frozen input schemas from the four inventory validators.
|
|
153
|
+
-- Additional properties remain allowed on objects where Zod strips them. The extra check field
|
|
154
|
+
-- covers refinements that do not appear in JSON Schema.
|
|
155
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_shape(value jsonb, shape jsonb)
|
|
156
|
+
RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
157
|
+
DECLARE
|
|
158
|
+
field record;
|
|
159
|
+
item jsonb;
|
|
160
|
+
count_matches integer := 0;
|
|
161
|
+
child_shapes jsonb := coalesce(shape->'oneOf', shape->'anyOf');
|
|
162
|
+
kind text := shape->>'type';
|
|
163
|
+
str text;
|
|
164
|
+
n double precision;
|
|
165
|
+
prev text;
|
|
166
|
+
BEGIN
|
|
167
|
+
IF child_shapes IS NOT NULL THEN
|
|
168
|
+
FOR item IN SELECT * FROM jsonb_array_elements(child_shapes) LOOP
|
|
169
|
+
IF pg_temp.history_shape(value, item) THEN count_matches := count_matches + 1; END IF;
|
|
170
|
+
END LOOP;
|
|
171
|
+
IF count_matches = 0 OR (shape ? 'oneOf' AND count_matches <> 1) THEN RETURN false; END IF;
|
|
172
|
+
END IF;
|
|
173
|
+
IF shape ? 'const' THEN
|
|
174
|
+
IF jsonb_typeof(shape->'const') = 'number' THEN
|
|
175
|
+
IF pg_temp.history_number(value) IS DISTINCT FROM pg_temp.history_number(shape->'const')
|
|
176
|
+
THEN RETURN false; END IF;
|
|
177
|
+
ELSIF value <> shape->'const' THEN RETURN false;
|
|
178
|
+
END IF;
|
|
179
|
+
END IF;
|
|
180
|
+
IF shape ? 'enum' AND NOT (shape->'enum') @> jsonb_build_array(value) THEN RETURN false; END IF;
|
|
181
|
+
IF kind IN ('integer', 'number') THEN
|
|
182
|
+
n := pg_temp.history_number(value);
|
|
183
|
+
RETURN n IS NOT NULL AND (kind <> 'integer' OR n = trunc(n))
|
|
184
|
+
AND (NOT shape ? 'minimum' OR n >= (shape->>'minimum')::double precision)
|
|
185
|
+
AND (NOT shape ? 'maximum' OR n <= (shape->>'maximum')::double precision)
|
|
186
|
+
AND (NOT shape ? 'exclusiveMinimum' OR n > (shape->>'exclusiveMinimum')::double precision);
|
|
187
|
+
END IF;
|
|
188
|
+
IF jsonb_typeof(shape->'type') = 'array' THEN
|
|
189
|
+
RETURN (shape->'type') ? jsonb_typeof(value);
|
|
190
|
+
END IF;
|
|
191
|
+
IF kind IS NOT NULL AND jsonb_typeof(value) <> kind THEN RETURN false; END IF;
|
|
192
|
+
CASE kind
|
|
193
|
+
WHEN 'object' THEN
|
|
194
|
+
IF shape ? 'required' AND NOT value ?& ARRAY(
|
|
195
|
+
SELECT jsonb_array_elements_text(shape->'required')
|
|
196
|
+
) THEN RETURN false; END IF;
|
|
197
|
+
FOR field IN SELECT * FROM jsonb_each(value) LOOP
|
|
198
|
+
IF (shape->'properties') ? field.key THEN
|
|
199
|
+
IF NOT pg_temp.history_shape(field.value, shape->'properties'->field.key) THEN
|
|
200
|
+
RETURN false;
|
|
201
|
+
END IF;
|
|
202
|
+
ELSIF shape->'additionalProperties' = 'false'::jsonb THEN RETURN false;
|
|
203
|
+
END IF;
|
|
204
|
+
END LOOP;
|
|
205
|
+
WHEN 'array' THEN
|
|
206
|
+
IF (shape ? 'minItems' AND jsonb_array_length(value) < (shape->>'minItems')::integer)
|
|
207
|
+
OR (shape ? 'maxItems' AND jsonb_array_length(value) > (shape->>'maxItems')::integer)
|
|
208
|
+
THEN RETURN false; END IF;
|
|
209
|
+
FOR item IN SELECT * FROM jsonb_array_elements(value) LOOP
|
|
210
|
+
IF NOT pg_temp.history_shape(item, shape->'items') THEN RETURN false; END IF;
|
|
211
|
+
IF shape->>'check' = 'sorted' THEN
|
|
212
|
+
str := item #>> '{}';
|
|
213
|
+
IF prev COLLATE "C" >= str COLLATE "C" THEN RETURN false; END IF;
|
|
214
|
+
prev := str;
|
|
215
|
+
END IF;
|
|
216
|
+
END LOOP;
|
|
217
|
+
WHEN 'string' THEN
|
|
218
|
+
str := value #>> '{}';
|
|
219
|
+
IF (shape ? 'minLength' AND pg_temp.history_utf16_length(str) < (shape->>'minLength')::integer)
|
|
220
|
+
OR (shape ? 'maxLength' AND pg_temp.history_utf16_length(str) > (shape->>'maxLength')::integer)
|
|
221
|
+
OR (shape ? 'pattern' AND str COLLATE "C" !~ (shape->>'pattern'))
|
|
222
|
+
THEN RETURN false; END IF;
|
|
223
|
+
CASE shape->>'check'
|
|
224
|
+
WHEN 'path' THEN RETURN pg_temp.history_path(str);
|
|
225
|
+
WHEN 'host' THEN
|
|
226
|
+
RETURN str COLLATE "C" ~ '^(\*\.)?([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]([a-z0-9-]{0,61}[a-z0-9])?$'
|
|
227
|
+
AND str COLLATE "C" !~ '\.(local|internal|localhost)$';
|
|
228
|
+
WHEN 'reason' THEN
|
|
229
|
+
-- ECMAScript trim includes BOM and excludes U+0085 (NEL).
|
|
230
|
+
RETURN btrim(str, E' \t\n\r\v\f' || chr(160) || chr(5760) ||
|
|
231
|
+
' ' || chr(8232) || chr(8233) || chr(8239) || chr(8287) ||
|
|
232
|
+
chr(12288) || chr(65279)) <> '';
|
|
233
|
+
WHEN 'justification' THEN RETURN length(str) <= 500;
|
|
234
|
+
ELSE NULL;
|
|
235
|
+
END CASE;
|
|
236
|
+
ELSE NULL;
|
|
237
|
+
END CASE;
|
|
238
|
+
RETURN true;
|
|
239
|
+
END
|
|
240
|
+
$$;
|
|
241
|
+
|
|
242
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_permission_context(p jsonb)
|
|
243
|
+
RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT AS $$
|
|
244
|
+
DECLARE
|
|
245
|
+
availability jsonb := p->'availability';
|
|
246
|
+
scope jsonb := nullif(p->'scope', 'null'::jsonb);
|
|
247
|
+
sandbox jsonb := nullif(p->'sandbox', 'null'::jsonb);
|
|
248
|
+
selected text := p->>'selectedProfile';
|
|
249
|
+
effective text := p->>'effectiveProfile';
|
|
250
|
+
boundary text := scope->>'boundary';
|
|
251
|
+
filesystem jsonb := scope->'filesystem';
|
|
252
|
+
network jsonb := scope->'network';
|
|
253
|
+
outer_network jsonb := scope->'outerNetwork';
|
|
254
|
+
host_count integer := coalesce(jsonb_array_length(network->'hosts'), 0);
|
|
255
|
+
BEGIN
|
|
256
|
+
IF ((p->>'profileRevision') IS NULL) <> (selected IS NULL) THEN RETURN false; END IF;
|
|
257
|
+
IF availability->>'state' = 'unavailable' THEN
|
|
258
|
+
IF effective IS NOT NULL OR p->>'deviceId' IS NOT NULL OR p->>'scopeRevision' IS NOT NULL
|
|
259
|
+
OR p->>'authorityRevision' IS NOT NULL OR scope IS NOT NULL THEN RETURN false; END IF;
|
|
260
|
+
IF availability->>'cause' = 'actor-device-access' THEN
|
|
261
|
+
IF selected IS NOT NULL OR availability->>'reason' NOT IN (
|
|
262
|
+
'This run''s actor cannot use the selected device',
|
|
263
|
+
'The selected device belongs to the conversation''s owner, and this run''s account is not its owner, so this run continues without the device'
|
|
264
|
+
) THEN RETURN false; END IF;
|
|
265
|
+
ELSIF availability->>'reason' <> 'Choose a device to run device operations' THEN
|
|
266
|
+
RETURN false;
|
|
267
|
+
END IF;
|
|
268
|
+
ELSIF selected IS NULL OR effective IS NULL OR p->>'deviceId' IS NULL
|
|
269
|
+
OR p->>'scopeRevision' IS NULL OR p->>'authorityRevision' IS NULL THEN RETURN false;
|
|
270
|
+
END IF;
|
|
271
|
+
IF (scope IS NOT NULL) <> (availability->>'state' = 'supported')
|
|
272
|
+
OR (sandbox IS NOT NULL AND availability->>'state' <> 'supported') THEN RETURN false; END IF;
|
|
273
|
+
IF effective IS NOT NULL THEN
|
|
274
|
+
IF p->>'execution' = 'scheduled' AND effective <> 'request-approval' THEN RETURN false; END IF;
|
|
275
|
+
IF p->>'execution' = 'interactive' AND (
|
|
276
|
+
(selected = 'full-access' AND effective NOT IN ('full-access', 'approve-for-me'))
|
|
277
|
+
OR (selected <> 'full-access' AND selected <> effective)
|
|
278
|
+
) THEN RETURN false; END IF;
|
|
279
|
+
END IF;
|
|
280
|
+
IF scope IS NULL THEN RETURN true; END IF;
|
|
281
|
+
IF effective = 'full-access' AND sandbox IS NULL THEN
|
|
282
|
+
IF filesystem->>'kind' <> 'device' OR network->>'kind' <> 'environment' THEN RETURN false; END IF;
|
|
283
|
+
ELSIF filesystem->>'kind' <> 'scoped' OR network->>'kind' = 'environment' THEN RETURN false;
|
|
284
|
+
END IF;
|
|
285
|
+
IF sandbox IS NOT NULL AND (
|
|
286
|
+
sandbox->>'boundary' <> boundary OR sandbox->>'workingRoot' <> scope->>'workingRoot'
|
|
287
|
+
OR sandbox->'outerNetwork' <> outer_network OR sandbox#>>'{network,kind}' <> network->>'kind'
|
|
288
|
+
OR pg_temp.history_number(sandbox#>'{network,hostsCount}') <> host_count
|
|
289
|
+
OR (boundary = 'instance' AND (
|
|
290
|
+
sandbox->'outsideSandbox' = 'true'::jsonb OR sandbox->'perCallHosts' = 'true'::jsonb
|
|
291
|
+
))
|
|
292
|
+
) THEN RETURN false; END IF;
|
|
293
|
+
IF filesystem->>'kind' = 'scoped'
|
|
294
|
+
AND NOT (filesystem->'writeDirectories') ? (scope->>'workingRoot') THEN RETURN false; END IF;
|
|
295
|
+
IF (boundary = 'operation') <> (outer_network->>'kind' = 'host-environment')
|
|
296
|
+
OR (network->>'kind' = 'open-public' AND outer_network->>'kind' IN ('none', 'allowlist'))
|
|
297
|
+
THEN RETURN false; END IF;
|
|
298
|
+
IF boundary = 'instance' AND network->>'kind' = 'allowlist' THEN
|
|
299
|
+
IF outer_network->>'kind' = 'none' THEN RETURN false; END IF;
|
|
300
|
+
IF outer_network->>'kind' = 'allowlist' AND EXISTS (
|
|
301
|
+
SELECT FROM jsonb_array_elements_text(network->'hosts') AS inner_host(host)
|
|
302
|
+
WHERE NOT EXISTS (
|
|
303
|
+
SELECT FROM jsonb_array_elements_text(outer_network->'hosts') AS outer_host(pattern)
|
|
304
|
+
WHERE pattern = host OR (left(pattern, 2) = '*.' AND ends_with(host, substr(pattern, 2)))
|
|
305
|
+
)
|
|
306
|
+
) THEN RETURN false; END IF;
|
|
307
|
+
END IF;
|
|
308
|
+
RETURN true;
|
|
309
|
+
END
|
|
310
|
+
$$;
|
|
311
|
+
|
|
312
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_event_incompatible(
|
|
313
|
+
event_type text, original jsonb, normalized jsonb, shape jsonb
|
|
314
|
+
) RETURNS boolean LANGUAGE sql IMMUTABLE AS $$
|
|
315
|
+
SELECT CASE
|
|
316
|
+
WHEN event_type = 'device_paths' AND NOT (
|
|
317
|
+
jsonb_typeof(original) = 'object' AND original ? 'permissionContextVersion'
|
|
318
|
+
) THEN true
|
|
319
|
+
WHEN NOT coalesce(pg_temp.history_shape(normalized, shape), false) THEN true
|
|
320
|
+
WHEN event_type = 'permission_context' THEN NOT pg_temp.history_permission_context(normalized)
|
|
321
|
+
ELSE false END;
|
|
322
|
+
$$;
|
|
323
|
+
|
|
324
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_old_sandbox_matches(
|
|
325
|
+
call jsonb, context_seq double precision, old_digest text
|
|
326
|
+
) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS $$
|
|
327
|
+
SELECT pg_temp.history_number(call#>'{request,permissionContext,version}') = 1
|
|
328
|
+
AND pg_temp.history_number(call#>'{request,permissionContext,contextSeq}') = context_seq
|
|
329
|
+
AND call#>'{request,permissionContext,sandbox}' = to_jsonb(old_digest);
|
|
330
|
+
$$;
|
|
331
|
+
|
|
332
|
+
CREATE TEMP TABLE IF NOT EXISTS history_event_shapes (type text PRIMARY KEY, shape jsonb NOT NULL) ON COMMIT DROP;
|
|
333
|
+
|
|
334
|
+
INSERT INTO history_event_shapes (type, shape) VALUES
|
|
335
|
+
('permission_reviewed', $shape$
|
|
336
|
+
{
|
|
337
|
+
"type": "object",
|
|
338
|
+
"required": ["binding", "permissionScope", "scopeKind", "candidateHash", "outcome", "reason", "decisionSource"],
|
|
339
|
+
"properties": {
|
|
340
|
+
"binding": {
|
|
341
|
+
"type": "object",
|
|
342
|
+
"properties": {
|
|
343
|
+
"orgId": {"type": "string"},
|
|
344
|
+
"rootConversationId": {"type": "string"},
|
|
345
|
+
"conversationId": {"type": "string"},
|
|
346
|
+
"deviceId": {"type": "string"},
|
|
347
|
+
"accountId": {"type": "string"},
|
|
348
|
+
"runId": {"type": "string"},
|
|
349
|
+
"callId": {"type": "string"},
|
|
350
|
+
"toolKey": {"type": "string"},
|
|
351
|
+
"specHash": {"type": "string"},
|
|
352
|
+
"callInputHash": {"type": "string"},
|
|
353
|
+
"resolvedInputHash": {"type": "string"},
|
|
354
|
+
"profile": {"type": "string", "enum": ["request-approval", "approve-for-me", "full-access"]},
|
|
355
|
+
"profileRevision": {"type": "integer", "minimum": 0, "maximum": 9007199254740991},
|
|
356
|
+
"scopeRevision": {"type": "string"},
|
|
357
|
+
"authorityRevision": {"type": "string"}
|
|
358
|
+
},
|
|
359
|
+
"required": [
|
|
360
|
+
"orgId",
|
|
361
|
+
"rootConversationId",
|
|
362
|
+
"conversationId",
|
|
363
|
+
"deviceId",
|
|
364
|
+
"accountId",
|
|
365
|
+
"runId",
|
|
366
|
+
"callId",
|
|
367
|
+
"toolKey",
|
|
368
|
+
"specHash",
|
|
369
|
+
"callInputHash",
|
|
370
|
+
"resolvedInputHash",
|
|
371
|
+
"profile",
|
|
372
|
+
"profileRevision",
|
|
373
|
+
"scopeRevision",
|
|
374
|
+
"authorityRevision"
|
|
375
|
+
],
|
|
376
|
+
"additionalProperties": false
|
|
377
|
+
},
|
|
378
|
+
"permissionScope": {
|
|
379
|
+
"type": "object",
|
|
380
|
+
"properties": {
|
|
381
|
+
"writeDirectories": {"maxItems": 64, "type": "array", "items": {"type": "string", "maxLength": 4096}},
|
|
382
|
+
"hosts": {"maxItems": 100, "type": "array", "items": {"type": "string", "maxLength": 253}}
|
|
383
|
+
},
|
|
384
|
+
"required": ["writeDirectories", "hosts"],
|
|
385
|
+
"additionalProperties": false
|
|
386
|
+
},
|
|
387
|
+
"targetPath": {"type": "string", "maxLength": 4096},
|
|
388
|
+
"scopeKind": {"type": "string", "enum": ["directories-and-hosts", "sandbox-escalation", "loopback-port", "site-access"]},
|
|
389
|
+
"escalation": {
|
|
390
|
+
"type": "object",
|
|
391
|
+
"properties": {
|
|
392
|
+
"justification": {"type": "string", "minLength": 1, "check": "justification"},
|
|
393
|
+
"background": {"type": "boolean", "const": true},
|
|
394
|
+
"maxSeconds": {"type": "integer", "exclusiveMinimum": 0, "maximum": 9007199254740991}
|
|
395
|
+
},
|
|
396
|
+
"required": ["justification"],
|
|
397
|
+
"additionalProperties": false
|
|
398
|
+
},
|
|
399
|
+
"candidateHash": {"type": "string"},
|
|
400
|
+
"outcome": {"type": "string", "enum": ["allow", "ask", "deny"]},
|
|
401
|
+
"reason": {"type": "string"},
|
|
402
|
+
"decisionSource": {"type": "string", "enum": ["review-model", "rule", "full-access"]},
|
|
403
|
+
"crossing": {"type": "string", "enum": ["directory", "host", "outside-sandbox", "loopback-port", "site-access"]},
|
|
404
|
+
"sensitive": {"type": "array", "items": {"type": "string"}},
|
|
405
|
+
"site": {
|
|
406
|
+
"type": "object",
|
|
407
|
+
"properties": {
|
|
408
|
+
"service": {"type": "string"},
|
|
409
|
+
"action": {"type": "string", "enum": ["evaluate", "storage-read", "storage-clear", "permission-grant"]},
|
|
410
|
+
"origin": {"type": "string", "maxLength": 300},
|
|
411
|
+
"detailSha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
|
|
412
|
+
"attempt": {"type": "integer", "exclusiveMinimum": 0, "maximum": 9007199254740991}
|
|
413
|
+
},
|
|
414
|
+
"required": ["service", "action", "origin", "detailSha256", "attempt"]
|
|
415
|
+
},
|
|
416
|
+
"loopback": {
|
|
417
|
+
"type": "object",
|
|
418
|
+
"properties": {
|
|
419
|
+
"service": {"type": "string"},
|
|
420
|
+
"port": {"type": "integer", "minimum": 1, "maximum": 65535},
|
|
421
|
+
"readyRerunAttempt": {"type": "integer", "exclusiveMinimum": 0, "maximum": 9007199254740991}
|
|
422
|
+
},
|
|
423
|
+
"required": ["service", "port"]
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
$shape$::jsonb),
|
|
428
|
+
('approval_requested', $shape$
|
|
429
|
+
{
|
|
430
|
+
"type": "object",
|
|
431
|
+
"required": ["step", "callId", "approvalId", "toolKey", "expiresAt", "requestedFor", "approverRole"],
|
|
432
|
+
"properties": {
|
|
433
|
+
"step": {"type": "integer", "minimum": 0, "maximum": 9007199254740991},
|
|
434
|
+
"callId": {"type": "string"},
|
|
435
|
+
"approvalId": {"type": "string"},
|
|
436
|
+
"toolKey": {"type": "string"},
|
|
437
|
+
"expiresAt": {"type": "string"},
|
|
438
|
+
"policy": {"type": "string", "enum": ["agent", "connector", "member", "default", "role", "org", "fixed"]},
|
|
439
|
+
"network": {
|
|
440
|
+
"type": "object",
|
|
441
|
+
"properties": {
|
|
442
|
+
"hosts": {
|
|
443
|
+
"minItems": 1,
|
|
444
|
+
"maxItems": 20,
|
|
445
|
+
"type": "array",
|
|
446
|
+
"items": {"type": "string", "minLength": 1, "maxLength": 255}
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
"required": ["hosts"]
|
|
450
|
+
},
|
|
451
|
+
"git": {
|
|
452
|
+
"type": "object",
|
|
453
|
+
"properties": {
|
|
454
|
+
"host": {"type": "string", "minLength": 1, "maxLength": 255},
|
|
455
|
+
"repo": {"type": "string", "minLength": 1, "maxLength": 191},
|
|
456
|
+
"refs": {"maxItems": 20, "type": "array", "items": {"type": "string", "minLength": 1, "maxLength": 255}}
|
|
457
|
+
},
|
|
458
|
+
"required": ["host", "repo", "refs"]
|
|
459
|
+
},
|
|
460
|
+
"command": {
|
|
461
|
+
"type": "object",
|
|
462
|
+
"properties": {
|
|
463
|
+
"permissionScope": {
|
|
464
|
+
"type": "object",
|
|
465
|
+
"properties": {
|
|
466
|
+
"writeDirectories": {"maxItems": 64, "type": "array", "items": {"type": "string", "maxLength": 4096}},
|
|
467
|
+
"hosts": {"maxItems": 100, "type": "array", "items": {"type": "string", "maxLength": 253}}
|
|
468
|
+
},
|
|
469
|
+
"required": ["writeDirectories", "hosts"],
|
|
470
|
+
"additionalProperties": false
|
|
471
|
+
},
|
|
472
|
+
"targetPath": {"type": "string", "maxLength": 4096},
|
|
473
|
+
"candidateHash": {"type": "string"},
|
|
474
|
+
"escalation": {
|
|
475
|
+
"type": "object",
|
|
476
|
+
"properties": {
|
|
477
|
+
"justification": {"type": "string", "minLength": 1, "check": "justification"},
|
|
478
|
+
"background": {"type": "boolean", "const": true},
|
|
479
|
+
"maxSeconds": {"type": "integer", "exclusiveMinimum": 0, "maximum": 9007199254740991}
|
|
480
|
+
},
|
|
481
|
+
"required": ["justification"],
|
|
482
|
+
"additionalProperties": false
|
|
483
|
+
},
|
|
484
|
+
"workingRoot": {"type": "string", "maxLength": 4096},
|
|
485
|
+
"kind": {"type": "string", "enum": ["command", "write", "read"]},
|
|
486
|
+
"profile": {"type": "string", "enum": ["request-approval", "approve-for-me", "full-access"]},
|
|
487
|
+
"command": {"type": "string", "maxLength": 2000},
|
|
488
|
+
"path": {"type": "string", "maxLength": 4096},
|
|
489
|
+
"rule": {"anyOf": [{"type": "string", "maxLength": 200}, {"type": "null"}]},
|
|
490
|
+
"reason": {"type": "string", "maxLength": 2000},
|
|
491
|
+
"mode": {"type": "string", "enum": ["auto", "sandbox"]}
|
|
492
|
+
},
|
|
493
|
+
"required": ["kind", "command", "rule", "reason", "mode"],
|
|
494
|
+
"additionalProperties": false
|
|
495
|
+
},
|
|
496
|
+
"site": {
|
|
497
|
+
"type": "object",
|
|
498
|
+
"properties": {
|
|
499
|
+
"service": {"type": "string"},
|
|
500
|
+
"action": {"type": "string", "enum": ["evaluate", "storage-read", "storage-clear", "permission-grant"]},
|
|
501
|
+
"origin": {"type": "string", "maxLength": 300}
|
|
502
|
+
},
|
|
503
|
+
"required": ["service", "action", "origin"]
|
|
504
|
+
},
|
|
505
|
+
"loopback": {
|
|
506
|
+
"type": "object",
|
|
507
|
+
"properties": {
|
|
508
|
+
"service": {"type": "string"},
|
|
509
|
+
"port": {"type": "integer", "minimum": 1, "maximum": 65535},
|
|
510
|
+
"database": {"type": "boolean"}
|
|
511
|
+
},
|
|
512
|
+
"required": ["service", "port", "database"]
|
|
513
|
+
},
|
|
514
|
+
"secureInput": {
|
|
515
|
+
"type": "object",
|
|
516
|
+
"properties": {
|
|
517
|
+
"target": {
|
|
518
|
+
"type": "object",
|
|
519
|
+
"properties": {
|
|
520
|
+
"zh": {"type": "string", "minLength": 1, "maxLength": 200},
|
|
521
|
+
"en": {"type": "string", "minLength": 1, "maxLength": 200}
|
|
522
|
+
},
|
|
523
|
+
"required": ["zh", "en"]
|
|
524
|
+
},
|
|
525
|
+
"fields": {
|
|
526
|
+
"minItems": 1,
|
|
527
|
+
"maxItems": 32,
|
|
528
|
+
"type": "array",
|
|
529
|
+
"items": {"type": "string", "minLength": 1, "maxLength": 200}
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
"required": ["target", "fields"]
|
|
533
|
+
},
|
|
534
|
+
"requestedFor": {"type": ["string", "null"]},
|
|
535
|
+
"approverRole": {"anyOf": [{"type": "string", "enum": ["guest", "member", "admin", "owner"]}, {"type": "null"}]}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
$shape$::jsonb),
|
|
539
|
+
('permission_context', $shape$
|
|
540
|
+
{
|
|
541
|
+
"type": "object",
|
|
542
|
+
"required": [
|
|
543
|
+
"version",
|
|
544
|
+
"rootConversationId",
|
|
545
|
+
"profileRevision",
|
|
546
|
+
"selectedProfile",
|
|
547
|
+
"effectiveProfile",
|
|
548
|
+
"execution",
|
|
549
|
+
"inherited",
|
|
550
|
+
"deviceId",
|
|
551
|
+
"scopeRevision",
|
|
552
|
+
"authorityRevision",
|
|
553
|
+
"availability",
|
|
554
|
+
"scope",
|
|
555
|
+
"sandbox"
|
|
556
|
+
],
|
|
557
|
+
"additionalProperties": false,
|
|
558
|
+
"properties": {
|
|
559
|
+
"version": {"type": "number", "const": 2},
|
|
560
|
+
"rootConversationId": {
|
|
561
|
+
"type": "string",
|
|
562
|
+
"pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
|
|
563
|
+
},
|
|
564
|
+
"profileRevision": {"anyOf": [{"type": "integer", "minimum": 0, "maximum": 9007199254740991}, {"type": "null"}]},
|
|
565
|
+
"selectedProfile": {"anyOf": [{"enum": ["request-approval", "approve-for-me", "full-access"]}, {"type": "null"}]},
|
|
566
|
+
"effectiveProfile": {"anyOf": [{"enum": ["request-approval", "approve-for-me", "full-access"]}, {"type": "null"}]},
|
|
567
|
+
"execution": {"type": "string", "enum": ["interactive", "scheduled"]},
|
|
568
|
+
"inherited": {"type": "boolean"},
|
|
569
|
+
"deviceId": {
|
|
570
|
+
"anyOf": [
|
|
571
|
+
{
|
|
572
|
+
"type": "string",
|
|
573
|
+
"pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
|
|
574
|
+
},
|
|
575
|
+
{"type": "null"}
|
|
576
|
+
]
|
|
577
|
+
},
|
|
578
|
+
"scopeRevision": {"anyOf": [{"type": "string", "minLength": 1, "maxLength": 256}, {"type": "null"}]},
|
|
579
|
+
"authorityRevision": {"anyOf": [{"type": "string", "minLength": 1, "maxLength": 256}, {"type": "null"}]},
|
|
580
|
+
"availability": {
|
|
581
|
+
"oneOf": [
|
|
582
|
+
{
|
|
583
|
+
"type": "object",
|
|
584
|
+
"properties": {"state": {"type": "string", "const": "supported"}},
|
|
585
|
+
"required": ["state"],
|
|
586
|
+
"additionalProperties": false
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
"type": "object",
|
|
590
|
+
"properties": {
|
|
591
|
+
"state": {"type": "string", "enum": ["unsupported", "unprobed"]},
|
|
592
|
+
"reason": {"type": "string", "minLength": 1, "maxLength": 1000, "check": "reason"}
|
|
593
|
+
},
|
|
594
|
+
"required": ["state", "reason"],
|
|
595
|
+
"additionalProperties": false
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
"type": "object",
|
|
599
|
+
"properties": {
|
|
600
|
+
"state": {"type": "string", "const": "unavailable"},
|
|
601
|
+
"cause": {"type": "string", "enum": ["no-device", "actor-device-access"]},
|
|
602
|
+
"reason": {"type": "string", "minLength": 1, "maxLength": 1000, "check": "reason"}
|
|
603
|
+
},
|
|
604
|
+
"required": ["state", "cause", "reason"],
|
|
605
|
+
"additionalProperties": false
|
|
606
|
+
}
|
|
607
|
+
]
|
|
608
|
+
},
|
|
609
|
+
"scope": {
|
|
610
|
+
"anyOf": [
|
|
611
|
+
{
|
|
612
|
+
"type": "object",
|
|
613
|
+
"properties": {
|
|
614
|
+
"boundary": {"type": "string", "enum": ["operation", "instance"]},
|
|
615
|
+
"workingRoot": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"},
|
|
616
|
+
"filesystem": {
|
|
617
|
+
"oneOf": [
|
|
618
|
+
{
|
|
619
|
+
"type": "object",
|
|
620
|
+
"properties": {"kind": {"type": "string", "const": "device"}},
|
|
621
|
+
"required": ["kind"],
|
|
622
|
+
"additionalProperties": false
|
|
623
|
+
},
|
|
624
|
+
{
|
|
625
|
+
"type": "object",
|
|
626
|
+
"properties": {
|
|
627
|
+
"kind": {"type": "string", "const": "scoped"},
|
|
628
|
+
"writeDirectories": {
|
|
629
|
+
"minItems": 1,
|
|
630
|
+
"maxItems": 64,
|
|
631
|
+
"type": "array",
|
|
632
|
+
"items": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"},
|
|
633
|
+
"check": "sorted"
|
|
634
|
+
}
|
|
635
|
+
},
|
|
636
|
+
"required": ["kind", "writeDirectories"],
|
|
637
|
+
"additionalProperties": false
|
|
638
|
+
}
|
|
639
|
+
]
|
|
640
|
+
},
|
|
641
|
+
"network": {
|
|
642
|
+
"oneOf": [
|
|
643
|
+
{
|
|
644
|
+
"type": "object",
|
|
645
|
+
"properties": {"kind": {"type": "string", "const": "none"}},
|
|
646
|
+
"required": ["kind"],
|
|
647
|
+
"additionalProperties": false
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
"type": "object",
|
|
651
|
+
"properties": {"kind": {"type": "string", "const": "environment"}},
|
|
652
|
+
"required": ["kind"],
|
|
653
|
+
"additionalProperties": false
|
|
654
|
+
},
|
|
655
|
+
{
|
|
656
|
+
"type": "object",
|
|
657
|
+
"properties": {
|
|
658
|
+
"kind": {"type": "string", "const": "allowlist"},
|
|
659
|
+
"hosts": {
|
|
660
|
+
"minItems": 1,
|
|
661
|
+
"maxItems": 100,
|
|
662
|
+
"type": "array",
|
|
663
|
+
"items": {"type": "string", "maxLength": 253, "check": "host"},
|
|
664
|
+
"check": "sorted"
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
"required": ["kind", "hosts"],
|
|
668
|
+
"additionalProperties": false
|
|
669
|
+
},
|
|
670
|
+
{
|
|
671
|
+
"type": "object",
|
|
672
|
+
"properties": {"kind": {"type": "string", "const": "open-public"}},
|
|
673
|
+
"required": ["kind"],
|
|
674
|
+
"additionalProperties": false
|
|
675
|
+
}
|
|
676
|
+
]
|
|
677
|
+
},
|
|
678
|
+
"outerNetwork": {
|
|
679
|
+
"oneOf": [
|
|
680
|
+
{
|
|
681
|
+
"type": "object",
|
|
682
|
+
"properties": {"kind": {"type": "string", "const": "host-environment"}},
|
|
683
|
+
"required": ["kind"],
|
|
684
|
+
"additionalProperties": false
|
|
685
|
+
},
|
|
686
|
+
{
|
|
687
|
+
"type": "object",
|
|
688
|
+
"properties": {"kind": {"type": "string", "const": "none"}},
|
|
689
|
+
"required": ["kind"],
|
|
690
|
+
"additionalProperties": false
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
"type": "object",
|
|
694
|
+
"properties": {"kind": {"type": "string", "const": "open-public"}},
|
|
695
|
+
"required": ["kind"],
|
|
696
|
+
"additionalProperties": false
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
"type": "object",
|
|
700
|
+
"properties": {
|
|
701
|
+
"kind": {"type": "string", "const": "allowlist"},
|
|
702
|
+
"hosts": {
|
|
703
|
+
"minItems": 1,
|
|
704
|
+
"maxItems": 100,
|
|
705
|
+
"type": "array",
|
|
706
|
+
"items": {"type": "string", "maxLength": 253, "check": "host"},
|
|
707
|
+
"check": "sorted"
|
|
708
|
+
}
|
|
709
|
+
},
|
|
710
|
+
"required": ["kind", "hosts"],
|
|
711
|
+
"additionalProperties": false
|
|
712
|
+
}
|
|
713
|
+
]
|
|
714
|
+
}
|
|
715
|
+
},
|
|
716
|
+
"required": ["boundary", "workingRoot", "filesystem", "network", "outerNetwork"],
|
|
717
|
+
"additionalProperties": false
|
|
718
|
+
},
|
|
719
|
+
{"type": "null"}
|
|
720
|
+
]
|
|
721
|
+
},
|
|
722
|
+
"sandbox": {
|
|
723
|
+
"anyOf": [
|
|
724
|
+
{
|
|
725
|
+
"type": "object",
|
|
726
|
+
"properties": {
|
|
727
|
+
"boundary": {"type": "string", "enum": ["operation", "instance"]},
|
|
728
|
+
"workingRoot": {"type": "string", "minLength": 1, "maxLength": 4096},
|
|
729
|
+
"network": {
|
|
730
|
+
"type": "object",
|
|
731
|
+
"properties": {
|
|
732
|
+
"kind": {"type": "string", "enum": ["none", "allowlist", "open-public"]},
|
|
733
|
+
"hostsCount": {"type": "integer", "minimum": 0, "maximum": 1000}
|
|
734
|
+
},
|
|
735
|
+
"required": ["kind", "hostsCount"],
|
|
736
|
+
"additionalProperties": false
|
|
737
|
+
},
|
|
738
|
+
"outsideSandbox": {"type": "boolean"},
|
|
739
|
+
"outerNetwork": {
|
|
740
|
+
"oneOf": [
|
|
741
|
+
{
|
|
742
|
+
"type": "object",
|
|
743
|
+
"properties": {"kind": {"type": "string", "const": "host-environment"}},
|
|
744
|
+
"required": ["kind"],
|
|
745
|
+
"additionalProperties": false
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
"type": "object",
|
|
749
|
+
"properties": {"kind": {"type": "string", "const": "none"}},
|
|
750
|
+
"required": ["kind"],
|
|
751
|
+
"additionalProperties": false
|
|
752
|
+
},
|
|
753
|
+
{
|
|
754
|
+
"type": "object",
|
|
755
|
+
"properties": {"kind": {"type": "string", "const": "open-public"}},
|
|
756
|
+
"required": ["kind"],
|
|
757
|
+
"additionalProperties": false
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
"type": "object",
|
|
761
|
+
"properties": {
|
|
762
|
+
"kind": {"type": "string", "const": "allowlist"},
|
|
763
|
+
"hosts": {
|
|
764
|
+
"minItems": 1,
|
|
765
|
+
"maxItems": 100,
|
|
766
|
+
"type": "array",
|
|
767
|
+
"items": {"type": "string", "maxLength": 253, "check": "host"},
|
|
768
|
+
"check": "sorted"
|
|
769
|
+
}
|
|
770
|
+
},
|
|
771
|
+
"required": ["kind", "hosts"],
|
|
772
|
+
"additionalProperties": false
|
|
773
|
+
}
|
|
774
|
+
]
|
|
775
|
+
},
|
|
776
|
+
"perCallHosts": {"type": "boolean"}
|
|
777
|
+
},
|
|
778
|
+
"required": ["boundary", "workingRoot", "network", "outsideSandbox", "outerNetwork"],
|
|
779
|
+
"additionalProperties": false
|
|
780
|
+
},
|
|
781
|
+
{"type": "null"}
|
|
782
|
+
]
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
$shape$::jsonb),
|
|
787
|
+
('device_paths', $shape$
|
|
788
|
+
{
|
|
789
|
+
"type": "object",
|
|
790
|
+
"required": ["permissionContextVersion", "deviceId", "roots", "workingRoot"],
|
|
791
|
+
"additionalProperties": false,
|
|
792
|
+
"properties": {
|
|
793
|
+
"permissionContextVersion": {"type": "number", "const": 1},
|
|
794
|
+
"deviceId": {
|
|
795
|
+
"type": "string",
|
|
796
|
+
"pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
|
|
797
|
+
},
|
|
798
|
+
"roots": {
|
|
799
|
+
"type": "object",
|
|
800
|
+
"properties": {
|
|
801
|
+
"workspace": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"},
|
|
802
|
+
"skills": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"},
|
|
803
|
+
"tmp": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"}
|
|
804
|
+
},
|
|
805
|
+
"required": ["workspace", "skills", "tmp"],
|
|
806
|
+
"additionalProperties": false
|
|
807
|
+
},
|
|
808
|
+
"workingRoot": {"type": "string", "minLength": 1, "maxLength": 4096, "check": "path"}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
$shape$::jsonb);
|
|
812
|
+
|
|
813
|
+
-- Hold the selection and the rows checked before deletion stable through the cutover.
|
|
814
|
+
LOCK TABLE conversations, conversation_events, runs, approvals, permission_call_grants,
|
|
815
|
+
sandbox_operation_intents, device_conversation_notices IN SHARE ROW EXCLUSIVE MODE;
|
|
816
|
+
|
|
817
|
+
CREATE TEMP TABLE IF NOT EXISTS history_missing_depths ON COMMIT DROP AS
|
|
818
|
+
SELECT c.id, coalesce(
|
|
819
|
+
p.kind IS DISTINCT FROM 'subagent' AND p.id IS NOT NULL
|
|
820
|
+
AND p.parent_conversation_id IS NULL AND p.subagent_depth = 0,
|
|
821
|
+
false
|
|
822
|
+
) AS convertible
|
|
823
|
+
FROM conversations c
|
|
824
|
+
LEFT JOIN conversations p ON p.id = c.parent_conversation_id AND p.org_id = c.org_id
|
|
825
|
+
WHERE c.kind = 'subagent' AND c.subagent_depth = 0;
|
|
826
|
+
|
|
827
|
+
CREATE TEMP TABLE IF NOT EXISTS history_checked_events ON COMMIT DROP AS
|
|
828
|
+
SELECT e.conversation_id, e.seq, e.type, e.payload AS original,
|
|
829
|
+
pg_temp.history_normalized(e.type, e.payload) AS normalized, s.shape
|
|
830
|
+
FROM conversation_events e JOIN history_event_shapes s ON s.type = e.type;
|
|
831
|
+
|
|
832
|
+
CREATE TEMP TABLE IF NOT EXISTS history_delete_conversations (id text PRIMARY KEY) ON COMMIT DROP;
|
|
833
|
+
INSERT INTO history_delete_conversations
|
|
834
|
+
WITH RECURSIVE direct AS (
|
|
835
|
+
SELECT conversation_id AS id FROM history_checked_events
|
|
836
|
+
WHERE pg_temp.history_event_incompatible(type, original, normalized, shape)
|
|
837
|
+
UNION
|
|
838
|
+
SELECT id FROM history_missing_depths WHERE NOT convertible
|
|
839
|
+
), tree AS (
|
|
840
|
+
SELECT id FROM direct
|
|
841
|
+
UNION
|
|
842
|
+
SELECT c.id FROM conversations c JOIN tree t ON c.parent_conversation_id = t.id
|
|
843
|
+
)
|
|
844
|
+
SELECT id FROM tree;
|
|
845
|
+
|
|
846
|
+
CREATE TEMP TABLE IF NOT EXISTS history_delete_runs (id text PRIMARY KEY) ON COMMIT DROP;
|
|
847
|
+
INSERT INTO history_delete_runs
|
|
848
|
+
SELECT r.id FROM runs r JOIN history_delete_conversations c ON c.id = r.conversation_id;
|
|
849
|
+
|
|
850
|
+
CREATE TEMP TABLE IF NOT EXISTS history_delete_approvals (id text PRIMARY KEY) ON COMMIT DROP;
|
|
851
|
+
INSERT INTO history_delete_approvals
|
|
852
|
+
SELECT a.id FROM approvals a
|
|
853
|
+
WHERE a.conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
854
|
+
OR a.run_id IN (SELECT id FROM history_delete_runs);
|
|
855
|
+
|
|
856
|
+
CREATE TEMP TABLE IF NOT EXISTS history_delete_grants (id uuid PRIMARY KEY) ON COMMIT DROP;
|
|
857
|
+
INSERT INTO history_delete_grants
|
|
858
|
+
SELECT g.id FROM permission_call_grants g
|
|
859
|
+
WHERE g.root_conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
860
|
+
OR g.conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
861
|
+
OR g.run_id IN (SELECT id FROM history_delete_runs)
|
|
862
|
+
OR g.approval_id IN (SELECT id FROM history_delete_approvals);
|
|
863
|
+
|
|
864
|
+
CREATE TEMP TABLE IF NOT EXISTS history_delete_operations ON COMMIT DROP AS
|
|
865
|
+
SELECT operation_id, state, revoke_acknowledged FROM sandbox_operation_intents
|
|
866
|
+
WHERE root_conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
867
|
+
OR conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
868
|
+
OR run_id IN (SELECT id FROM history_delete_runs)
|
|
869
|
+
OR permission_grant_id IN (SELECT id FROM history_delete_grants);
|
|
870
|
+
|
|
871
|
+
-- capybara:allow-destructive Guard every deletion and rewrite below with exact verified export coverage and settled device work.
|
|
872
|
+
DO $$
|
|
873
|
+
DECLARE
|
|
874
|
+
exported text[];
|
|
875
|
+
BEGIN
|
|
876
|
+
IF to_regclass('capybara._history_export_0210') IS NOT NULL THEN
|
|
877
|
+
SELECT conversation_ids INTO exported FROM _history_export_0210 FOR SHARE;
|
|
878
|
+
END IF;
|
|
879
|
+
|
|
880
|
+
IF exported IS NULL THEN
|
|
881
|
+
IF EXISTS (SELECT FROM history_delete_conversations) THEN
|
|
882
|
+
RAISE EXCEPTION '0210 history export receipt is missing. Follow the conversation history cutover in docs/deploy/upgrade.md.';
|
|
883
|
+
END IF;
|
|
884
|
+
ELSIF EXISTS (
|
|
885
|
+
(SELECT id FROM history_delete_conversations EXCEPT SELECT unnest(exported))
|
|
886
|
+
UNION ALL
|
|
887
|
+
(SELECT unnest(exported) EXCEPT SELECT id FROM history_delete_conversations)
|
|
888
|
+
) THEN
|
|
889
|
+
RAISE EXCEPTION '0210 history export does not match the SQL deletion set. Follow docs/deploy/upgrade.md.';
|
|
890
|
+
END IF;
|
|
891
|
+
|
|
892
|
+
IF EXISTS (
|
|
893
|
+
SELECT FROM history_delete_operations
|
|
894
|
+
WHERE state <> 'completed' AND NOT (state = 'revoked' AND revoke_acknowledged)
|
|
895
|
+
) THEN
|
|
896
|
+
RAISE EXCEPTION '0210 has unsettled sandbox operations for histories selected for deletion.';
|
|
897
|
+
END IF;
|
|
898
|
+
|
|
899
|
+
IF EXISTS (
|
|
900
|
+
SELECT FROM device_conversation_notices
|
|
901
|
+
WHERE conversation_id IN (SELECT id FROM history_delete_conversations)
|
|
902
|
+
) THEN
|
|
903
|
+
RAISE EXCEPTION '0210 has pending device notices for histories selected for deletion.';
|
|
904
|
+
END IF;
|
|
905
|
+
END
|
|
906
|
+
$$;
|
|
907
|
+
|
|
908
|
+
-- capybara:allow-destructive Retire settled operations for the deleted history and release grant FKs. Payload chunks cascade.
|
|
909
|
+
DELETE FROM sandbox_operation_intents WHERE operation_id IN (SELECT operation_id FROM history_delete_operations);
|
|
910
|
+
|
|
911
|
+
-- capybara:allow-destructive Call grants belong to the deleted history and otherwise block conversation, run and approval cascades.
|
|
912
|
+
DELETE FROM permission_call_grants WHERE id IN (SELECT id FROM history_delete_grants);
|
|
913
|
+
|
|
914
|
+
-- capybara:allow-destructive Remove inbox rows whose in-app link points directly to a deleted conversation in that org.
|
|
915
|
+
DELETE FROM inbox_items i USING conversations c, history_delete_conversations d
|
|
916
|
+
WHERE c.id = d.id AND i.org_id = c.org_id
|
|
917
|
+
AND split_part(split_part(i.link, '#', 1), '?', 1) = '/chat/' || c.id;
|
|
918
|
+
|
|
919
|
+
-- capybara:allow-destructive Remove the exact subject-to-conversation bindings of deleted histories; retain automations and subjects.
|
|
920
|
+
DELETE FROM automation_conversations WHERE conversation_id IN (SELECT id FROM history_delete_conversations);
|
|
921
|
+
|
|
922
|
+
-- capybara:allow-destructive Retired run causes must not authorize a fresh trigger after their owning run is removed.
|
|
923
|
+
DELETE FROM trigger_cause_handles WHERE run_id IN (SELECT id FROM history_delete_runs);
|
|
924
|
+
|
|
925
|
+
-- Eval results are independent records; clear their optional live transcript link as the eval
|
|
926
|
+
-- retention worker does. Automation run history and trigger/audit provenance are retained.
|
|
927
|
+
UPDATE eval_results SET eval_conversation_id = NULL
|
|
928
|
+
WHERE eval_conversation_id IN (SELECT id FROM history_delete_conversations);
|
|
929
|
+
|
|
930
|
+
-- capybara:allow-destructive Remove only the approved incompatible conversations and their descendants with FK-owned metadata.
|
|
931
|
+
DELETE FROM conversations WHERE id IN (SELECT id FROM history_delete_conversations);
|
|
932
|
+
|
|
933
|
+
-- Rebase only a digest that exactly authenticates the old sandbox object at the call's
|
|
934
|
+
-- effective context locator. This preserves genuine mismatches and all other request metadata.
|
|
935
|
+
WITH digests AS MATERIALIZED (
|
|
936
|
+
SELECT h.conversation_id, h.seq,
|
|
937
|
+
pg_temp.history_sandbox_digest(h.original->'sandbox') AS original,
|
|
938
|
+
pg_temp.history_sandbox_digest(h.normalized->'sandbox') AS normalized
|
|
939
|
+
FROM history_checked_events h
|
|
940
|
+
WHERE h.type = 'permission_context' AND (h.original->'sandbox') ? 'location'
|
|
941
|
+
AND NOT EXISTS (SELECT FROM history_delete_conversations d WHERE d.id = h.conversation_id)
|
|
942
|
+
)
|
|
943
|
+
UPDATE conversation_events e SET payload = jsonb_set(
|
|
944
|
+
e.payload, '{request,permissionContext,sandbox}', to_jsonb(d.normalized), false
|
|
945
|
+
)
|
|
946
|
+
FROM digests d
|
|
947
|
+
WHERE e.conversation_id = d.conversation_id AND e.type = 'model_call'
|
|
948
|
+
AND d.original IS NOT NULL AND d.normalized IS NOT NULL AND d.original <> d.normalized
|
|
949
|
+
AND e.seq > d.seq
|
|
950
|
+
AND pg_temp.history_number(e.payload#>'{request,toSeq}') >= d.seq
|
|
951
|
+
AND pg_temp.history_old_sandbox_matches(e.payload, d.seq::double precision, d.original)
|
|
952
|
+
AND NOT EXISTS (
|
|
953
|
+
SELECT FROM conversation_events later
|
|
954
|
+
WHERE later.conversation_id = e.conversation_id AND later.type = 'permission_context'
|
|
955
|
+
AND later.seq > d.seq AND later.seq < e.seq
|
|
956
|
+
AND later.seq <= pg_temp.history_number(e.payload#>'{request,toSeq}')
|
|
957
|
+
);
|
|
958
|
+
|
|
959
|
+
UPDATE conversation_events e SET payload = h.normalized
|
|
960
|
+
FROM history_checked_events h
|
|
961
|
+
WHERE e.conversation_id = h.conversation_id AND e.seq = h.seq
|
|
962
|
+
AND h.original IS DISTINCT FROM h.normalized;
|
|
963
|
+
|
|
964
|
+
-- Saved cases keep their own history, even when their source has disappeared. Materialize the
|
|
965
|
+
-- same known conversions there. Cases containing incompatible events keep the original snapshot.
|
|
966
|
+
CREATE OR REPLACE FUNCTION pg_temp.history_normalized_case_events(events jsonb)
|
|
967
|
+
RETURNS jsonb LANGUAGE sql STABLE STRICT AS $$
|
|
968
|
+
WITH items AS MATERIALIZED (
|
|
969
|
+
SELECT e, ordinal, e->>'type' AS type, e->'payload' AS payload,
|
|
970
|
+
pg_temp.history_number(e->'seq') AS seq,
|
|
971
|
+
pg_temp.history_normalized(e->>'type', e->'payload') AS normalized
|
|
972
|
+
FROM jsonb_array_elements(events) WITH ORDINALITY AS entries(e, ordinal)
|
|
973
|
+
), digests AS MATERIALIZED (
|
|
974
|
+
SELECT seq, ordinal, (payload->'sandbox') ? 'location' AS changed,
|
|
975
|
+
CASE WHEN (payload->'sandbox') ? 'location'
|
|
976
|
+
THEN pg_temp.history_sandbox_digest(payload->'sandbox') END AS original,
|
|
977
|
+
CASE WHEN (payload->'sandbox') ? 'location'
|
|
978
|
+
THEN pg_temp.history_sandbox_digest(normalized->'sandbox') END AS replacement
|
|
979
|
+
FROM items WHERE type = 'permission_context'
|
|
980
|
+
)
|
|
981
|
+
SELECT CASE WHEN EXISTS (
|
|
982
|
+
SELECT FROM items i JOIN pg_temp.history_event_shapes s ON s.type = i.type
|
|
983
|
+
WHERE pg_temp.history_event_incompatible(i.type, i.payload, i.normalized, s.shape)
|
|
984
|
+
) THEN events ELSE coalesce((
|
|
985
|
+
SELECT jsonb_agg(CASE
|
|
986
|
+
WHEN i.type = 'model_call' AND d.changed AND d.replacement IS NOT NULL
|
|
987
|
+
AND pg_temp.history_old_sandbox_matches(i.payload, d.seq, d.original)
|
|
988
|
+
THEN jsonb_set(i.e, '{payload,request,permissionContext,sandbox}', to_jsonb(d.replacement), false)
|
|
989
|
+
WHEN i.normalized IS NOT NULL AND i.normalized IS DISTINCT FROM i.payload
|
|
990
|
+
THEN jsonb_set(i.e, '{payload}', i.normalized, false)
|
|
991
|
+
ELSE i.e END ORDER BY i.ordinal)
|
|
992
|
+
FROM items i
|
|
993
|
+
LEFT JOIN LATERAL (
|
|
994
|
+
SELECT * FROM digests d
|
|
995
|
+
WHERE i.type = 'model_call' AND d.seq < i.seq
|
|
996
|
+
AND d.seq <= pg_temp.history_number(i.payload#>'{request,toSeq}')
|
|
997
|
+
ORDER BY d.seq DESC, d.ordinal LIMIT 1
|
|
998
|
+
) d ON true
|
|
999
|
+
), '[]'::jsonb) END;
|
|
1000
|
+
$$;
|
|
1001
|
+
|
|
1002
|
+
WITH cases AS MATERIALIZED (
|
|
1003
|
+
SELECT id, pg_temp.history_normalized_case_events(snapshot->'events') AS events
|
|
1004
|
+
FROM eval_cases WHERE jsonb_typeof(snapshot->'events') = 'array'
|
|
1005
|
+
)
|
|
1006
|
+
UPDATE eval_cases c SET snapshot = jsonb_set(c.snapshot, '{events}', cases.events, false)
|
|
1007
|
+
FROM cases WHERE c.id = cases.id AND c.snapshot->'events' IS DISTINCT FROM cases.events;
|
|
1008
|
+
|
|
1009
|
+
UPDATE runs SET compact_origin = 'manual' WHERE compact AND compact_origin IS NULL;
|
|
1010
|
+
|
|
1011
|
+
UPDATE conversations c SET subagent_depth = 1
|
|
1012
|
+
FROM history_missing_depths d WHERE c.id = d.id AND d.convertible;
|