Files
archie/internal/infrastructure/postgres/queries/edastore.sql
T
sam 23f87c4cea Fix/workflow eda audit (#1627)
* fix(webui): accept the merge task action

The action whitelist omitted merge, so the dashboard's Merge button on a
pr_open task was refused with 400 unknown action.

Closes: archie-core-slpoz

* fix(workflow): give each parallel branch its own outputs

Branches shared the task's Outputs map, so concurrent declared-output
writes raced (or were lost when the map started nil). Each branch now
writes a copy; join merges new outputs in branch-name order and refuses
one written by two branches as a second write.

Closes: archie-core-3a7w2

* fix(postgres): dispatch captures oldest first and keep pending ones

ListUndispatchedCaptures ordered newest first and listed captures no
binding can match (failed HMAC), so sustained intake starved older work
and bad signatures held the batch. It is now oldest first and lists only
dispatchable captures. Count retention no longer evicts a capture that
is still pending for an armed binding. The daemon lists only sources
with an enabled workflow, so captures waiting on a disabled one do not
hold the oldest-first batch.

Closes: archie-core-2et4b, archie-core-o0s0z

* fix(ui): refuse renaming a workflow onto an existing id

Saving upserts by id, so editing a workflow's id to another workflow's
id removed the open one and overwrote the other. The editor now refuses
that save and says why.

Closes: archie-core-wpoz8

* fix(workflow): reject unknown step settings at save

Step factories decoded settings with yaml.Node.Decode, which drops keys
it does not know, and settings-less steps ignored settings entirely, so a
misspelt key (readOnly for read_only) saved and silently did nothing.
Every factory now decodes strictly and settings-less steps refuse
settings; ParseDefinition runs the factories, so the save refuses them.

Closes: archie-core-8b5nk

* fix(postgres): keep a stored capture when its prune fails

InsertCapture committed the capture, then returned the prune's error, so
intake answered 500 and a sender without a delivery header retried into
a duplicate capture and a duplicate dispatch. A failed prune is now
logged and the stored capture's id returned.

Closes: archie-core-3miuh

* fix(crondelivery): fail and advance a job no runner can deliver

Router.Run returned nil for a kind with no runner, so the engine reported
a successful run while the schedule never advanced and the job fired on
every tick. The run now fails with the refusal, which the engine reports,
and the schedule advances (a one-shot is removed). The router's own
refusal event is gone with it.

Closes: archie-core-s6ahh

* fix(taskactions): stop the runs of cancelled callees

CancelExecution cancelled a caller's waited-on callees in the store, but
only the caller's run was stopped, so callee containers ran to the end.
CancelExecution now returns the callees it cancelled (nested included)
in place of the step ids nobody read, and every cancelling action stops
their runs too.

Closes: archie-core-9labz

* fix(workflow): treat a zero json.Number as false in when

Task inputs and step results decode numbers as json.Number, which truthy
did not unpack, so when: inputs.count held for a count of 0.

Closes: archie-core-uv270

* fix(webui): read a workflow's runs with a workflow-filtered page

The workflows page and the canvas run picker filtered the newest 100
tasks of every workflow client-side, so on a busy instance a workflow's
older runs were invisible. GET /api/tasks takes a workflow filter, carried
through the State Store's Tasks request to the query, and both views ask
for the selected workflow's runs.

Closes: archie-core-tpiw1

* fix(ui): load the restored workflow into the editor

Restore Defaults saved the shipped definition but left the open draft in
the editor, still marked unsaved, so a later save re-applied it. The
editor now reloads the restored definition.

Closes: archie-core-85e7w
2026-10-12 03:20:00 +11:00

232 lines
12 KiB
SQL

-- EDA queries: captures, mappings, bindings, dispatch ledgers, tool_calls.
-- name: InsertCapture :exec
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, delivery, org_id, workspace_id)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
COALESCE(s.org_id, @default_org::text), COALESCE(s.workspace_id, @default_workspace::text)
FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2;
-- name: ListCaptures :many
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery
FROM captures
WHERE org_id = @org_id
ORDER BY received_at DESC
LIMIT $1;
-- name: ListUndispatchedCaptures :many
-- Oldest first, so sustained intake cannot starve accepted work. Only
-- dispatchable captures (signed, or from an approved unsigned source) are
-- listed: no binding ever matches the rest, so they would hold the batch.
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery
FROM captures c
WHERE c.org_id = @org_id AND c.source = ANY(@sources::text[])
AND c.event_type <> ''
AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
ORDER BY c.received_at, c.id
LIMIT @entry_limit;
-- name: DeleteCapturesOlderThan :exec
DELETE FROM captures WHERE received_at < $1;
-- name: DeleteCapturesBeyondCount :exec
-- Count retention stays within the just-inserted capture's org: one org's
-- volume must never evict another org's recent captures. It never evicts a
-- capture ListUndispatchedCaptures would still offer: that is accepted work.
DELETE FROM captures AS c
WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
AND c.id NOT IN (
SELECT k.id FROM captures AS k
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
ORDER BY k.received_at DESC
LIMIT sqlc.arg(max_events)
)
AND NOT (
c.event_type <> '' AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
);
-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetMapping :one
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListMappings :many
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: UpdateMapping :execrows
UPDATE mappings
SET name = sqlc.arg(name), source_hint = sqlc.arg(source_hint), event_type = sqlc.arg(event_type), fields = sqlc.arg(fields), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertMappingMatch :exec
INSERT INTO mapping_matches (mapping, capture) VALUES ($1, $2)
ON CONFLICT DO NOTHING;
-- name: DeleteMapping :execrows
DELETE FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertBinding :exec
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11, $12);
-- name: GetBinding :one
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListBindings :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: ArmedBindingsForSource :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE source = sqlc.arg(source) AND status = 'armed' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: UpdateBinding :execrows
UPDATE bindings
SET name = sqlc.arg(name), source = sqlc.arg(source), mapping = sqlc.arg(mapping), filter = sqlc.arg(filter),
workflow = sqlc.arg(workflow), owner = sqlc.arg(owner), repo = sqlc.arg(repo),
version = version + 1, status = sqlc.arg(status), inputs = sqlc.arg(inputs), repo_param = sqlc.arg(repo_param), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetBindingArmed :execrows
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: TransitionBindingStatus :execrows
UPDATE bindings SET status = @to_status, updated_at = now()
WHERE id = @id AND status = @from_status AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteBinding :execrows
DELETE FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertBindingDispatch :execrows
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason, delivery)
SELECT @binding, @binding_version, c.id, @task_id, @reason, c.delivery
FROM captures c WHERE c.id = @capture;
-- name: SetBindingDispatchTask :exec
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2;
-- name: ListBindingDispatches :many
SELECT d.binding, d.binding_version, d.capture, d.task_id, d.reason, d.dispatched_at
FROM binding_dispatches d JOIN bindings b ON b.id = d.binding
WHERE (sqlc.narg(scope_org)::text IS NULL OR b.org_id = sqlc.narg(scope_org))
AND (sqlc.narg(binding)::text IS NULL OR d.binding = sqlc.narg(binding))
AND (sqlc.narg(capture)::text IS NULL OR d.capture = sqlc.narg(capture))
AND (sqlc.narg(task_id)::bigint IS NULL OR d.task_id = sqlc.narg(task_id))
ORDER BY d.dispatched_at DESC
LIMIT @entry_limit;
-- name: InsertPlaybookDispatch :exec
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
VALUES ($1, $2, $3, $4);
-- name: DeletePlaybookDispatches :exec
DELETE FROM playbook_dispatches WHERE playbook_id = $1;
-- name: InsertToolCall :exec
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
SELECT sqlc.arg(id), t.id, sqlc.arg(attempt), sqlc.arg(tool), sqlc.arg(result), sqlc.arg(error), sqlc.arg(called_at), t.org_id, t.workspace_id
FROM tasks t WHERE t.id = sqlc.arg(task_id);
-- name: TaskToolCalls :many
SELECT id, task_id, attempt, tool, result, error, called_at
FROM tool_calls WHERE task_id = sqlc.arg(task_id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY called_at ASC;
-- name: InsertEventType :exec
INSERT INTO event_types (id, source, name, rule, schema, org_id)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetEventType :one
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY source, name;
-- name: EventTypesForSource :many
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE source = $1 ORDER BY name;
-- name: UpdateEventType :execrows
UPDATE event_types SET name = sqlc.arg(name), rule = sqlc.arg(rule), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteEventType :execrows
DELETE FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: LockEventTypeSource :exec
-- Serialises saves per source so two concurrent saves cannot each pass the
-- overlap check against a set that excludes the other.
SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text));
-- name: InsertSource :exec
INSERT INTO sources (path, signing, secret, org_id, workspace_id) VALUES ($1, $2, $3, $4, $5);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC, path;
-- name: SetSourceSigning :execrows
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceSecret :execrows
UPDATE sources SET secret = sqlc.arg(secret), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceName :execrows
UPDATE sources SET name = sqlc.arg(name), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceDeliveryHeader :execrows
UPDATE sources SET delivery_header = sqlc.arg(delivery_header), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteUnboundSource :execrows
-- A source an armed binding fires on is not deleted: the binding would
-- silently stop receiving events.
DELETE FROM sources s WHERE s.path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR s.org_id = sqlc.narg(scope_org))
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
-- name: SourceExists :one
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
-- name: DeriveSources :exec
SELECT derive_sources();
-- name: InsertCaptureRefusal :exec
INSERT INTO capture_refusals (source, addr, refused_at) VALUES ($1, $2, $3);
-- name: DeleteCaptureRefusalsBefore :exec
DELETE FROM capture_refusals WHERE refused_at < $1;
-- name: CaptureRefusalSummary :many
WITH r AS (
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
FROM capture_refusals cr WHERE refused_at >= sqlc.arg(since)
AND (sqlc.narg(scope_org)::text IS NULL
OR EXISTS (SELECT 1 FROM sources s WHERE s.path = cr.source AND s.org_id = sqlc.narg(scope_org)))
GROUP BY source
), a AS (
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= sqlc.arg(since) GROUP BY source
)
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
FROM r LEFT JOIN a ON a.source = r.source
ORDER BY r.source;