Files
archie/internal/infrastructure/postgres/queries/tasks.sql
T
sam 3960a00f5d Feat/org epic (#1634)
* 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
2026-10-12 03:24:04 +11:00

272 lines
14 KiB
SQL

-- name: ClaimNextTask :one
-- FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
-- claimers running at once take different rows instead of blocking, so the
-- daemon no longer depends on holding the only connection to the file.
-- The claim also starts the attempt's output set empty: outputs is
-- attempt-scoped, so nothing the previous attempt wrote survives
-- (docs/prds/workflow-call-outputs.md).
UPDATE tasks
SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now()
WHERE id = (
SELECT id FROM tasks
WHERE status = 'queued'
ORDER BY id
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING *;
-- name: TaskByID :one
SELECT * FROM tasks WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: TaskByIssue :one
SELECT * FROM tasks WHERE owner = sqlc.arg(owner) AND repo = sqlc.arg(repo) AND issue_number = sqlc.arg(issue_number)
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: TaskByPR :one
-- OpenTaskByPR is the live-task lookup: it only resolves a PR the task is
-- currently tracking as open, so a merged or rejected task does not read as
-- the owner of a PR number it no longer holds.
SELECT * FROM tasks WHERE owner = sqlc.arg(owner) AND repo = sqlc.arg(repo) AND pr_number = sqlc.arg(pr_number) AND status = sqlc.arg(status)
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListTaskSummaries :many
-- The dashboard's list. This projection is deliberately narrow: Plan gates
-- the "Decision required" panel and Source labels the row, and widening it
-- back would send rows without them. outputs rides it because the task row's
-- own structured result is part of the summary a caller or the dashboard
-- reads (docs/prds/workflow-call-outputs.md, "Storage and wire").
-- review_gate and rereview_rounds join it deliberately: the offer the gate
-- wrote is what the operator reads before answering it
-- (docs/prds/pr-review-operator-response.md, "The review the operator
-- answers"), and the round count is the cap's visible half.
SELECT id, owner, repo, issue_number, title, status, workflow,
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
created_at, updated_at, plan, source, identity, binding_id, binding_version,
outputs, review_gate, rereview_rounds
FROM tasks
WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
AND (cardinality(@statuses::text[]) = 0 OR status = ANY(@statuses::text[]))
AND (@workflow::text = '' OR workflow = @workflow::text)
AND (sqlc.narg(before_updated)::timestamptz IS NULL
OR (updated_at, id) < (sqlc.narg(before_updated)::timestamptz, @before_id::bigint))
ORDER BY updated_at DESC, id DESC
LIMIT @page_limit;
-- name: CountTasksByStatus :many
SELECT status, count(*)::int AS count FROM tasks WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) GROUP BY status;
-- name: InsertChatTask :one
-- The synthetic issue number keeps chat-sourced tasks off the forge's real
-- issue numbers; chat_issue_numbers is the single source of truth for it, and
-- bumping its row serializes concurrent inserts in one repository.
-- fallback_issue_number is only the seed for a repo's first chat task -- the
-- passed value is not the issue number that lands.
-- inputs is the chat task's own workflow inputs; a binding dispatch passes
-- its inputs here too, so this insert is the single writer of the column and
-- StampTaskBinding adds only the provenance.
-- The task runs in its identity's org; a caller confined to another org
-- inserts nothing, so it cannot start work under an identity it does not own.
WITH number AS (
INSERT INTO chat_issue_numbers (owner, repo, last_number)
VALUES (sqlc.arg(owner), sqlc.arg(repo), sqlc.arg(fallback_issue_number)::bigint + 1)
ON CONFLICT (owner, repo) DO UPDATE SET last_number = chat_issue_numbers.last_number + 1
RETURNING last_number
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin)
SELECT
sqlc.arg(owner), sqlc.arg(repo), number.last_number,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity),
owner_org.id,
sqlc.arg(inputs),
sqlc.arg(origin)
FROM number, (SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = sqlc.arg(identity)),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = sqlc.arg(identity)
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
) AS id) AS owner_org
WHERE sqlc.narg(scope_org)::text IS NULL OR owner_org.id = sqlc.narg(scope_org)
RETURNING *;
-- name: ActiveTasksByOrigin :many
-- The queued and running tasks one conversation created, for its /stop.
SELECT * FROM tasks
WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'running') AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
ORDER BY id;
-- name: UnpostedOutcomes :many
-- Finished chat tasks whose outcome has not reached their conversation yet.
SELECT * FROM tasks
WHERE origin <> '' AND NOT outcome_posted
AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed')
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
ORDER BY id
LIMIT 50;
-- name: MarkOutcomePosted :exec
UPDATE tasks SET outcome_posted = true WHERE id = $1;
-- name: StampTaskBinding :exec
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1;
-- name: UpdateTask :exec
-- An update without a definition keeps the stored pin: a task's own
-- credential sends none, so it cannot replace the definition it runs.
UPDATE tasks SET workflow = @workflow, branch = @branch, plan = @plan, notes = @notes,
pr_number = @pr_number, tokens_used = @tokens_used, iterations = @iterations, park_reason = @park_reason,
watch_comment_id = @watch_comment_id, retry_count = @retry_count, remediation_rounds = @remediation_rounds,
review_payload = @review_payload,
workflow_definition_version = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_version ELSE @workflow_definition_version END,
workflow_definition_digest = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_digest ELSE @workflow_definition_digest END,
workflow_definition_yaml = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_yaml ELSE @workflow_definition_yaml::text END,
outputs = @outputs, review_gate = @review_gate, rereview_rounds = @rereview_rounds,
retry_mode = @retry_mode, updated_at = now()
WHERE id = @id;
-- name: RereviewRounds :one
-- Read under the lock LockTaskStatus takes in the same transaction, so the
-- cap decision and the increment it guards cannot interleave with a second
-- response write.
SELECT rereview_rounds FROM tasks WHERE id = $1;
-- name: LockTaskStatus :one
-- Locks the task's row and returns its current status, so the staleness and
-- transition-table checks that decide a guarded write hold against a
-- concurrent claim or transition until the update in the same transaction.
SELECT status FROM tasks WHERE id = $1 FOR UPDATE;
-- name: TransitionTask :execrows
-- Arriving at 'parked' also records why. The class is normalized by the
-- caller, so an unknown value persists as needs_human rather than as itself.
UPDATE tasks
SET status = $2,
park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END,
park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END,
updated_at = now()
WHERE id = $1 AND status = $5;
-- name: InsertTransition :exec
INSERT INTO transitions (task_id, from_status, to_status, detail, org_id, workspace_id)
SELECT t.id, sqlc.arg(from_status), sqlc.arg(to_status), sqlc.arg(detail), t.org_id, t.workspace_id
FROM tasks t WHERE t.id = sqlc.arg(task_id);
-- name: ParkTask :execrows
UPDATE tasks
SET status = 'parked', park_reason = $2, park_class = $3, updated_at = now()
WHERE id = $1 AND status = $4;
-- name: ClearTerminalTasks :execrows
DELETE FROM tasks
WHERE status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed')
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: RecoverStaleTasks :execrows
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running';
-- name: EnqueueIssue :execrows
-- The org is derived from the identity at the one place a task row is
-- written (an agent's assignment, else a membership, else the default org);
-- uniqueness follows the record's identity: two orgs may work the same
-- issue under their own identities, one may not work it twice.
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity, org_id, workspace_id)
VALUES (
$1, $2, $3, $4, $5, $6, $7,
COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $7),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
),
'default'
)
ON CONFLICT (org_id, identity, owner, repo, issue_number) DO NOTHING;
-- name: ClaimByIssue :one
-- The claim starts the attempt's output set empty, as ClaimNextTask does:
-- outputs is attempt-scoped (docs/prds/workflow-call-outputs.md).
UPDATE tasks SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now()
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
RETURNING *;
-- name: BeginRemediationTask :execrows
-- A remediation must continue the branch its pull request lives on, so it
-- records that dispatch mode explicitly (taskstate.RetryContinuePushedWork)
-- rather than leaving prepareWorkspace to infer it from the workflow.
UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', park_class = 'needs_human', review_payload = $2, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now()
WHERE id = $1 AND status = 'pr_open';
-- name: AppendPendingReview :execrows
-- A distinct review that arrived while a remediation owned the task. The two
-- containment tests are the dedup: a re-delivered reaction, or a poll that
-- re-publishes a review already queued, appends nothing, and one already
-- active in review_payload is not queued to run a second time.
UPDATE tasks SET pending_reviews = CASE
WHEN review_payload = $2::text THEN pending_reviews
WHEN pending_reviews @> jsonb_build_array($2::text) THEN pending_reviews
ELSE pending_reviews || jsonb_build_array($2::text)
END,
updated_at = now()
WHERE id = $1 AND status IN ('queued', 'running') AND workflow = 'remediate';
-- name: PromotePendingReview :execrows
-- The run that owned the task returned it to pr_open with reviews still
-- waiting: the oldest becomes the active payload and the task is queued
-- again, so it is remediated after the run that just finished.
UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '',
park_class = 'needs_human',
review_payload = pending_reviews->>0, pending_reviews = pending_reviews - 0,
retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}',
updated_at = now()
WHERE id = $1 AND status = 'pr_open' AND workflow = 'remediate'
AND jsonb_array_length(pending_reviews) > 0;
-- name: LockReviewUnits :one
-- The remediation's review units, locked so a comment merge cannot race the
-- claim that freezes the active unit or the promotion of a pending one.
SELECT status, workflow, review_payload, pending_reviews::text AS pending_reviews
FROM tasks WHERE id = $1 FOR UPDATE;
-- name: SetReviewUnits :exec
UPDATE tasks SET review_payload = $2, pending_reviews = CAST(CAST(sqlc.arg(pending_reviews) AS text) AS jsonb), updated_at = now()
WHERE id = $1;
-- name: SetReviewCursors :execrows
UPDATE tasks SET review_cursor = $2, watch_comment_id = $3, updated_at = now()
WHERE id = $1 AND status = 'pr_open';
-- name: RequeueTask :execrows
UPDATE tasks SET status = 'queued',
workflow = CASE WHEN @workflow::text = '' THEN workflow ELSE @workflow::text END,
park_reason = '', park_class = 'needs_human', updated_at = now()
WHERE id = @id AND status = @from_status;
-- name: RetryTask :execrows
UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
workflow = CASE WHEN @workflow::text = '' THEN workflow ELSE @workflow::text END,
retry_mode = @retry_mode::text,
resume_from = @resume_from::text, resume_results = @resume_results::jsonb,
park_reason = '', park_class = 'needs_human', updated_at = now()
WHERE id = @id AND status = @from_status;
-- name: RespondReviewGateTask :execrows
-- The review gate response write (docs/prds/pr-review-operator-response.md,
-- Decision 2): one guarded requeue beside RetryTask/BeginRemediation. The
-- from-status guard and the transition table live in the store's transaction
-- around this statement; the rereview cap is enforced HERE, in the row, so
-- two simultaneous re-reviews cannot spend a round the cap forbids. An
-- approve passes rereview=false and bumps nothing.
UPDATE tasks SET status = 'queued',
review_gate = @gate::text,
rereview_rounds = rereview_rounds + CASE WHEN @rereview::bool THEN 1 ELSE 0 END,
park_reason = '', park_class = 'needs_human', updated_at = now()
WHERE id = @id AND status = @from_status
AND (@rereview::bool = false OR rereview_rounds < @cap::bigint);
-- name: ArchiveTaskDelete :execrows
DELETE FROM tasks WHERE id = $1 AND status = $2;
-- name: ListOpenPRs :many
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt, review_cursor, watch_comment_id, outputs
FROM tasks WHERE status = 'pr_open' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));