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
This commit is contained in:
sam authored and GitHub committed 2026-10-12 03:24:04 +11:00
1 parent 8540c9f7c8
commit f34f60f9c7
60 files changed
+1695 -828

No files matched your search

@@ -8,6 +8,8 @@ package postgresdb
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const claimIdentitySubject = `-- name: ClaimIdentitySubject :one
@@ -75,6 +77,33 @@ func (q *Queries) GetIdentity(ctx context.Context, id string) (Identity, error)
return i, err
}
const getVisibleIdentity = `-- name: GetVisibleIdentity :one
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
FROM identities WHERE id = $1 AND ($2::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $2)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $2))
`
type GetVisibleIdentityParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) GetVisibleIdentity(ctx context.Context, arg GetVisibleIdentityParams) (Identity, error) {
row := q.db.QueryRow(ctx, getVisibleIdentity, arg.ID, arg.ScopeOrg)
var i Identity
err := row.Scan(
&i.ID,
&i.Kind,
&i.DisplayName,
&i.Lifecycle,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertIdentity = `-- name: InsertIdentity :exec
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -166,15 +195,21 @@ func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEve
const listIdentities = `-- name: ListIdentities :many
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
FROM identities ORDER BY created_at, id
FROM identities
WHERE ($1::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $1)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $1))
ORDER BY created_at, id
`
// Identity persistence. The domain owns lifecycle semantics (internal/domain/
// identity); these queries are the durable half of that contract. Aliases are
// unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
// the SQLite schema's COLLATE NOCASE.
func (q *Queries) ListIdentities(ctx context.Context) ([]Identity, error) {
rows, err := q.db.Query(ctx, listIdentities)
// A scoped read sees the identities that are members of, or agents assigned
// to, its org.
func (q *Queries) ListIdentities(ctx context.Context, scopeOrg pgtype.Text) ([]Identity, error) {
rows, err := q.db.Query(ctx, listIdentities, scopeOrg)
if err != nil {
return nil, err
}
@@ -240,11 +275,18 @@ func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubj
const resolveIdentityAlias = `-- name: ResolveIdentityAlias :one
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
WHERE lower(a.alias) = lower($1)
WHERE lower(a.alias) = lower($1) AND ($2::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = i.id AND m.org_id = $2)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = i.id AND a.org_id = $2))
`
func (q *Queries) ResolveIdentityAlias(ctx context.Context, lower string) (Identity, error) {
row := q.db.QueryRow(ctx, resolveIdentityAlias, lower)
type ResolveIdentityAliasParams struct {
Alias string
ScopeOrg pgtype.Text
}
func (q *Queries) ResolveIdentityAlias(ctx context.Context, arg ResolveIdentityAliasParams) (Identity, error) {
row := q.db.QueryRow(ctx, resolveIdentityAlias, arg.Alias, arg.ScopeOrg)
var i Identity
err := row.Scan(
&i.ID,