@makaio/storage-pg 1.0.0-dev-1795338532209 → 1.0.0-dev-1795446229073

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.
@@ -0,0 +1,157 @@
1
+ CREATE FUNCTION pg_temp.workflow_step_names_rewrite(node jsonb, definition_dispatched boolean) RETURNS jsonb LANGUAGE plpgsql AS $$
2
+ DECLARE
3
+ result jsonb := node;
4
+ node_type text;
5
+ BEGIN
6
+ IF jsonb_typeof(node) IS DISTINCT FROM 'object' THEN
7
+ RETURN node;
8
+ END IF;
9
+ node_type := node ->> 'type';
10
+ IF node_type = 'delegate-role' THEN
11
+ result := jsonb_set(result, '{type}', '"agent-step"');
12
+ ELSIF node_type = 'station' THEN
13
+ IF jsonb_typeof(node -> 'role') IS DISTINCT FROM 'string' THEN
14
+ result := jsonb_set(result, '{type}', '"code-step"') - 'prompt' - 'completion';
15
+ ELSIF definition_dispatched THEN
16
+ result := jsonb_set(result, '{type}', '"agent-step"');
17
+ END IF;
18
+ END IF;
19
+ IF node_type = 'sequence' AND jsonb_typeof(node -> 'nodes') = 'array' THEN
20
+ result := jsonb_set(result, '{nodes}', (
21
+ SELECT COALESCE(jsonb_agg(pg_temp.workflow_step_names_rewrite(e.value, definition_dispatched) ORDER BY e.ordinality), '[]'::jsonb)
22
+ FROM jsonb_array_elements(node -> 'nodes') WITH ORDINALITY AS e(value, ordinality)
23
+ ));
24
+ ELSIF node_type = 'parallel' AND jsonb_typeof(node -> 'branches') = 'object' THEN
25
+ result := jsonb_set(result, '{branches}', (
26
+ SELECT COALESCE(jsonb_object_agg(b.key, pg_temp.workflow_step_names_rewrite(b.value, definition_dispatched)), '{}'::jsonb)
27
+ FROM jsonb_each(node -> 'branches') AS b
28
+ ));
29
+ ELSIF node_type IN ('iterate', 'iterate-chain', 'loop') AND jsonb_typeof(node -> 'body') = 'object' THEN
30
+ result := jsonb_set(result, '{body}', pg_temp.workflow_step_names_rewrite(node -> 'body', definition_dispatched));
31
+ END IF;
32
+ RETURN result;
33
+ END
34
+ $$;
35
+ --> statement-breakpoint
36
+ -- A guarded cast. `instruction::jsonb` throws for ANY row whose text contains an escape
37
+ -- PostgreSQL's jsonb input function cannot decode (for example `\u0000`, which cannot be
38
+ -- represented in `text`); that throw aborts the whole statement while it scans every row for the 'workflow'
39
+ -- kind, not just the rows it would rewrite. Foreign, non-'workflow' rows must stay fully untouched instead.
40
+ CREATE FUNCTION pg_temp.workflow_step_names_try_jsonb(value text) RETURNS jsonb LANGUAGE plpgsql AS $$
41
+ BEGIN
42
+ RETURN value::jsonb;
43
+ EXCEPTION WHEN OTHERS THEN
44
+ RETURN NULL;
45
+ END
46
+ $$;
47
+ --> statement-breakpoint
48
+ UPDATE workflow_definitions
49
+ SET root = pg_temp.workflow_step_names_rewrite(root, executable_source IS NULL OR executable_source ->> 'kind' = 'definition')
50
+ WHERE jsonb_typeof(root) = 'object'
51
+ AND root IS DISTINCT FROM pg_temp.workflow_step_names_rewrite(
52
+ root, executable_source IS NULL OR executable_source ->> 'kind' = 'definition');
53
+ --> statement-breakpoint
54
+ UPDATE workflow_run_contexts
55
+ SET definition_snapshot = jsonb_set(
56
+ definition_snapshot, '{root}',
57
+ pg_temp.workflow_step_names_rewrite(definition_snapshot -> 'root', source_kind = 'definition'))
58
+ WHERE jsonb_typeof(definition_snapshot -> 'root') = 'object'
59
+ AND definition_snapshot -> 'root' IS DISTINCT FROM
60
+ pg_temp.workflow_step_names_rewrite(definition_snapshot -> 'root', source_kind = 'definition');
61
+ --> statement-breakpoint
62
+ UPDATE execution_attempt
63
+ SET instruction = jsonb_set(
64
+ pg_temp.workflow_step_names_try_jsonb(instruction), '{workload,input,definitionSnapshot,root}',
65
+ pg_temp.workflow_step_names_rewrite(
66
+ pg_temp.workflow_step_names_try_jsonb(instruction) #> '{workload,input,definitionSnapshot,root}',
67
+ (pg_temp.workflow_step_names_try_jsonb(instruction) #>> '{workload,input,source,kind}') = 'definition'))::text
68
+ WHERE pg_temp.workflow_step_names_try_jsonb(instruction) IS NOT NULL
69
+ AND pg_temp.workflow_step_names_try_jsonb(instruction) #>> '{workload,kind}' = 'workflow'
70
+ AND jsonb_typeof(pg_temp.workflow_step_names_try_jsonb(instruction) #> '{workload,input,definitionSnapshot,root}') = 'object'
71
+ AND pg_temp.workflow_step_names_try_jsonb(instruction) #> '{workload,input,definitionSnapshot,root}' IS DISTINCT FROM
72
+ pg_temp.workflow_step_names_rewrite(
73
+ pg_temp.workflow_step_names_try_jsonb(instruction) #> '{workload,input,definitionSnapshot,root}',
74
+ (pg_temp.workflow_step_names_try_jsonb(instruction) #>> '{workload,input,source,kind}') = 'definition');
75
+ --> statement-breakpoint
76
+ CREATE TEMP TABLE workflow_step_names_kinds (
77
+ execution_id text NOT NULL,
78
+ node_id text NOT NULL,
79
+ step_type text NOT NULL,
80
+ PRIMARY KEY (execution_id, node_id)
81
+ ) ON COMMIT DROP;
82
+ --> statement-breakpoint
83
+ WITH RECURSIVE
84
+ roots (execution_id, root) AS (
85
+ SELECT r.execution_id, r.definition_snapshot -> 'root'
86
+ FROM workflow_run_contexts AS r
87
+ WHERE jsonb_typeof(r.definition_snapshot -> 'root') = 'object'
88
+ ),
89
+ walk (execution_id, node) AS (
90
+ SELECT execution_id, root FROM roots
91
+ UNION ALL
92
+ SELECT w.execution_id, c.value
93
+ FROM walk AS w
94
+ CROSS JOIN LATERAL (
95
+ SELECT e.value
96
+ FROM jsonb_array_elements(
97
+ CASE WHEN w.node ->> 'type' = 'sequence' AND jsonb_typeof(w.node -> 'nodes') = 'array'
98
+ THEN w.node -> 'nodes' ELSE '[]'::jsonb END) AS e(value)
99
+ UNION ALL
100
+ SELECT b.value
101
+ FROM jsonb_each(
102
+ CASE WHEN w.node ->> 'type' = 'parallel' AND jsonb_typeof(w.node -> 'branches') = 'object'
103
+ THEN w.node -> 'branches' ELSE '{}'::jsonb END) AS b
104
+ UNION ALL
105
+ SELECT w.node -> 'body'
106
+ WHERE w.node ->> 'type' IN ('iterate', 'iterate-chain', 'loop') AND jsonb_typeof(w.node -> 'body') = 'object'
107
+ ) AS c(value)
108
+ WHERE jsonb_typeof(c.value) = 'object'
109
+ )
110
+ INSERT INTO workflow_step_names_kinds (execution_id, node_id, step_type)
111
+ SELECT execution_id, node ->> 'id', MIN(node ->> 'type')
112
+ FROM walk
113
+ WHERE node ->> 'id' IS NOT NULL
114
+ GROUP BY execution_id, node ->> 'id'
115
+ HAVING MIN(node ->> 'type') = MAX(node ->> 'type') AND MIN(node ->> 'type') IN ('code-step', 'agent-step');
116
+ --> statement-breakpoint
117
+ UPDATE workflow_execution_frames AS f
118
+ SET node_type = k.step_type
119
+ FROM workflow_step_names_kinds AS k
120
+ WHERE f.node_type = 'station' AND k.execution_id = f.execution_id AND k.node_id = f.node_id;
121
+ --> statement-breakpoint
122
+ UPDATE workflow_execution_frames SET node_type = 'agent-step' WHERE node_type IN ('delegate-role', 'delegate-agent');
123
+ --> statement-breakpoint
124
+ UPDATE workflow_execution_frames SET node_type = 'code-step' WHERE node_type = 'station' AND substr(execution_id, 1, 8) = 'wfx-ext-';
125
+ --> statement-breakpoint
126
+ UPDATE workflow_step_spans AS s
127
+ SET step_type = k.step_type
128
+ FROM workflow_step_names_kinds AS k
129
+ WHERE s.step_type = 'station' AND k.execution_id = s.execution_id AND k.node_id = s.step_id;
130
+ --> statement-breakpoint
131
+ UPDATE workflow_step_spans SET step_type = 'agent-step' WHERE step_type IN ('delegate-role', 'delegate-agent');
132
+ --> statement-breakpoint
133
+ UPDATE workflow_step_spans SET step_type = 'code-step' WHERE step_type = 'station' AND substr(execution_id, 1, 8) = 'wfx-ext-';
134
+ --> statement-breakpoint
135
+ UPDATE worklog_frame_entries AS w
136
+ SET node_type = k.step_type
137
+ FROM workflow_step_names_kinds AS k
138
+ WHERE w.node_type = 'station' AND k.execution_id = w.execution_id AND k.node_id = w.node_id;
139
+ --> statement-breakpoint
140
+ UPDATE worklog_frame_entries SET node_type = 'agent-step' WHERE node_type IN ('delegate-role', 'delegate-agent');
141
+ --> statement-breakpoint
142
+ UPDATE worklog_frame_entries SET node_type = 'code-step' WHERE node_type = 'station' AND substr(execution_id, 1, 8) = 'wfx-ext-';
143
+ --> statement-breakpoint
144
+ UPDATE workflow_executions
145
+ SET external_settlement_fingerprint = replace(
146
+ replace(
147
+ replace(external_settlement_fingerprint, '"nodeType":"station"', '"nodeType":"code-step"'),
148
+ '"nodeType":"delegate-role"', '"nodeType":"agent-step"'),
149
+ '"nodeType":"delegate-agent"', '"nodeType":"agent-step"')
150
+ WHERE substr(id, 1, 8) = 'wfx-ext-'
151
+ AND (external_settlement_fingerprint LIKE '%"nodeType":"station"%'
152
+ OR external_settlement_fingerprint LIKE '%"nodeType":"delegate-role"%'
153
+ OR external_settlement_fingerprint LIKE '%"nodeType":"delegate-agent"%');
154
+ --> statement-breakpoint
155
+ DROP FUNCTION pg_temp.workflow_step_names_rewrite(jsonb, boolean);
156
+ --> statement-breakpoint
157
+ DROP FUNCTION pg_temp.workflow_step_names_try_jsonb(text);
@@ -0,0 +1,259 @@
1
+ -- Waiting removal, data part (paired with migrations/drizzle/0045_waiting-removal-data.sql).
2
+ -- Retain historical rows, gate tables and legacy columns. Fail waiting executions and live
3
+ -- executions with incompatible definition snapshots, then settle their frames, spans and summaries.
4
+ -- Clear gate-frame registration bindings, convert compatible loop checks and remove layout from
5
+ -- executable copies. Unconvertible trees are retained for explicit rejection when loaded.
6
+ -- `instruction` and `outcome_text` are text columns holding hand-serialized JSON. A valid stored
7
+ -- document can contain the literal 6-character escape `\u0000`, which
8
+ -- `JSON.parse` accepts but which `::jsonb` always rejects, because the decoded NUL byte cannot be represented
9
+ -- in Postgres text at all, regardless of position. A whole-document cast therefore blocks conversion when
10
+ -- any opaque leaf contains this escape. A fixed sentinel would corrupt U+E000 payloads and cannot handle
11
+ -- lone UTF-16 surrogates. `pg_temp.waiting_removal_has_nul_escape` scans raw text with backslash parity and reports
12
+ -- true only for a genuine, unescaped `\u0000` escape token (never for an escaped backslash followed by the
13
+ -- literal text `u0000`, and never for an unrelated character). `pg_temp.waiting_removal_parse_jsonb` returns NULL whenever
14
+ -- the text carries such a token or is otherwise malformed JSON, including a lone UTF-16 surrogate, which
15
+ -- `jsonb` also rejects on input; both failures leave the ROW untouched by every statement below, regardless of
16
+ -- where in the document the token sits. Two distinct
17
+ -- row classes can be left behind this way, each with its own actual rejection reader, named separately because
18
+ -- they are read by different code and neither is "the outcome codec" in general: (1) an `instruction` of
19
+ -- `workload.kind = 'workflow'` whose embedded `definitionSnapshot` still contains a stored `gate` node or loop
20
+ -- `gate` is rejected only when that instruction is later parsed as an executable workflow instruction, i.e. when
21
+ -- an outcome for this attempt is accepted (`WorkflowInvocationInputSchema`,
22
+ -- `framework/subsystems/workflow-engine/src/workflow-attempt-instruction.ts`; the parse runs at
23
+ -- `framework/subsystems/workflow-engine/src/workflow-attempt-outcome.ts`); this row's own `status` need not be `paused` and it may
24
+ -- carry no `outcome_text` at all. (2) an `outcome_text` of `workload.kind = 'workflow'` with its OWN recorded
25
+ -- `status: 'paused'` keeps that superseded status and is rejected visibly when read, because the outcome codec
26
+ -- no longer accepts it once this migration ships
27
+ -- (`framework/subsystems/workflow-engine/src/execution-attempt-owner-recovery.ts`,
28
+ -- `framework/storage/pg/src/execution-attempt-control.ts`). The conformance suite counts both classes separately
29
+ -- by owner and remaining legacy structure or status (unlike SQLite, which
30
+ -- has no such whole-document restriction). No fixed sentinel of any kind is introduced: valid
31
+ -- JSON containing U+E000, or an escaped backslash in front of the literal text `u0000`, casts and round-trips
32
+ -- exactly like every other value.
33
+ CREATE FUNCTION pg_temp.waiting_removal_has_nul_escape(value text) RETURNS boolean LANGUAGE sql IMMUTABLE AS $$
34
+ SELECT COALESCE(value ~ $nul$(^|[^\\])(\\\\)*\\u0000$nul$, false)
35
+ $$;--> statement-breakpoint
36
+ CREATE FUNCTION pg_temp.waiting_removal_parse_jsonb(value text) RETURNS jsonb LANGUAGE plpgsql IMMUTABLE AS $$
37
+ BEGIN
38
+ IF value IS NULL OR pg_temp.waiting_removal_has_nul_escape(value) THEN
39
+ RETURN NULL;
40
+ END IF;
41
+ RETURN value::jsonb;
42
+ EXCEPTION WHEN OTHERS THEN
43
+ RETURN NULL;
44
+ END
45
+ $$;--> statement-breakpoint
46
+ UPDATE "workflow_executions" SET "external_registration_frame_id" = NULL WHERE "external_registration_frame_id" IN (SELECT "frame_id" FROM "worklog_frame_entries" WHERE "node_type" = 'gate') OR "external_registration_frame_id" IN (SELECT "frame_id" FROM "workflow_execution_frames" WHERE "node_type" = 'gate');--> statement-breakpoint
47
+ UPDATE "workflow_executions" SET "status" = 'failed', "error" = 'Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.', "completed_at" = (extract(epoch FROM clock_timestamp()) * 1000)::bigint WHERE "status" IN ('pending', 'running', 'paused', 'finalizing') AND ("status" = 'paused' OR "id" IN (SELECT "execution_id" FROM "workflow_execution_frames" WHERE "status" = 'waiting') OR "id" IN (SELECT "execution_id" FROM "worklog_frame_entries" WHERE "status" = 'waiting') OR "id" IN (SELECT "execution_id" FROM "worklog_summaries" WHERE "status" = 'paused') OR "id" IN (SELECT "execution_id" FROM "workflow_gate_instances" WHERE "status" = 'waiting') OR "id" IN (SELECT "execution_id" FROM "worklog_gate_events" WHERE "status" = 'waiting'));--> statement-breakpoint
48
+ WITH candidates AS MATERIALIZED (
49
+ SELECT execution_attempt_id, pg_temp.waiting_removal_parse_jsonb(outcome_text) AS outcome,
50
+ pg_temp.waiting_removal_parse_jsonb(instruction) AS instruction
51
+ FROM execution_attempt
52
+ WHERE strpos(outcome_text, '"paused"') > 0 OR strpos(outcome_text, '\u') > 0
53
+ )
54
+ UPDATE execution_attempt AS attempt
55
+ SET outcome_text = jsonb_build_object(
56
+ 'executionId', c.outcome ->> 'executionId', 'workflowId', c.outcome ->> 'workflowId', 'status', 'failed',
57
+ 'error', 'Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.'
58
+ || ' pausedAtGateId=' || (c.outcome -> 'pausedAtGateId')::text
59
+ || ' pausedAtFrameId=' || (c.outcome -> 'pausedAtFrameId')::text)::text
60
+ FROM candidates AS c
61
+ WHERE attempt.execution_attempt_id = c.execution_attempt_id
62
+ AND c.outcome ->> 'status' = 'paused' AND c.instruction #>> '{workload,kind}' = 'workflow';--> statement-breakpoint
63
+ CREATE FUNCTION pg_temp.waiting_removal_node_blocked(node jsonb) RETURNS boolean LANGUAGE plpgsql IMMUTABLE AS $$
64
+ DECLARE child jsonb;
65
+ BEGIN
66
+ IF jsonb_typeof(node) IS DISTINCT FROM 'object' THEN
67
+ RETURN false;
68
+ END IF;
69
+ CASE node ->> 'type'
70
+ WHEN 'gate' THEN
71
+ RETURN true;
72
+ WHEN 'sequence' THEN
73
+ IF jsonb_typeof(node -> 'nodes') = 'array' THEN
74
+ FOR child IN SELECT "value" FROM jsonb_array_elements(node -> 'nodes') LOOP
75
+ IF pg_temp.waiting_removal_node_blocked(child) THEN RETURN true; END IF;
76
+ END LOOP;
77
+ END IF;
78
+ WHEN 'parallel' THEN
79
+ IF jsonb_typeof(node -> 'branches') = 'object' THEN
80
+ FOR child IN SELECT "value" FROM jsonb_each(node -> 'branches') LOOP
81
+ IF pg_temp.waiting_removal_node_blocked(child) THEN RETURN true; END IF;
82
+ END LOOP;
83
+ END IF;
84
+ WHEN 'iterate', 'iterate-chain' THEN
85
+ RETURN pg_temp.waiting_removal_node_blocked(node -> 'body');
86
+ WHEN 'loop' THEN
87
+ IF EXISTS (SELECT 1 FROM jsonb_object_keys(node) AS "lk" WHERE "lk" NOT IN ('id', 'type', 'maxRounds', 'body', 'gate', 'check', 'onExhausted', 'when', 'skip', 'writes')) THEN
88
+ RETURN true;
89
+ END IF;
90
+ IF node ? 'gate' AND NOT (
91
+ jsonb_typeof(node -> 'gate') = 'object'
92
+ AND NOT (node ? 'check')
93
+ AND NOT (node ? 'onExhausted')
94
+ AND NOT EXISTS (SELECT 1 FROM jsonb_object_keys(node -> 'gate') AS "k" WHERE "k" NOT IN ('handler', 'input', 'config'))
95
+ AND COALESCE(jsonb_typeof(node -> 'gate' -> 'handler') = 'string', false)
96
+ AND COALESCE(length(node -> 'gate' ->> 'handler') > 0, false)
97
+ AND (
98
+ NOT (node -> 'gate' ? 'input')
99
+ OR (COALESCE(jsonb_typeof(node -> 'gate' -> 'input') = 'string', false) AND COALESCE(length(node -> 'gate' ->> 'input') > 0, false))
100
+ )
101
+ ) THEN
102
+ RETURN true;
103
+ END IF;
104
+ RETURN pg_temp.waiting_removal_node_blocked(node -> 'body');
105
+ ELSE
106
+ NULL;
107
+ END CASE;
108
+ RETURN false;
109
+ END
110
+ $$;--> statement-breakpoint
111
+ -- A live execution cannot continue with a blocked definition in either executable copy.
112
+ WITH live_attempts AS MATERIALIZED (
113
+ SELECT e.id, pg_temp.waiting_removal_parse_jsonb(attempt.instruction) AS doc
114
+ FROM workflow_executions AS e
115
+ JOIN active_execution_attempt AS active ON active.execution_id = e.id
116
+ JOIN execution_attempt AS attempt USING (execution_id, execution_attempt_id)
117
+ WHERE e.status IN ('pending', 'running') AND (strpos(attempt.instruction, '"definitionSnapshot"') > 0 OR strpos(attempt.instruction, '\u') > 0)
118
+ ), incompatible AS (
119
+ SELECT e.id
120
+ FROM workflow_executions AS e
121
+ JOIN workflow_run_contexts AS ctx ON ctx.execution_id = e.id
122
+ WHERE e.status IN ('pending', 'running') AND pg_temp.waiting_removal_node_blocked(ctx.definition_snapshot -> 'root')
123
+ UNION ALL
124
+ SELECT id FROM live_attempts
125
+ WHERE doc #>> '{workload,kind}' = 'workflow'
126
+ AND pg_temp.waiting_removal_node_blocked(doc #> '{workload,input,definitionSnapshot,root}')
127
+ )
128
+ UPDATE workflow_executions
129
+ SET status = 'failed', error = 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.',
130
+ completed_at = (extract(epoch FROM clock_timestamp()) * 1000)::bigint
131
+ WHERE status IN ('pending', 'running') AND id IN (SELECT id FROM incompatible);--> statement-breakpoint
132
+ -- Close only the current attempt belonging to a failed workflow. Existing outcomes are
133
+ -- still evidence, and provider-operation recovery retains its own durable obligations.
134
+ UPDATE execution_attempt AS attempt
135
+ SET status = 'settled',
136
+ settlement_kind = CASE WHEN attempt.outcome_text IS NULL THEN 'abandoned' ELSE 'outcome' END,
137
+ claimable = 0, operation_start_gate = 'closed'
138
+ FROM workflow_executions AS execution
139
+ JOIN active_execution_attempt AS active ON active.execution_id = execution.id
140
+ WHERE attempt.execution_id = active.execution_id AND attempt.execution_attempt_id = active.execution_attempt_id
141
+ AND attempt.settlement_kind IS NULL AND execution.status = 'failed'
142
+ AND execution.error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
143
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.');--> statement-breakpoint
144
+ -- Detach settled attempts from active ownership. Keep the attempts and their historical evidence.
145
+ DELETE FROM active_execution_attempt AS active
146
+ WHERE EXISTS (
147
+ SELECT 1 FROM workflow_executions AS execution
148
+ JOIN execution_attempt AS attempt ON attempt.execution_id = execution.id
149
+ WHERE active.execution_id = execution.id AND active.execution_attempt_id = attempt.execution_attempt_id
150
+ AND attempt.status = 'settled' AND attempt.settlement_kind IS NOT NULL AND execution.status = 'failed'
151
+ AND execution.error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
152
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.')
153
+ );--> statement-breakpoint
154
+ UPDATE workflow_execution_frames AS frame
155
+ SET status = 'failed',
156
+ error = CASE WHEN frame.status = 'waiting' THEN 'Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.'
157
+ ELSE (SELECT e.error FROM workflow_executions AS e WHERE e.id = frame.execution_id) END,
158
+ completed_at = (extract(epoch FROM clock_timestamp()) * 1000)::bigint
159
+ WHERE frame.status = 'waiting' OR (frame.status IN ('pending', 'running') AND frame.execution_id IN (
160
+ SELECT id FROM workflow_executions WHERE status = 'failed' AND error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
161
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.')
162
+ ));--> statement-breakpoint
163
+ UPDATE worklog_frame_entries AS frame
164
+ SET status = 'failed',
165
+ error = CASE WHEN frame.status = 'waiting' THEN 'Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.'
166
+ ELSE (SELECT e.error FROM workflow_executions AS e WHERE e.id = frame.execution_id) END,
167
+ completed_at = (extract(epoch FROM clock_timestamp()) * 1000)::bigint
168
+ WHERE frame.status = 'waiting' OR (frame.status IN ('pending', 'running') AND frame.execution_id IN (
169
+ SELECT id FROM workflow_executions WHERE status = 'failed' AND error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
170
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.')
171
+ ));--> statement-breakpoint
172
+ UPDATE worklog_summaries AS summary
173
+ SET status = 'failed', error = e.error, completed_at = (extract(epoch FROM clock_timestamp()) * 1000)::bigint
174
+ FROM workflow_executions AS e
175
+ WHERE summary.execution_id = e.id AND summary.status IN ('pending', 'running', 'paused', 'finalizing')
176
+ AND e.status = 'failed' AND e.error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
177
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.');--> statement-breakpoint
178
+ UPDATE "worklog_summaries" AS "s" SET "status" = "e"."status", "completed_at" = COALESCE("s"."completed_at", "e"."completed_at"), "error" = COALESCE("s"."error", "e"."error") FROM "workflow_executions" AS "e" WHERE "e"."id" = "s"."execution_id" AND "s"."status" = 'paused';--> statement-breakpoint
179
+ UPDATE workflow_step_spans
180
+ SET status = 'failed', completed_at = (extract(epoch FROM clock_timestamp()) * 1000)::bigint
181
+ WHERE status = 'running' AND execution_id IN (
182
+ SELECT id FROM workflow_executions WHERE status = 'failed' AND error IN ('Workflow waits were removed; this execution was waiting for a gate response and can no longer continue.',
183
+ 'Workflow waits were removed; this execution has an incompatible definition snapshot and cannot continue.')
184
+ );--> statement-breakpoint
185
+ CREATE FUNCTION pg_temp.waiting_removal_convert_loops(node jsonb) RETURNS jsonb LANGUAGE plpgsql IMMUTABLE AS $$
186
+ BEGIN
187
+ IF jsonb_typeof(node) IS DISTINCT FROM 'object' THEN
188
+ RETURN node;
189
+ END IF;
190
+ CASE node ->> 'type'
191
+ WHEN 'sequence' THEN
192
+ IF jsonb_typeof(node -> 'nodes') = 'array' THEN
193
+ RETURN jsonb_set(node, '{nodes}', COALESCE((SELECT jsonb_agg(pg_temp.waiting_removal_convert_loops("e") ORDER BY "ord") FROM jsonb_array_elements(node -> 'nodes') WITH ORDINALITY AS "t"("e", "ord")), '[]'::jsonb));
194
+ END IF;
195
+ WHEN 'parallel' THEN
196
+ IF jsonb_typeof(node -> 'branches') = 'object' THEN
197
+ RETURN jsonb_set(node, '{branches}', COALESCE((SELECT jsonb_object_agg("k", pg_temp.waiting_removal_convert_loops("v")) FROM jsonb_each(node -> 'branches') AS "t"("k", "v")), '{}'::jsonb));
198
+ END IF;
199
+ WHEN 'iterate', 'iterate-chain' THEN
200
+ IF node ? 'body' THEN
201
+ RETURN jsonb_set(node, '{body}', pg_temp.waiting_removal_convert_loops(node -> 'body'));
202
+ END IF;
203
+ WHEN 'loop' THEN
204
+ node := CASE WHEN node ? 'body' THEN jsonb_set(node, '{body}', pg_temp.waiting_removal_convert_loops(node -> 'body')) ELSE node END;
205
+ IF jsonb_typeof(node -> 'gate') = 'object' AND NOT (node ? 'check') THEN
206
+ RETURN (node - 'gate') || jsonb_build_object('onExhausted', 'complete', 'check', node -> 'gate');
207
+ END IF;
208
+ RETURN node;
209
+ ELSE
210
+ NULL;
211
+ END CASE;
212
+ RETURN node;
213
+ END
214
+ $$;--> statement-breakpoint
215
+ WITH rewrites AS MATERIALIZED (
216
+ SELECT id, pg_temp.waiting_removal_convert_loops(root) AS root FROM workflow_definitions
217
+ WHERE strpos(root::text, '"gate"') > 0 AND NOT pg_temp.waiting_removal_node_blocked(root)
218
+ )
219
+ UPDATE workflow_definitions AS definition
220
+ SET root = r.root FROM rewrites AS r
221
+ WHERE definition.id = r.id AND definition.root IS DISTINCT FROM r.root;--> statement-breakpoint
222
+ WITH rewrites AS MATERIALIZED (
223
+ SELECT execution_id, pg_temp.waiting_removal_convert_loops(definition_snapshot -> 'root') AS root
224
+ FROM workflow_run_contexts
225
+ WHERE strpos((definition_snapshot -> 'root')::text, '"gate"') > 0
226
+ AND NOT pg_temp.waiting_removal_node_blocked(definition_snapshot -> 'root')
227
+ )
228
+ UPDATE workflow_run_contexts AS context
229
+ SET definition_snapshot = jsonb_set(context.definition_snapshot, '{root}', r.root)
230
+ FROM rewrites AS r
231
+ WHERE context.execution_id = r.execution_id AND context.definition_snapshot -> 'root' IS DISTINCT FROM r.root;--> statement-breakpoint
232
+ -- Layout belongs to the stored definition, while these two copies are executable inputs.
233
+ UPDATE workflow_run_contexts
234
+ SET definition_snapshot = definition_snapshot - 'canvasLayout'
235
+ WHERE jsonb_typeof(definition_snapshot) = 'object' AND definition_snapshot ? 'canvasLayout';--> statement-breakpoint
236
+ -- Parse the text instruction once per candidate. Keep its original text when the snapshot is unchanged.
237
+ WITH candidates AS MATERIALIZED (
238
+ SELECT execution_attempt_id, pg_temp.waiting_removal_parse_jsonb(instruction) AS doc
239
+ FROM execution_attempt
240
+ WHERE strpos(instruction, '"definitionSnapshot"') > 0 OR strpos(instruction, '\u') > 0
241
+ ), snapshots AS (
242
+ SELECT execution_attempt_id, doc, doc #> '{workload,input,definitionSnapshot}' AS snapshot
243
+ FROM candidates
244
+ WHERE doc #>> '{workload,kind}' = 'workflow'
245
+ ), rewrites AS MATERIALIZED (
246
+ SELECT execution_attempt_id, doc, snapshot,
247
+ CASE WHEN strpos((snapshot -> 'root')::text, '"gate"') > 0 AND NOT pg_temp.waiting_removal_node_blocked(snapshot -> 'root')
248
+ THEN jsonb_set(snapshot - 'canvasLayout', '{root}', pg_temp.waiting_removal_convert_loops(snapshot -> 'root'))
249
+ ELSE snapshot - 'canvasLayout' END AS rewritten
250
+ FROM snapshots WHERE jsonb_typeof(snapshot) = 'object'
251
+ )
252
+ UPDATE execution_attempt AS attempt
253
+ SET instruction = jsonb_set(r.doc, '{workload,input,definitionSnapshot}', r.rewritten)::text
254
+ FROM rewrites AS r
255
+ WHERE attempt.execution_attempt_id = r.execution_attempt_id AND r.snapshot IS DISTINCT FROM r.rewritten;--> statement-breakpoint
256
+ DROP FUNCTION pg_temp.waiting_removal_convert_loops(jsonb);--> statement-breakpoint
257
+ DROP FUNCTION pg_temp.waiting_removal_node_blocked(jsonb);--> statement-breakpoint
258
+ DROP FUNCTION pg_temp.waiting_removal_parse_jsonb(text);--> statement-breakpoint
259
+ DROP FUNCTION pg_temp.waiting_removal_has_nul_escape(text);
@@ -239,6 +239,20 @@
239
239
  "when": 1795263644243,
240
240
  "tag": "0033_workflow_definition_extensions",
241
241
  "breakpoints": true
242
+ },
243
+ {
244
+ "idx": 34,
245
+ "version": "7",
246
+ "when": 1795369102042,
247
+ "tag": "0034_workflow_step_names",
248
+ "breakpoints": true
249
+ },
250
+ {
251
+ "idx": 35,
252
+ "version": "7",
253
+ "when": 1795388652936,
254
+ "tag": "0035_waiting-removal-data",
255
+ "breakpoints": true
242
256
  }
243
257
  ]
244
258
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@makaio/storage-pg",
3
3
  "description": "Postgres storage engine for the Makaio framework: driver glue, error classification, and migration behavior registered through the storage engine seam.",
4
- "version": "1.0.0-dev-1795338532209",
4
+ "version": "1.0.0-dev-1795446229073",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",