fix(access): check the policy version before each decision

archied, archie-ui and the tool gateway re-read the stored policies on a 30s
ticker, so a forbid written seconds earlier did not stop a run. One
instance-wide revision now moves inside the transaction of every policy write,
and a decision reads it first and rebuilds the engine only when it moved.

A version read that fails, or a policy list that fails after a bump, refuses
with ErrChainUnavailable instead of standing on the last chain, so three call
sites now treat unavailable as availability rather than denial: dispatch leaves
the capture for the next cycle, a task start parks transient, and the tool
gateway reports the tool as unavailable.
This commit is contained in:
sam committed 2026-10-25 16:21:01 +11:00
1 parent 536155752c
commit 75c1dbf83c
25 files changed
+1228 -797

No files matched your search

@@ -11,6 +11,33 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const accessPolicyRevision = `-- name: AccessPolicyRevision :one
SELECT version FROM access_policy_revision
`
func (q *Queries) AccessPolicyRevision(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, accessPolicyRevision)
var version int64
err := row.Scan(&version)
return version, err
}
const bumpAccessPolicyRevision = `-- name: BumpAccessPolicyRevision :one
INSERT INTO access_policy_revision (only_row, version) VALUES (true, 1)
ON CONFLICT (only_row)
DO UPDATE SET version = access_policy_revision.version + 1, updated_at = now()
RETURNING version
`
// The instance-wide version. It moves inside the transaction of every policy
// write, so it changes exactly when the policy snapshot changes.
func (q *Queries) BumpAccessPolicyRevision(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, bumpAccessPolicyRevision)
var version int64
err := row.Scan(&version)
return version, err
}
const countShippedOrgPolicies = `-- name: CountShippedOrgPolicies :one
SELECT count(*) FROM access_policies
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[])