* fix(postgres): scope OpenPRs to the caller's org A principal-carrying caller read every org's pr_open tasks. The query now honours the request scope; the daemon's unscoped review scan still sees all orgs. Issue: archie-core-kgxw.23 * fix(statestore): serve task logs only to the task's org ReadTaskLog and StreamTaskLogContent took a task ID and read the file with no org check. The State Store's task-log reader now confirms the task is visible under the caller's scope first; unscoped services read as before. Issue: archie-core-kgxw.24 * fix(postgres): scope ListPolicies to the caller's org Every org's policy text reached any principal-carrying caller. A scoped read now sees its own org's policies, plus instance policies when scoped to the system org. The engines and the lockout guard decide for every org, so they read the full chain unscoped: the dashboard engine loads without the request's principal and the guard lifts the caller's scope. Issue: archie-core-kgxw.25 * fix(postgres): scope identity reads to the caller's org ListIdentities and GetIdentity (and its alias fallback) returned every identity to any caller. A caller scoped to an org now sees only that org's members and assigned agents; the system org and unscoped services still see the whole instance. Issue: archie-core-kgxw.26 * fix(postgres): hide instance status from other orgs ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot answered any org principal with instance-wide state. They now return nothing unless the request is unscoped or scoped to the system org. The dashboard reads the published config as itself, since its handlers project it for callers of every org. Issue: archie-core-kgxw.27 * feat(postgres): assign new agents an org and role at creation A bot or service account created through the API served no org until the next State Store boot granted it one. Create now assigns it to the org the request acts in with the developer role in the same transaction, so it is authorized immediately. Issue: archie-core-kgxw.14 * feat(edastore): create event records in a chosen workspace Sources are created in a workspace of the caller's org, the default one when none is named; event types and bindings take their source's workspace and mappings their event type's, the same rule the org backfill applied. The binding, mapping and event type contracts carry the workspace, the four list endpoints accept ?workspace=, and the Events page has a workspace selector that filters every step and picks where a new source goes. Issue: archie-core-kgxw.11 * feat(webui): authorize a record in its workspace The API built every access request with no workspace, so workspace policies and per-workspace roles never applied. A request naming a source, binding, mapping, event type or task now reads the record's workspace under the caller's scope and authorizes in it; tasks carry their workspace on the State Store contract for this. Issue: archie-core-kgxw.9 * feat(access): expose workspace environment to policies Policies can test resource.workspace.environment. The workspace attribute is the workspace entity, which carries its environment; the principal brings its org's workspaces from the State Store, so the dashboard and the daemon's run checks describe a resource's workspace without another lookup, and the daemon now authorizes runs in the task's or binding's workspace. Issue: archie-core-kgxw.10 * fix(webui): ask read_secret when a source secret is read Rotating a source's secret returns its value, but the route asked update, which every developer holds, so no policy could narrow who reads a secret. It now asks read_secret on the secret, which the shipped roles give admins and an object policy can narrow further. Issue: archie-core-kgxw.17 * refactor(org): delete the unused upgrade-incomplete sentinel Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org upgrade before it serves. Issue: archie-core-kgxw.18
245 lines
13 KiB
SQL
245 lines
13 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
|
|
-- A mapping lives in its event type's workspace; one with no event type in
|
|
-- the org's default workspace.
|
|
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(name), sqlc.arg(source_hint), sqlc.arg(event_type), sqlc.arg(fields), sqlc.arg(org_id),
|
|
COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = sqlc.arg(event_type) AND y.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- 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(),
|
|
workspace_id = COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = sqlc.arg(event_type) AND y.org_id = mappings.org_id), 'default')
|
|
WHERE mappings.id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR mappings.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
|
|
-- A binding lives in its source's workspace.
|
|
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(name), sqlc.arg(source), sqlc.arg(mapping), sqlc.arg(filter), sqlc.arg(workflow), sqlc.arg(owner), sqlc.arg(repo), 1,
|
|
sqlc.arg(status), sqlc.arg(inputs), sqlc.arg(repo_param), sqlc.arg(org_id),
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- name: GetBinding :one
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_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, workspace_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, workspace_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(),
|
|
workspace_id = COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = bindings.org_id), 'default')
|
|
WHERE bindings.id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR bindings.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
|
|
-- An event type lives in its source's workspace.
|
|
INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(source), sqlc.arg(name), sqlc.arg(rule), sqlc.arg(schema), sqlc.arg(org_id),
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- name: GetEventType :one
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
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 :execrows
|
|
-- Inserts nothing when the workspace is not one of the org's.
|
|
INSERT INTO sources (path, signing, secret, org_id, workspace_id)
|
|
SELECT sqlc.arg(path), sqlc.arg(signing), sqlc.arg(secret), w.org_id, w.id
|
|
FROM workspaces w WHERE w.org_id = sqlc.arg(org_id) AND w.id = sqlc.arg(workspace_id);
|
|
|
|
-- 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;
|