Merge branch 'main' into worktree-agent-a6a4a1df2423fa389

This commit is contained in:
sam committed 2026-11-22 11:18:05 +11:00
commit bc56b71539
46 files changed
+2695 -818

No files matched your search

+701
View File
@@ -1,5 +1,26 @@
components:
schemas:
AddMemberInputBody:
additionalProperties: false
properties:
display_name:
description: Name shown for a new person; defaults to the email or subject
type: string
email:
description: The person's email; give this or a subject
type: string
role:
description: The role to hold
type: string
subject:
description: The subject the identity provider asserts; give this or an email
type: string
workspace:
description: Scope the membership to this workspace; empty is organisation-wide
type: string
required:
- role
type: object
AnswerInputBody:
additionalProperties: false
properties:
@@ -601,6 +622,28 @@ components:
type: string
writeOnly: true
type: object
ConnectorsHealth:
additionalProperties: false
properties:
contract:
description: connector, secretengine, guard or mcp
type: string
error:
type: string
failed_at:
format: date-time
type:
- string
- "null"
healthy:
type: boolean
name:
type: string
required:
- name
- contract
- healthy
type: object
ConnectorsMcpRequest:
additionalProperties: false
properties:
@@ -1286,6 +1329,60 @@ components:
- id
- token
type: object
GovernanceDenial:
additionalProperties: false
properties:
action:
type: string
at:
format: date-time
type: string
count:
format: int64
type: integer
kind:
type: string
level:
type: string
org:
type: string
policies:
items:
type: string
type:
- array
- "null"
principal:
type: string
resource:
type: string
required:
- principal
- org
- action
- kind
- resource
- level
- policies
- at
- count
type: object
GovernanceDenialsBody:
additionalProperties: false
properties:
denials:
items:
$ref: "#/components/schemas/GovernanceDenial"
type:
- array
- "null"
more:
description: Whether older refusals match beyond this page
type: boolean
required:
- denials
- more
type: object
GovernanceIdentity:
additionalProperties: false
properties:
@@ -1327,6 +1424,67 @@ components:
- url
- expires_at
type: object
GovernanceMember:
additionalProperties: false
properties:
display_name:
type: string
identity_id:
type: string
kind:
type: string
last_sign_in:
format: date-time
type: string
role:
type: string
status:
type: string
workspace_id:
type: string
required:
- identity_id
- kind
- display_name
- role
- status
type: object
GovernanceMembersBody:
additionalProperties: false
properties:
members:
items:
$ref: "#/components/schemas/GovernanceMember"
type:
- array
- "null"
required:
- members
type: object
GovernanceMembership:
additionalProperties: false
properties:
identity_id:
type: string
org_id:
type: string
role:
type: string
workspace_id:
type: string
required:
- identity_id
- org_id
- role
type: object
GovernanceMembershipBody:
additionalProperties: false
properties:
membership:
$ref: "#/components/schemas/GovernanceMembership"
required:
- membership
type: object
GovernanceModel:
additionalProperties: false
properties:
@@ -1505,6 +1663,58 @@ components:
- expires_at
- last_used_at
type: object
GovernancePoliciesBody:
additionalProperties: false
properties:
policies:
items:
$ref: "#/components/schemas/GovernancePolicy"
type:
- array
- "null"
shipped:
description: IDs of the policies Archie ships, which a reset restores
items:
type: string
type:
- array
- "null"
required:
- policies
- shipped
type: object
GovernancePolicy:
additionalProperties: false
properties:
id:
type: string
level:
type: string
object_id:
type: string
object_kind:
type: string
org_id:
type: string
text:
type: string
workspace_id:
type: string
required:
- id
- level
- text
type: object
GovernancePolicyVersionBody:
additionalProperties: false
properties:
version:
description: The policy chain's new revision
format: int64
type: integer
required:
- version
type: object
GovernanceRegistration:
additionalProperties: false
properties:
@@ -2508,6 +2718,31 @@ components:
- trigger
- filter
type: object
RunsRunCounts:
additionalProperties: false
properties:
parked:
format: int64
type: integer
queued:
format: int64
type: integer
running:
format: int64
type: integer
waiting:
format: int64
type: integer
waiting_approval:
format: int64
type: integer
required:
- queued
- running
- waiting
- waiting_approval
- parked
type: object
RunsRunView:
additionalProperties: false
properties:
@@ -2855,6 +3090,18 @@ components:
required:
- text
type: object
SetMemberInputBody:
additionalProperties: false
properties:
role:
description: The role to hold
type: string
workspace:
description: Scope the membership to this workspace; empty is organisation-wide
type: string
required:
- role
type: object
SignInInputBody:
additionalProperties: false
properties:
@@ -2899,6 +3146,105 @@ components:
- admin_token_set
- sealing_identity_set
type: object
SystemCheck:
additionalProperties: false
properties:
error:
type: string
healthy:
type: boolean
name:
type: string
required:
- name
- healthy
type: object
SystemFailure:
additionalProperties: false
properties:
at:
format: date-time
type: string
cause:
type: string
run_id:
type: string
source:
description: A connector name, the runner, or a workflow
type: string
required:
- source
- cause
- at
type: object
SystemHealth:
additionalProperties: false
properties:
checked_at:
format: date-time
type: string
checks:
items:
$ref: "#/components/schemas/SystemCheck"
type:
- array
- "null"
connectors:
items:
$ref: "#/components/schemas/ConnectorsHealth"
type:
- array
- "null"
healthy:
type: boolean
last_failure:
$ref: "#/components/schemas/SystemFailure"
runner:
$ref: "#/components/schemas/SystemRunner"
version:
type: string
waiting:
$ref: "#/components/schemas/SystemWaiting"
required:
- version
- healthy
- checked_at
- checks
- connectors
- runner
type: object
SystemRunner:
additionalProperties: false
properties:
error:
type: string
error_at:
format: date-time
type:
- string
- "null"
healthy:
type: boolean
workers:
format: int64
type: integer
required:
- healthy
- workers
type: object
SystemWaiting:
additionalProperties: false
properties:
approvals:
description: Approval requests waiting for an answer
format: int64
type: integer
runs:
$ref: "#/components/schemas/RunsRunCounts"
required:
- approvals
- runs
type: object
TextInputBody:
additionalProperties: false
properties:
@@ -2946,6 +3292,147 @@ info:
version: dev
openapi: 3.1.0
paths:
/api/access/denials:
get:
operationId: list-access-denials
parameters:
- description: Only refusals of this identity
explode: false
in: query
name: principal
schema:
description: Only refusals of this identity
type: string
- description: Only refusals of this action
explode: false
in: query
name: action
schema:
description: Only refusals of this action
type: string
- description: Only refusals on this resource kind
explode: false
in: query
name: kind
schema:
description: Only refusals on this resource kind
type: string
- description: Only refusals first recorded at or after this time
explode: false
in: query
name: since
schema:
description: Only refusals first recorded at or after this time
format: date-time
type: string
- description: Only refusals first recorded before this time
explode: false
in: query
name: until
schema:
description: Only refusals first recorded before this time
format: date-time
type: string
- description: How many refusals to return
explode: false
in: query
name: limit
schema:
default: 100
description: How many refusals to return
format: int64
maximum: 1000
minimum: 1
type: integer
- description: How many of the newest matching refusals to skip
explode: false
in: query
name: offset
schema:
default: 0
description: How many of the newest matching refusals to skip
format: int64
minimum: 0
type: integer
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernanceDenialsBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: List the requests access policies refused
tags:
- Access
/api/access/policies:
delete:
operationId: delete-access-policy
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/GovernancePolicy"
required: true
responses:
"204":
description: No Content
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Delete an access policy
tags:
- Access
get:
operationId: list-access-policies
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernancePoliciesBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: List the organisation's access policies
tags:
- Access
put:
operationId: put-access-policy
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/GovernancePolicy"
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernancePolicyVersionBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Create or replace an access policy
tags:
- Access
/api/account/channels:
get:
operationId: list-channel-links
@@ -5084,6 +5571,201 @@ paths:
summary: List the organisation's name changes
tags:
- Organisations
/api/orgs/{id}/members:
get:
operationId: list-org-members
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernanceMembersBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: List an organisation's members
tags:
- Access
post:
operationId: add-org-member
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/AddMemberInputBody"
required: true
responses:
"201":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernanceMembershipBody"
description: Created
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Add a person by email or provider subject
tags:
- Access
/api/orgs/{id}/members/{identity}:
delete:
operationId: remove-org-member
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
- description: Identity ID of the member
in: path
name: identity
required: true
schema:
description: Identity ID of the member
type: string
- description: Remove the membership scoped to this workspace; empty removes the organisation-wide one
explode: false
in: query
name: workspace
schema:
description: Remove the membership scoped to this workspace; empty removes the organisation-wide one
type: string
responses:
"204":
description: No Content
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Remove a membership
tags:
- Access
put:
operationId: set-org-member
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
- description: Identity ID of the member
in: path
name: identity
required: true
schema:
description: Identity ID of the member
type: string
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/SetMemberInputBody"
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GovernanceMembershipBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Give a member a role
tags:
- Access
/api/orgs/{id}/members/{identity}/reactivate:
post:
operationId: reactivate-org-member
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
- description: Identity ID of the member
in: path
name: identity
required: true
schema:
description: Identity ID of the member
type: string
responses:
"204":
description: No Content
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Reactivate a member's identity
tags:
- Access
/api/orgs/{id}/members/{identity}/suspend:
post:
operationId: suspend-org-member
parameters:
- description: Organisation ID
in: path
name: id
required: true
schema:
description: Organisation ID
type: string
- description: Identity ID of the member
in: path
name: identity
required: true
schema:
description: Identity ID of the member
type: string
responses:
"204":
description: No Content
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Suspend a member's identity
tags:
- Access
/api/orgs/{id}/workspaces:
get:
operationId: list-org-workspaces
@@ -5781,6 +6463,25 @@ paths:
summary: Get the bootstrap values, which only the config file sets
tags:
- System
/api/system/health:
get:
operationId: get-system-health
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/SystemHealth"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Report whether each module, connector and the runner is working, and what waits on a person
tags:
- System
/api/tokens:
get:
operationId: list-personal-tokens
+1
View File
@@ -44,6 +44,7 @@ func (m apiModules) register(api huma.API) {
m.settings.Register(api)
m.settings.RegisterAccounts(api)
m.settings.RegisterOrganisations(api)
m.settings.RegisterAccess(api)
m.conns.Register(api)
m.system.Register(api)
m.tell.Register(api)
+43
View File
@@ -0,0 +1,43 @@
package archie
import (
"context"
"fmt"
"slices"
"github.com/samcharles93/archie-core/internal/platform/health"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/runs"
"github.com/samcharles93/archie-core/modules/system"
)
// healthSources joins what each module knows about itself into the sources the
// system health report reads. The report asks them on demand, so nothing here
// polls or keeps state.
func healthSources(checks *health.Registry, conns *connectors.Module, engine *runs.Engine, allow *approvals.Module) system.Sources {
return system.Sources{
Checks: func(ctx context.Context) []health.Result {
return slices.DeleteFunc(checks.Results(ctx), func(r health.Result) bool {
// The report lists each connector with its own cause.
return r.Name == "connectors"
})
},
Connectors: conns.Health,
Runner: engine.Runner,
Waiting: func(ctx context.Context) (system.Waiting, error) {
pending, err := allow.Pending(ctx)
if err != nil {
return system.Waiting{}, fmt.Errorf("list pending approvals: %w", err)
}
counts, err := engine.Counts(ctx, orgID)
if err != nil {
return system.Waiting{}, err
}
return system.Waiting{Approvals: len(pending), Runs: counts}, nil
},
LastFailedRun: func(ctx context.Context) (*runs.RunView, error) {
return engine.LastFailed(ctx, orgID)
},
}
}
+1
View File
@@ -208,6 +208,7 @@ func wire(log *slog.Logger, configPath string, cfg Config, db *sqlx.DB) (*binary
}
return nil
})
sys.UseHealth(healthSources(checks, conns, engine, allow))
apiModules{
settings: settings, conns: conns, system: sys, tell: tell, allow: allow, classes: classes, policies: policies,
hear: hear, judge: judge, engine: engine, chat: talk, drafts: drafts.NewModule(db, log.With("module", "drafts"), orgID, hear, engine, policies),
+24 -3
View File
@@ -41,14 +41,35 @@ func (r *Registry) Routes(mux *http.ServeMux) {
mux.HandleFunc("GET /readyz", r.ready)
}
// Result is the outcome of one readiness check; Error is empty when it passed.
type Result struct {
Name string `json:"name"`
Error string `json:"error,omitzero"`
}
// Results runs every check and returns the outcomes in name order. A failing
// check does not stop the others.
func (r *Registry) Results(ctx context.Context) []Result {
names := r.names()
out := make([]Result, 0, len(names))
for _, name := range names {
res := Result{Name: name}
if err := r.check(name)(ctx); err != nil {
res.Error = err.Error()
}
out = append(out, res)
}
return out
}
func (r *Registry) ready(w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second)
defer cancel()
failures := map[string]string{}
for _, name := range r.names() {
if err := r.check(name)(ctx); err != nil {
failures[name] = err.Error()
for _, res := range r.Results(ctx) {
if res.Error != "" {
failures[res.Name] = res.Error
}
}
w.Header().Set("Content-Type", "application/json")
@@ -0,0 +1,15 @@
-- +goose Up
-- The members page shows when each person last signed in. Sessions expire and
-- are purged, so the time is kept on the account.
ALTER TABLE governance.accounts ADD COLUMN last_sign_in_at timestamptz;
UPDATE governance.accounts a
SET last_sign_in_at = s.last
FROM (
SELECT identity_id, max(issued_at) AS last
FROM governance.account_sessions GROUP BY identity_id
) s
WHERE s.identity_id = a.identity_id;
-- +goose Down
ALTER TABLE governance.accounts DROP COLUMN last_sign_in_at;
+73
View File
@@ -0,0 +1,73 @@
package connectors
import (
"maps"
"slices"
"time"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
// Health is what the module knows about one accepted package right now. It is
// read from memory, so a database outage does not hide it.
type Health struct {
Name string `json:"name"`
Contract string `json:"contract" doc:"connector, secretengine, guard or mcp"`
Healthy bool `json:"healthy"`
// Error is why the package is not running.
Error string `json:"error,omitzero"`
// FailedAt is when the package last failed to start. A process that exited
// after it started has no time.
FailedAt *time.Time `json:"failed_at,omitzero"`
}
// Health lists every package the module launched, in name order: those running,
// those that failed to start, and those whose process has since exited or whose
// MCP server stopped answering.
func (m *Module) Health() []Health {
m.mu.RLock()
failed := maps.Clone(m.failed)
failedAt := maps.Clone(m.failedAt)
contracts := maps.Clone(m.contracts)
servers := maps.Clone(m.servers)
names := map[string]string{}
for name := range m.running {
names[name] = store.ContractConnector
}
for name := range m.engines {
names[name] = store.ContractSecretEngine
}
for name := range m.guards {
names[name] = store.ContractGuard
}
for name := range servers {
names[name] = store.ContractMCP
}
for name := range failed {
if _, ok := names[name]; !ok {
names[name] = contracts[name]
}
}
m.mu.RUnlock()
out := make([]Health, 0, len(names))
for _, name := range slices.Sorted(maps.Keys(names)) {
h := Health{Name: name, Contract: names[name], Healthy: true}
server, isServer := servers[name]
switch {
case failed[name] != "":
h.Healthy, h.Error = false, failed[name]
if at, ok := failedAt[name]; ok {
h.FailedAt = &at
}
case isServer:
if err := server.Ready(); err != nil {
h.Healthy, h.Error = false, err.Error()
}
case !m.host.Alive(name):
h.Healthy, h.Error = false, "process exited"
}
out = append(out, h)
}
return out
}
+22 -1
View File
@@ -15,6 +15,7 @@ import (
"sort"
"strings"
"sync"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/jmoiron/sqlx"
@@ -47,6 +48,11 @@ type Module struct {
servers map[string]*Server
stops map[string]context.CancelFunc
failed map[string]string
// failedAt is when a name last failed to start, and contracts is what each
// launched name is. Both are read only for health, so an entry that outlives
// its failure does no harm.
failedAt map[string]time.Time
contracts map[string]string
// recheck, when set, re-checks saved configuration against a connector's
// new declarations; an error refuses them.
recheck Recheck
@@ -452,7 +458,7 @@ func (m *Module) acceptAs(ctx context.Context, in *packageInput, contract string
// name runs one package: whatever ran under it before is stopped first, so a
// connector, a secret engine and an MCP server never share a name.
func (m *Module) launch(ctx context.Context, a store.Accepted) error {
m.release(a.Name)
m.releaseAs(a.Name, a.Contract)
if a.Contract == store.ContractMCP {
return m.startServer(ctx, a)
}
@@ -613,12 +619,27 @@ func (m *Module) release(name string) {
}
}
// releaseAs stops whatever ran under name and records what it is about to be.
func (m *Module) releaseAs(name, contract string) {
m.release(name)
m.mu.Lock()
defer m.mu.Unlock()
if m.contracts == nil {
m.contracts = map[string]string{}
}
m.contracts[name] = contract
}
func (m *Module) fail(ctx context.Context, name string, err error) error {
m.mu.Lock()
delete(m.running, name)
delete(m.engines, name)
delete(m.guards, name)
m.failed[name] = err.Error()
if m.failedAt == nil {
m.failedAt = map[string]time.Time{}
}
m.failedAt[name] = time.Now()
m.mu.Unlock()
m.log.ErrorContext(ctx, "extension failed to start", "extension", name, "error", err)
return err
+173
View File
@@ -0,0 +1,173 @@
package governance
import (
"context"
"errors"
"log/slog"
"net/http"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
)
// policyAdmin is the stored policy chain the routes edit. PutPolicy and
// DeletePolicy refuse a change that leaves an org with nobody able to manage
// policies.
type policyAdmin interface {
ListPolicies(context.Context) ([]Policy, error)
PutPolicy(context.Context, Policy) (int64, error)
DeletePolicy(context.Context, Policy) error
}
// denialSearch reads the recorded refusals.
type denialSearch interface {
SearchDenials(context.Context, DenialQuery) ([]Denial, error)
}
// accessAPI serves the access policies and the record of refused requests.
type accessAPI struct {
policies policyAdmin
denials denialSearch
log *slog.Logger
}
func (m *Module) access() *accessAPI {
return &accessAPI{policies: m, denials: m, log: m.log}
}
type policiesBody struct {
Policies []Policy `json:"policies"`
Shipped []string `json:"shipped" doc:"IDs of the policies Archie ships, which a reset restores"`
}
type policyVersionBody struct {
Version int64 `json:"version" doc:"The policy chain's new revision"`
}
type policyInput struct {
Body Policy
}
type denialsInput struct {
Principal string `query:"principal" doc:"Only refusals of this identity"`
Action string `query:"action" doc:"Only refusals of this action"`
Kind string `query:"kind" doc:"Only refusals on this resource kind"`
Since time.Time `query:"since" doc:"Only refusals first recorded at or after this time"`
Until time.Time `query:"until" doc:"Only refusals first recorded before this time"`
Limit int `query:"limit" default:"100" minimum:"1" maximum:"1000" doc:"How many refusals to return"`
Offset int `query:"offset" default:"0" minimum:"0" doc:"How many of the newest matching refusals to skip"`
}
type denialsBody struct {
Denials []Denial `json:"denials"`
More bool `json:"more" doc:"Whether older refusals match beyond this page"`
}
func (m *Module) registerPolicies(api huma.API, op func(id, method, path, summary string, status int) huma.Operation) {
a := m.access()
httpapi.Register(api, httpapi.Authorized(ActionRead, KindPolicy), op("list-access-policies", http.MethodGet, "/api/access/policies", "List the organisation's access policies", 0), a.listPolicies)
httpapi.Register(api, httpapi.Authorized(ActionManagePolicies, KindPolicy), op("put-access-policy", http.MethodPut, "/api/access/policies", "Create or replace an access policy", 0), a.putPolicy)
httpapi.Register(api, httpapi.Authorized(ActionManagePolicies, KindPolicy), op("delete-access-policy", http.MethodDelete, "/api/access/policies", "Delete an access policy", http.StatusNoContent), a.deletePolicy)
}
func (m *Module) registerDenials(api huma.API, op func(id, method, path, summary string, status int) huma.Operation) {
a := m.access()
httpapi.Register(api, httpapi.Authorized(ActionReadAudit, KindAudit), op("list-access-denials", http.MethodGet, "/api/access/denials", "List the requests access policies refused", 0), a.listDenials)
}
func (a *accessAPI) listPolicies(ctx context.Context, _ *struct{}) (*httpapi.Reply[policiesBody], error) {
p, err := caller(ctx)
if err != nil {
return nil, err
}
stored, err := a.policies.ListPolicies(ctx)
if err != nil {
return nil, a.failed(ctx, "read the policies", err)
}
out := make([]Policy, 0, len(stored))
for _, s := range stored {
if s.Level == LevelInstance && p.InstanceAdmin() || s.Level != LevelInstance && s.OrgID == p.Org {
out = append(out, s)
}
}
return httpapi.OK(policiesBody{Policies: out, Shipped: shippedOrgPolicyIDs()}), nil
}
func (a *accessAPI) putPolicy(ctx context.Context, in *policyInput) (*httpapi.Reply[policyVersionBody], error) {
value, err := a.scoped(ctx, in.Body)
if err != nil {
return nil, err
}
if err := NewEngine(nil).Validate(value); err != nil {
return nil, a.failed(ctx, "save the policy", err)
}
version, err := a.policies.PutPolicy(ctx, value)
if err != nil {
return nil, a.failed(ctx, "save the policy", err)
}
return httpapi.OK(policyVersionBody{Version: version}), nil
}
func (a *accessAPI) deletePolicy(ctx context.Context, in *policyInput) (*struct{}, error) {
value, err := a.scoped(ctx, in.Body)
if err != nil {
return nil, err
}
if err := a.policies.DeletePolicy(ctx, value); err != nil {
return nil, a.failed(ctx, "delete the policy", err)
}
return nil, nil
}
// scoped pins a policy to the caller's organisation, since no request field
// chooses where a policy applies. Instance policies span every organisation, so
// only an instance administrator edits them.
func (a *accessAPI) scoped(ctx context.Context, value Policy) (Policy, error) {
p, err := caller(ctx)
if err != nil {
return Policy{}, err
}
if value.Level == LevelInstance {
if !p.InstanceAdmin() {
return Policy{}, huma.Error403Forbidden("Only an instance owner can change instance policies.")
}
value.OrgID, value.WorkspaceID, value.ObjectKind, value.ObjectID = "", "", "", ""
return value, nil
}
value.OrgID = p.Org
return value, nil
}
func (a *accessAPI) listDenials(ctx context.Context, in *denialsInput) (*httpapi.Reply[denialsBody], error) {
p, err := caller(ctx)
if err != nil {
return nil, err
}
found, err := a.denials.SearchDenials(ctx, DenialQuery{
Org: p.Org, Principal: IdentityID(in.Principal), Action: Action(in.Action), Kind: ResourceKind(in.Kind),
Since: in.Since, Until: in.Until, Limit: in.Limit + 1, Offset: in.Offset,
})
if err != nil {
return nil, a.failed(ctx, "read the denials", err)
}
more := len(found) > in.Limit
return httpapi.OK(denialsBody{Denials: found[:min(len(found), in.Limit)], More: more}), nil
}
// failed answers a policy failure with the status a person can act on: the
// Cedar error for a policy that does not compile, a conflict for a change that
// would lock every owner out.
func (a *accessAPI) failed(ctx context.Context, what string, err error) error {
switch {
case errors.Is(err, ErrPolicyLockout):
return huma.Error409Conflict("That change would leave no owner able to manage policies.")
case errors.Is(err, ErrPolicyNotFound):
return huma.Error404NotFound("That policy does not exist.")
case errors.Is(err, ErrInvalidPolicy), errors.Is(err, ErrInvalidLevel), errors.Is(err, ErrPolicyInvalidText):
return huma.Error400BadRequest(err.Error())
}
a.log.ErrorContext(ctx, "access operation failed", "operation", what, "err", err)
return huma.Error503ServiceUnavailable("Couldn't " + what + ". Try again shortly.")
}
+91
View File
@@ -0,0 +1,91 @@
package governance
import (
"context"
"log/slog"
"net/http"
"slices"
"strings"
"testing"
)
// fakePolicies applies the same validation and lockout rule as the module's
// store-backed methods, against policies held in memory.
type fakePolicies struct {
stored []Policy
puts []Policy
org []Principal
}
func (f *fakePolicies) ListPolicies(context.Context) ([]Policy, error) { return f.stored, nil }
func (f *fakePolicies) PutPolicy(ctx context.Context, p Policy) (int64, error) {
err := CheckPolicyChange(ctx, f.stored, func(cur []Policy) []Policy {
return append(slices.DeleteFunc(cur, func(c Policy) bool { return samePolicy(c, p) }), p)
}, []Org{{ID: DefaultOrgID}}, func(context.Context, OrgID) ([]Principal, error) { return f.org, nil })
if err != nil {
return 0, err
}
f.puts = append(f.puts, p)
return 1, nil
}
func (f *fakePolicies) DeletePolicy(context.Context, Policy) error { return nil }
func policyRoutes(f *fakePolicies) *accessAPI {
return &accessAPI{policies: f, log: slog.New(slog.DiscardHandler)}
}
// TestPolicyEdits guards what a policy edit may do: an invalid policy is
// refused with the Cedar error and never stored, a change that leaves no owner
// able to manage policies is refused, and a policy lands in the caller's own
// organisation, with instance policies reserved for the instance owner.
func TestPolicyEdits(t *testing.T) {
owner := actor("root", RoleOwner)
otherOrgOwner := Principal{
IdentityID: "pat", Kind: KindUser, Org: "acme",
Memberships: []Membership{{IdentityID: "pat", OrgID: "acme", Role: RoleOwner}},
}
withOwner := func() *fakePolicies {
return &fakePolicies{stored: ShippedOrgPolicies(DefaultOrgID), org: []Principal{owner}}
}
policy := func(level Level, id, text string) Policy {
return Policy{ID: id, Level: level, OrgID: "elsewhere", Text: text}
}
const harmless = `permit(principal, action == Archie::Action::"read", resource);`
tests := []struct {
name string
by Principal
policy Policy
want int
wantError string
stored bool
wantOrg OrgID
}{
{name: "a valid policy", by: owner, policy: policy(LevelOrg, "p", harmless), want: http.StatusOK, stored: true, wantOrg: DefaultOrgID},
{name: "text that does not compile", by: owner, policy: policy(LevelOrg, "p", `permit(principal, action == Archie::Action::"nope", resource);`), want: http.StatusBadRequest, wantError: "nope"},
{name: "no policy text", by: owner, policy: policy(LevelOrg, "p", " "), want: http.StatusBadRequest, wantError: "no text"},
{name: "a forbid that locks owners out", by: owner, policy: policy(LevelOrg, "lock", `forbid(principal, action == Archie::Action::"manage_policies", resource);`), want: http.StatusConflict},
{name: "an instance policy by an instance owner", by: owner, policy: policy(LevelInstance, "i", `permit(principal, action, resource);`), want: http.StatusOK, stored: true},
{name: "an instance policy by another organisation's owner", by: otherOrgOwner, policy: policy(LevelInstance, "i", harmless), want: http.StatusForbidden},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := withOwner()
_, err := policyRoutes(f).putPolicy(WithPrincipal(t.Context(), tt.by), &policyInput{Body: tt.policy})
if got := status(t, err); got != tt.want {
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
}
if tt.wantError != "" && !strings.Contains(err.Error(), tt.wantError) {
t.Fatalf("error %q does not carry the Cedar error %q", err, tt.wantError)
}
if stored := len(f.puts) > 0; stored != tt.stored {
t.Fatalf("stored = %v, want %v", stored, tt.stored)
}
if tt.wantOrg != "" && f.puts[0].OrgID != tt.wantOrg {
t.Fatalf("policy stored for org %q, want %q", f.puts[0].OrgID, tt.wantOrg)
}
})
}
}
+8 -3
View File
@@ -608,17 +608,22 @@ func (m *Module) capabilities(ctx context.Context, _ *struct{}) (*httpapi.Reply[
sections := map[string]bool{"chat": signedIn, "memory": signedIn}
for _, name := range []string{
"settings", "events", "workflows", "runs", "credentials", "extensions", "secret_engines", "models", "controls", "memory_admin",
"home", "system_status",
} {
sections[name] = p.InstanceAdmin()
}
for _, name := range []string{
"home", "tasks", "work_requests", "logs", "skills", "curators", "channels",
"tasks", "work_requests", "logs", "skills", "curators", "channels",
"captures", "mappings", "bindings", "workflow_editor", "extension_catalogue", "model_aliases",
"system_status", "task_execution", "organisations", "tools", "plugins", "container_runtime", "agent_profiles",
"org_members", "org_agents", "org_policies", "org_denials", "org_audit", "org_usage", "org_config",
"task_execution", "organisations", "tools", "plugins", "container_runtime", "agent_profiles",
"org_agents", "org_audit", "org_usage", "org_config",
} {
sections[name] = false
}
// The access pages are offered to those the policy chain lets use them.
sections["org_members"] = m.mayUse(ctx, p, ActionManageMembers, KindMember)
sections["org_policies"] = m.mayUse(ctx, p, ActionManagePolicies, KindPolicy)
sections["org_denials"] = m.mayUse(ctx, p, ActionReadAudit, KindAudit)
// Every member reads the organisation they belong to and its workspaces.
sections["org"] = signedIn
sections["org_workspaces"] = signedIn
+14
View File
@@ -97,3 +97,17 @@ func (m *Module) Allow(ctx context.Context, action, kind string) error {
}
return httpapi.ErrUnavailable
}
// mayUse reports whether p's grants cover action on a kind of resource in its
// org. It asks the chain without recording a denial, since a dashboard probing
// which pages to offer has not been refused anything.
func (m *Module) mayUse(ctx context.Context, p Principal, action Action, kind ResourceKind) bool {
chain := m.authz()
if r, ok := chain.(recording); ok {
chain = r.Authorizer
}
if chain == nil || p.IdentityID == "" {
return false
}
return chain.Authorize(ctx, p, action, Resource{Kind: kind, Org: p.Org}, AccessContext{Time: time.Now()}).Allowed
}
+5 -3
View File
@@ -61,6 +61,11 @@ func buildEntities(p Principal, r Resource) cedar.EntityMap {
attrs := types.RecordMap{
"org": types.String(resOrg),
"kind": types.String(string(r.Kind)),
// Always present: a policy that compares the state of a resource that
// has none would otherwise error and never match, so a shipped rule
// such as "not an owner's membership" would refuse the route guard's
// request, which names no state.
"state": types.String(r.State),
}
if r.Workspace != "" {
wsUID := types.NewEntityUID(EntityTypeWorkspace, types.String(r.Workspace))
@@ -78,9 +83,6 @@ func buildEntities(p Principal, r Resource) cedar.EntityMap {
if r.Owner != "" {
attrs["owner"] = types.String(string(r.Owner))
}
if r.State != "" {
attrs["state"] = types.String(r.State)
}
entities[resUID] = cedar.Entity{
UID: resUID,
Parents: cedar.NewEntityUIDSet(parents...),
@@ -306,6 +306,43 @@ func (s *Store) ListDenials(ctx context.Context, orgID string, limit int) ([]Den
return out, nil
}
// DenialFilter narrows SearchDenials. A zero field matches everything.
type DenialFilter struct {
OrgID string
Principal string
Action string
ResourceKind string
Since, Until time.Time
Limit int
Offset int
}
// SearchDenials returns an org's denials matching f, newest first.
func (s *Store) SearchDenials(ctx context.Context, f DenialFilter) ([]DenialRow, error) {
var out []DenialRow
if err := s.db.SelectContext(ctx, &out, `
SELECT id, org_id, principal, action, resource_kind, resource_id, level, policies, minute_at, count, created_at, updated_at
FROM governance.access_denials
WHERE org_id = $1
AND ($2 = '' OR principal = $2)
AND ($3 = '' OR action = $3)
AND ($4 = '' OR resource_kind = $4)
AND ($5::timestamptz IS NULL OR created_at >= $5)
AND ($6::timestamptz IS NULL OR created_at < $6)
ORDER BY id DESC LIMIT $7 OFFSET $8`,
f.OrgID, f.Principal, f.Action, f.ResourceKind, nullTime(f.Since), nullTime(f.Until), f.Limit, f.Offset); err != nil {
return nil, fmt.Errorf("search denials: %w", err)
}
return out, nil
}
func nullTime(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
// PruneDenials deletes up to batch denial rows created before cutoff.
func (s *Store) PruneDenials(ctx context.Context, cutoff time.Time, batch int) (int64, error) {
res, err := s.db.ExecContext(ctx, `
@@ -206,7 +206,8 @@ func recordFailedLogin(ctx context.Context, tx *sqlx.Tx, acc AccountRow) error {
func openSignInSessionTx(ctx context.Context, tx *sqlx.Tx, identityID, tokenHash string, expires time.Time) error {
if _, err := tx.ExecContext(ctx, `
UPDATE governance.accounts SET failed_attempts = 0, locked_until = NULL WHERE identity_id = $1`,
UPDATE governance.accounts SET failed_attempts = 0, locked_until = NULL, last_sign_in_at = now()
WHERE identity_id = $1`,
identityID); err != nil {
return err
}
@@ -278,6 +279,10 @@ func (s *Store) OpenAccountSession(ctx context.Context, identityID, tokenHash st
tokenHash, identityID, expires); err != nil {
return IdentityRow{}, err
}
if _, err := s.db.ExecContext(ctx, `
UPDATE governance.accounts SET last_sign_in_at = now() WHERE identity_id = $1`, identityID); err != nil {
return IdentityRow{}, err
}
return row, nil
}
+4 -1
View File
@@ -61,6 +61,8 @@ type MemberRow struct {
DisplayName string `db:"display_name"`
WorkspaceID sql.NullString `db:"workspace_id"`
Role string `db:"role"`
Lifecycle string `db:"lifecycle"`
LastSignIn sql.NullTime `db:"last_sign_in_at"`
}
// AgentAssignmentRow is an agent's owning org in governance.org_agents.
@@ -300,9 +302,10 @@ func (s *Store) EnsureMembership(ctx context.Context, m MembershipRow) error {
func (s *Store) ListMembers(ctx context.Context, orgID string) ([]MemberRow, error) {
var out []MemberRow
if err := s.db.SelectContext(ctx, &out, `
SELECT m.identity_id, i.kind, i.display_name, m.workspace_id, m.role
SELECT m.identity_id, i.kind, i.display_name, m.workspace_id, m.role, i.lifecycle, a.last_sign_in_at
FROM governance.memberships m
JOIN governance.identities i ON i.id = m.identity_id
LEFT JOIN governance.accounts a ON a.identity_id = m.identity_id
WHERE m.org_id = $1
ORDER BY m.created_at, m.identity_id, m.workspace_id`, orgID); err != nil {
return nil, fmt.Errorf("list members: %w", err)
+382
View File
@@ -0,0 +1,382 @@
package governance
import (
"context"
"crypto/rand"
"errors"
"log/slog"
"net/http"
"strings"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
)
// memberDirectory is what the members routes read and change.
type memberDirectory interface {
ListMembers(context.Context, OrgID) ([]Member, error)
EnsureMembership(context.Context, Membership) error
RemoveMembership(context.Context, Membership) error
Get(context.Context, IdentityID) (Identity, error)
Apply(context.Context, IdentityID, int64, IdentityCommand, IdentityAudit) (Identity, error)
}
// personInviter creates the identity of a person who has not signed in yet and
// binds it to the subject their identity provider will assert.
type personInviter interface {
ResolveSubject(context.Context, Subject) (Identity, error)
Create(context.Context, Identity, IdentityAudit) (Identity, error)
BindSubject(context.Context, IdentityID, Subject, IdentityAudit) error
AuthenticationSettings(context.Context) (AuthenticationSettings, error)
}
// memberAPI serves the organisation's members. The route guard decides whether
// the caller may manage members at all; each handler then asks the policy chain
// again with the role being granted or changed, which is how the shipped
// policies keep an admin from creating or altering an owner.
type memberAPI struct {
dir memberDirectory
people personInviter
authz func() Authorizer
log *slog.Logger
}
func (m *Module) members() *memberAPI {
return &memberAPI{dir: m, people: m, authz: m.authz, log: m.log}
}
type membersBody struct {
Members []Member `json:"members"`
}
type membershipBody struct {
Membership Membership `json:"membership"`
}
type memberPath struct {
ID string `path:"id" doc:"Organisation ID"`
Identity string `path:"identity" doc:"Identity ID of the member"`
}
type setMemberInput struct {
ID string `path:"id" doc:"Organisation ID"`
Identity string `path:"identity" doc:"Identity ID of the member"`
Body struct {
Role string `json:"role" doc:"The role to hold"`
Workspace string `json:"workspace,omitempty" doc:"Scope the membership to this workspace; empty is organisation-wide"`
}
}
type removeMemberInput struct {
ID string `path:"id" doc:"Organisation ID"`
Identity string `path:"identity" doc:"Identity ID of the member"`
Workspace string `query:"workspace" doc:"Remove the membership scoped to this workspace; empty removes the organisation-wide one"`
}
type addMemberInput struct {
ID string `path:"id" doc:"Organisation ID"`
Body struct {
Subject string `json:"subject,omitempty" doc:"The subject the identity provider asserts; give this or an email"`
Email string `json:"email,omitempty" doc:"The person's email; give this or a subject"`
DisplayName string `json:"display_name,omitempty" doc:"Name shown for a new person; defaults to the email or subject"`
Role string `json:"role" doc:"The role to hold"`
Workspace string `json:"workspace,omitempty" doc:"Scope the membership to this workspace; empty is organisation-wide"`
}
}
// RegisterAccess adds the organisation's members, its access policies and the
// record of refused requests.
func (m *Module) RegisterAccess(api huma.API) {
tags := []string{"Access"}
op := func(id, method, path, summary string, status int) huma.Operation {
return huma.Operation{
OperationID: id, Method: method, Path: path, Summary: summary,
Tags: tags, DefaultStatus: status, MaxBodyBytes: 64 << 10,
}
}
a := m.members()
httpapi.Register(api, httpapi.Authorized(ActionRead, KindMember), op("list-org-members", http.MethodGet, "/api/orgs/{id}/members", "List an organisation's members", 0), a.list)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("add-org-member", http.MethodPost, "/api/orgs/{id}/members", "Add a person by email or provider subject", http.StatusCreated), a.add)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("set-org-member", http.MethodPut, "/api/orgs/{id}/members/{identity}", "Give a member a role", 0), a.set)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("remove-org-member", http.MethodDelete, "/api/orgs/{id}/members/{identity}", "Remove a membership", http.StatusNoContent), a.remove)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("suspend-org-member", http.MethodPost, "/api/orgs/{id}/members/{identity}/suspend", "Suspend a member's identity", http.StatusNoContent), a.suspend)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("reactivate-org-member", http.MethodPost, "/api/orgs/{id}/members/{identity}/reactivate", "Reactivate a member's identity", http.StatusNoContent), a.reactivate)
m.registerPolicies(api, op)
m.registerDenials(api, op)
}
func (a *memberAPI) list(ctx context.Context, in *orgPath) (*httpapi.Reply[membersBody], error) {
if _, err := actingOrg(ctx, OrgID(in.ID)); err != nil {
return nil, err
}
values, err := a.dir.ListMembers(ctx, OrgID(in.ID))
if err != nil {
return nil, a.failed(ctx, "read the members", err)
}
return httpapi.OK(membersBody{Members: values}), nil
}
func (a *memberAPI) set(ctx context.Context, in *setMemberInput) (*httpapi.Reply[membershipBody], error) {
org, target := OrgID(in.ID), IdentityID(in.Identity)
p, err := actingOrg(ctx, org)
if err != nil {
return nil, err
}
want := Membership{IdentityID: target, OrgID: org, WorkspaceID: WorkspaceID(in.Body.Workspace), Role: Role(in.Body.Role)}
if err := checkGrantable(want); err != nil {
return nil, err
}
members, err := a.dir.ListMembers(ctx, org)
if err != nil {
return nil, a.failed(ctx, "read the members", err)
}
current, held := roleHeld(members, target, want.WorkspaceID)
if held && current != want.Role && strandsOwner(members, target, want.WorkspaceID, want.Role) {
return nil, a.failed(ctx, "change the role", ErrLastOwner)
}
if err := a.mayManage(ctx, p, org, want.Role); err != nil {
return nil, err
}
if held && current != want.Role {
if err := a.mayManage(ctx, p, org, current); err != nil {
return nil, err
}
}
if _, err := a.dir.Get(ctx, target); err != nil {
return nil, a.failed(ctx, "read the member", err)
}
if err := a.dir.EnsureMembership(ctx, want); err != nil {
return nil, a.failed(ctx, "change the role", err)
}
return httpapi.OK(membershipBody{Membership: want}), nil
}
func (a *memberAPI) remove(ctx context.Context, in *removeMemberInput) (*struct{}, error) {
org, target := OrgID(in.ID), IdentityID(in.Identity)
p, err := actingOrg(ctx, org)
if err != nil {
return nil, err
}
if target == SystemID {
return nil, a.failed(ctx, "remove the member", ErrSystemImmutable)
}
scope := WorkspaceID(in.Workspace)
members, err := a.dir.ListMembers(ctx, org)
if err != nil {
return nil, a.failed(ctx, "read the members", err)
}
current, held := roleHeld(members, target, scope)
if !held {
return nil, huma.Error404NotFound("That person is not a member.")
}
if strandsOwner(members, target, scope, "") {
return nil, a.failed(ctx, "remove the member", ErrLastOwner)
}
if err := a.mayManage(ctx, p, org, current); err != nil {
return nil, err
}
if err := a.dir.RemoveMembership(ctx, Membership{IdentityID: target, OrgID: org, WorkspaceID: scope}); err != nil {
return nil, a.failed(ctx, "remove the member", err)
}
return nil, nil
}
func (a *memberAPI) suspend(ctx context.Context, in *memberPath) (*struct{}, error) {
return a.transition(ctx, in, CommandSuspend)
}
func (a *memberAPI) reactivate(ctx context.Context, in *memberPath) (*struct{}, error) {
return a.transition(ctx, in, CommandReactivate)
}
// transition suspends or reactivates a member's identity. A suspended identity
// stops acting at once and keeps its memberships.
func (a *memberAPI) transition(ctx context.Context, in *memberPath, command CommandType) (*struct{}, error) {
org, target := OrgID(in.ID), IdentityID(in.Identity)
p, err := actingOrg(ctx, org)
if err != nil {
return nil, err
}
if target == SystemID {
return nil, a.failed(ctx, "change the member", ErrSystemImmutable)
}
members, err := a.dir.ListMembers(ctx, org)
if err != nil {
return nil, a.failed(ctx, "read the members", err)
}
current, held := roleHeld(members, target, "")
if !held {
return nil, huma.Error404NotFound("That person is not a member.")
}
if command == CommandSuspend && strandsOwner(members, target, "", "") {
return nil, a.failed(ctx, "suspend the member", ErrLastOwner)
}
if err := a.mayManage(ctx, p, org, current); err != nil {
return nil, err
}
value, err := a.dir.Get(ctx, target)
if err != nil {
return nil, a.failed(ctx, "read the member", err)
}
if _, err := a.dir.Apply(ctx, target, value.Version, IdentityCommand{Type: command}, memberAudit(p)); err != nil {
return nil, a.failed(ctx, "change the member", err)
}
return nil, nil
}
func (a *memberAPI) add(ctx context.Context, in *addMemberInput) (*httpapi.Reply[membershipBody], error) {
org := OrgID(in.ID)
p, err := actingOrg(ctx, org)
if err != nil {
return nil, err
}
subject, email := strings.TrimSpace(in.Body.Subject), strings.TrimSpace(in.Body.Email)
if (subject == "") == (email == "") {
return nil, huma.Error400BadRequest("Give either an email or a provider subject.")
}
want := Membership{IdentityID: "new", OrgID: org, WorkspaceID: WorkspaceID(in.Body.Workspace), Role: Role(in.Body.Role)}
if err := checkGrantable(want); err != nil {
return nil, err
}
if err := a.mayManage(ctx, p, org, want.Role); err != nil {
return nil, err
}
settings, err := a.people.AuthenticationSettings(ctx)
if err != nil {
return nil, a.failed(ctx, "read the authentication settings", err)
}
if settings.OIDCIssuer == "" {
return nil, huma.Error409Conflict("Adding a person by email needs an identity provider. Set one in the authentication settings, or create a local account.")
}
bound, name := Subject{Issuer: settings.OIDCIssuer, Subject: subject}, subject
if email != "" {
bound, name = EmailSubject(settings.OIDCIssuer, email), email
}
if display := strings.TrimSpace(in.Body.DisplayName); display != "" {
name = display
}
want.IdentityID, err = a.person(ctx, p, bound, name)
if err != nil {
return nil, err
}
if err := a.dir.EnsureMembership(ctx, want); err != nil {
return nil, a.failed(ctx, "add the member", err)
}
return httpapi.OK(membershipBody{Membership: want}), nil
}
// person returns the identity bound to subject, creating and binding a user
// identity when no one holds it yet.
func (a *memberAPI) person(ctx context.Context, p Principal, subject Subject, name string) (IdentityID, error) {
existing, err := a.people.ResolveSubject(ctx, subject)
switch {
case err == nil:
return existing.ID, nil
case !errors.Is(err, ErrIdentityNotFound):
return "", a.failed(ctx, "look up the person", err)
}
value, err := NewIdentity(IdentityID(rand.Text()), KindUser, name)
if err != nil {
return "", huma.Error400BadRequest("Give the person a name.")
}
audit := memberAudit(p)
if _, err := a.people.Create(ctx, value, audit); err != nil {
return "", a.failed(ctx, "create the person", err)
}
if err := a.people.BindSubject(ctx, value.ID, subject, audit); err != nil {
return "", a.failed(ctx, "create the person", err)
}
return value.ID, nil
}
func memberAudit(p Principal) IdentityAudit {
return IdentityAudit{ActorID: p.IdentityID, Source: "members api", RequestID: newRequestID("member")}
}
// checkGrantable refuses a membership no member can hold: the system
// identity's, the workload role, and an invalid role.
func checkGrantable(m Membership) error {
switch {
case m.IdentityID == SystemID:
return huma.Error400BadRequest("The system identity can't be changed.")
case m.Role == RoleWorkload:
return huma.Error400BadRequest("The workload role belongs to background work and can't be given to a member.")
}
if err := m.Validate(); err != nil {
return huma.Error400BadRequest("Choose a role: lowercase letters, digits, dots, dashes or underscores.")
}
return nil
}
// mayManage asks the policy chain whether p may manage a member holding role.
// The resource state carries the role, so a policy can reserve the owner role
// for owners.
func (a *memberAPI) mayManage(ctx context.Context, p Principal, org OrgID, role Role) error {
chain := a.authz()
if chain == nil {
return huma.Error503ServiceUnavailable("Access policies are unavailable.")
}
scoped := p
scoped.Org = org
decision := chain.Authorize(ctx, scoped, ActionManageMembers, Resource{Kind: KindMember, Org: org, State: string(role)}, AccessContext{Time: time.Now()})
switch {
case decision.Err != nil:
return huma.Error503ServiceUnavailable("Access policies are unavailable.")
case !decision.Allowed && role == RoleOwner:
return huma.Error403Forbidden("Only an owner may grant or change the owner role.")
case !decision.Allowed:
return huma.Error403Forbidden("An access policy refuses this change.")
}
return nil
}
// roleHeld returns the role identity holds in scope, an empty scope being the
// organisation-wide membership.
func roleHeld(members []Member, identity IdentityID, scope WorkspaceID) (Role, bool) {
for _, m := range members {
if m.IdentityID == identity && m.WorkspaceID == scope {
return m.Role, true
}
}
return "", false
}
// strandsOwner reports whether giving identity role next (empty to remove or
// suspend) in scope would leave the organisation without an active owner. The
// store enforces the same rule inside its transaction; checking here answers
// before any change and names the cause.
func strandsOwner(members []Member, identity IdentityID, scope WorkspaceID, next Role) bool {
if scope != "" || next == RoleOwner {
return false
}
if role, held := roleHeld(members, identity, ""); !held || role != RoleOwner {
return false
}
for _, m := range members {
if m.IdentityID != identity && m.WorkspaceID == "" && m.Role == RoleOwner && m.Status == LifecycleActive {
return false
}
}
return true
}
// failed answers a failure with the status a person can act on, logging the
// ones it cannot explain.
func (a *memberAPI) failed(ctx context.Context, what string, err error) error {
switch {
case errors.Is(err, ErrLastOwner):
return huma.Error409Conflict("This is the organisation's last active owner. Make another member an owner first.")
case errors.Is(err, ErrIdentityNotFound), errors.Is(err, ErrOrgNotFound):
return huma.Error404NotFound("That person or organisation was not found.")
case errors.Is(err, ErrSystemImmutable):
return huma.Error400BadRequest("The system identity can't be changed.")
case errors.Is(err, ErrIllegalTransition):
return huma.Error409Conflict("The member is already in that state.")
case errors.Is(err, ErrIdentityConflict):
return huma.Error409Conflict("The member changed after you opened the page. Reload and try again.")
}
a.log.ErrorContext(ctx, "member operation failed", "operation", what, "err", err)
return huma.Error503ServiceUnavailable("Couldn't " + what + ". Try again shortly.")
}
+219
View File
@@ -0,0 +1,219 @@
package governance
import (
"context"
"errors"
"log/slog"
"net/http"
"strings"
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
)
// fakeDirectory records the changes a member route lets through.
type fakeDirectory struct {
members []Member
changes []string
}
func (f *fakeDirectory) ListMembers(context.Context, OrgID) ([]Member, error) { return f.members, nil }
func (f *fakeDirectory) EnsureMembership(_ context.Context, m Membership) error {
f.changes = append(f.changes, "ensure "+string(m.IdentityID)+" "+string(m.Role))
return nil
}
func (f *fakeDirectory) RemoveMembership(_ context.Context, m Membership) error {
f.changes = append(f.changes, "remove "+string(m.IdentityID))
return nil
}
func (f *fakeDirectory) Get(_ context.Context, id IdentityID) (Identity, error) {
return Identity{ID: id, Version: 1}, nil
}
func (f *fakeDirectory) Apply(_ context.Context, id IdentityID, _ int64, c IdentityCommand, _ IdentityAudit) (Identity, error) {
f.changes = append(f.changes, string(c.Type)+" "+string(id))
return Identity{ID: id}, nil
}
func status(t *testing.T, err error) int {
t.Helper()
if err == nil {
return http.StatusOK
}
var se huma.StatusError
if !errors.As(err, &se) {
t.Fatalf("error %v carries no status", err)
}
return se.GetStatus()
}
func actor(id IdentityID, role Role) Principal {
return Principal{
IdentityID: id, Kind: KindUser, Org: DefaultOrgID,
Memberships: []Membership{{IdentityID: id, OrgID: DefaultOrgID, Role: role}},
}
}
func member(id IdentityID, role Role, status Lifecycle) Member {
return Member{IdentityID: id, Kind: KindUser, DisplayName: string(id), Role: role, Status: status}
}
func memberRoutes(dir *fakeDirectory) *memberAPI {
return &memberAPI{
dir: dir,
authz: func() Authorizer { return NewEngine(ShippedOrgPolicies(DefaultOrgID)) },
log: slog.New(slog.DiscardHandler),
}
}
// TestAccessWritesNeedTheirGrant guards the declared access of every route that
// changes members or policies, and the grant the shipped roles give it. A
// viewer or developer who reached a write would let them reassign roles or
// rewrite the access policies.
func TestAccessWritesNeedTheirGrant(t *testing.T) {
api := httpapi.New(http.NewServeMux(), "test", nil, nil)
(&Module{}).RegisterAccess(api)
engine := NewEngine(ShippedOrgPolicies(DefaultOrgID))
writes := 0
for _, route := range httpapi.Routes(api) {
mutates := route.Method != http.MethodGet
action, kind, ok := route.Access.Declared()
if !ok {
t.Errorf("%s declares no access", route.ID)
continue
}
if mutates {
writes++
}
wantAction := string(ActionManageMembers)
if strings.HasPrefix(route.Path, "/api/access/policies") {
wantAction = string(ActionManagePolicies)
}
if mutates && action != wantAction {
t.Errorf("%s declares %q, want %q", route.ID, action, wantAction)
}
if !mutates && route.Path == "/api/access/denials" && action != string(ActionReadAudit) {
t.Errorf("%s declares %q, want read_audit", route.ID, action)
}
resource := Resource{Kind: ResourceKind(kind), Org: DefaultOrgID}
if !engine.Authorize(t.Context(), actor("admin", RoleAdmin), Action(action), resource, AccessContext{}).Allowed {
t.Errorf("%s: an admin is refused %s %s", route.ID, action, kind)
}
if mutates || route.Path == "/api/access/denials" {
for _, role := range []Role{RoleViewer, RoleDeveloper} {
if engine.Authorize(t.Context(), actor("lowly", role), Action(action), resource, AccessContext{}).Allowed {
t.Errorf("%s: a %s is allowed %s %s", route.ID, role, action, kind)
}
}
}
}
if writes != 7 {
t.Errorf("%d write routes registered, want 7", writes)
}
}
// TestLastOwnerIsKept guards the rule that an organisation always keeps an
// active owner: removing, demoting or suspending the only one is refused before
// anything changes.
func TestLastOwnerIsKept(t *testing.T) {
sole := []Member{member("root", RoleOwner, LifecycleActive), member("dev", RoleDeveloper, LifecycleActive)}
pair := []Member{member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleActive)}
suspendedPair := []Member{member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleSuspended)}
scoped := append(slicesClone(sole), Member{IdentityID: "root", Kind: KindUser, WorkspaceID: "ws", Role: RoleOwner, Status: LifecycleActive})
tests := []struct {
name string
members []Member
act func(*memberAPI, context.Context) error
want int
changed bool
}{
{"demote the only owner", sole, setRole("root", "admin"), http.StatusConflict, false},
{"remove the only owner", sole, removeMember("root", ""), http.StatusConflict, false},
{"suspend the only owner", sole, suspendMember("root"), http.StatusConflict, false},
{"demote when the other owner is suspended", suspendedPair, setRole("root", "viewer"), http.StatusConflict, false},
{"demote one of two owners", pair, setRole("root", "admin"), http.StatusOK, true},
{"remove one of two owners", pair, removeMember("second", ""), http.StatusOK, true},
{"keep the only owner as owner", sole, setRole("root", "owner"), http.StatusOK, true},
{"remove an owner's workspace membership", scoped, removeMember("root", "ws"), http.StatusOK, true},
{"remove the only developer", sole, removeMember("dev", ""), http.StatusOK, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := &fakeDirectory{members: tt.members}
ctx := WithPrincipal(t.Context(), actor("root", RoleOwner))
err := tt.act(memberRoutes(dir), ctx)
if got := status(t, err); got != tt.want {
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
}
if changed := len(dir.changes) > 0; changed != tt.changed {
t.Fatalf("changes = %v, want changed %v", dir.changes, tt.changed)
}
})
}
}
// TestOwnerRoleNeedsAnOwner guards the rule that only an owner grants the owner
// role or alters an owner: an admin who could would promote themselves.
func TestOwnerRoleNeedsAnOwner(t *testing.T) {
members := []Member{
member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleActive),
member("dev", RoleDeveloper, LifecycleActive),
}
tests := []struct {
name string
by Principal
act func(*memberAPI, context.Context) error
want int
}{
{"admin grants owner", actor("adm", RoleAdmin), setRole("dev", "owner"), http.StatusForbidden},
{"admin demotes an owner", actor("adm", RoleAdmin), setRole("second", "viewer"), http.StatusForbidden},
{"admin removes an owner", actor("adm", RoleAdmin), removeMember("second", ""), http.StatusForbidden},
{"admin suspends an owner", actor("adm", RoleAdmin), suspendMember("second"), http.StatusForbidden},
{"admin grants developer", actor("adm", RoleAdmin), setRole("dev", "admin"), http.StatusOK},
{"owner grants owner", actor("root", RoleOwner), setRole("dev", "owner"), http.StatusOK},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := &fakeDirectory{members: members}
err := tt.act(memberRoutes(dir), WithPrincipal(t.Context(), tt.by))
if got := status(t, err); got != tt.want {
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
}
if (tt.want == http.StatusOK) != (len(dir.changes) > 0) {
t.Fatalf("changes = %v with status %d", dir.changes, tt.want)
}
})
}
}
func setRole(identity, role string) func(*memberAPI, context.Context) error {
return func(a *memberAPI, ctx context.Context) error {
in := &setMemberInput{ID: string(DefaultOrgID), Identity: identity}
in.Body.Role = role
_, err := a.set(ctx, in)
return err
}
}
func removeMember(identity, workspace string) func(*memberAPI, context.Context) error {
return func(a *memberAPI, ctx context.Context) error {
_, err := a.remove(ctx, &removeMemberInput{ID: string(DefaultOrgID), Identity: identity, Workspace: workspace})
return err
}
}
func suspendMember(identity string) func(*memberAPI, context.Context) error {
return func(a *memberAPI, ctx context.Context) error {
_, err := a.suspend(ctx, &memberPath{ID: string(DefaultOrgID), Identity: identity})
return err
}
}
func slicesClone(m []Member) []Member { return append([]Member(nil), m...) }
+5
View File
@@ -176,6 +176,11 @@ type Member struct {
DisplayName string `json:"display_name"`
WorkspaceID WorkspaceID `json:"workspace_id,omitempty"`
Role Role `json:"role"`
// Status is the identity's lifecycle: a suspended member keeps the role but
// cannot act.
Status Lifecycle `json:"status"`
// LastSignIn is nil for a member without an account or who never signed in.
LastSignIn *time.Time `json:"last_sign_in,omitempty"`
}
// AgentAssignment records the one org an agent or service identity serves.
+31 -6
View File
@@ -274,15 +274,40 @@ func (m *Module) RecordDenial(ctx context.Context, d Denial) error {
// ListDenials returns an org's most recent denials, newest first.
func (m *Module) ListDenials(ctx context.Context, orgID OrgID, limit int) ([]Denial, error) {
if limit <= 0 {
limit = defaultDenials
} else if limit > maxDenials {
limit = maxDenials
return m.SearchDenials(ctx, DenialQuery{Org: orgID, Limit: limit})
}
// DenialQuery narrows SearchDenials. A zero field matches everything.
type DenialQuery struct {
Org OrgID
Principal IdentityID
Action Action
Kind ResourceKind
Since time.Time
Until time.Time
Limit int
Offset int
}
// SearchDenials returns the denials matching q, newest first, at most
// maxDenials per call.
func (m *Module) SearchDenials(ctx context.Context, q DenialQuery) ([]Denial, error) {
if q.Limit <= 0 {
q.Limit = defaultDenials
} else if q.Limit > maxDenials {
q.Limit = maxDenials
}
rows, err := m.store.ListDenials(ctx, string(orgID), limit)
rows, err := m.store.SearchDenials(ctx, store.DenialFilter{
OrgID: string(q.Org), Principal: string(q.Principal), Action: string(q.Action),
ResourceKind: string(q.Kind), Since: q.Since, Until: q.Until, Limit: q.Limit, Offset: max(q.Offset, 0),
})
if err != nil {
return nil, err
}
return denialsFromRows(rows), nil
}
func denialsFromRows(rows []store.DenialRow) []Denial {
out := make([]Denial, 0, len(rows))
for _, r := range rows {
out = append(out, Denial{
@@ -297,7 +322,7 @@ func (m *Module) ListDenials(ctx context.Context, orgID OrgID, limit int) ([]Den
Count: r.Count,
})
}
return out, nil
return out
}
// PruneDenials deletes denial rows older than cutoff in bounded batches.
+7 -2
View File
@@ -238,13 +238,18 @@ func (m *Module) ListMembers(ctx context.Context, id OrgID) ([]Member, error) {
if r.WorkspaceID.Valid {
ws = WorkspaceID(r.WorkspaceID.String)
}
out = append(out, Member{
member := Member{
IdentityID: IdentityID(r.IdentityID),
Kind: Kind(r.Kind),
DisplayName: r.DisplayName,
WorkspaceID: ws,
Role: Role(r.Role),
})
Status: Lifecycle(r.Lifecycle),
}
if r.LastSignIn.Valid {
member.LastSignIn = &r.LastSignIn.Time
}
out = append(out, member)
}
return out, nil
}
+76
View File
@@ -0,0 +1,76 @@
package runs
import (
"context"
"time"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// RunnerHealth is the state of the workers that execute runs.
type RunnerHealth struct {
Workers int `json:"workers"`
// Error is the last failure to claim work, until a claim succeeds again.
Error string `json:"error,omitzero"`
ErrorAt *time.Time `json:"error_at,omitzero"`
}
// RunCounts is how many runs are in each state that still needs work or a
// person.
type RunCounts struct {
Queued int `json:"queued"`
Running int `json:"running"`
// Waiting runs wait for a timer or a signal.
Waiting int `json:"waiting"`
// WaitingApproval runs ended waiting for an approval.
WaitingApproval int `json:"waiting_approval"`
// Parked runs stopped for a person, who retries them from a step.
Parked int `json:"parked"`
}
type claimFailure struct {
err string
at time.Time
}
// noteClaim records the outcome of a worker's attempt to claim a run.
func (e *Engine) noteClaim(err error) {
if err == nil {
e.claimFailure.Store(nil)
return
}
e.claimFailure.Store(&claimFailure{err: err.Error(), at: time.Now()})
}
// Runner reports the workers and the last failure to claim work.
func (e *Engine) Runner() RunnerHealth {
h := RunnerHealth{Workers: e.opts.Workers}
if f := e.claimFailure.Load(); f != nil {
h.Error, h.ErrorAt = f.err, &f.at
}
return h
}
// Counts returns how many of orgID's runs are in each state that still needs
// work or a person.
func (e *Engine) Counts(ctx context.Context, orgID string) (RunCounts, error) {
n, err := e.db.Counts(ctx, orgID)
if err != nil {
return RunCounts{}, err
}
return RunCounts{
Queued: n[store.StateQueued], Running: n[store.StateRunning], Waiting: n[store.StateWaiting],
WaitingApproval: n[store.StateWaitingApproval], Parked: n[store.StateParked],
}, nil
}
// LastFailed returns orgID's most recently failed run, or nil when none has
// failed.
func (e *Engine) LastFailed(ctx context.Context, orgID string) (*RunView, error) {
s, ok, err := e.db.LastFailed(ctx, orgID)
if err != nil || !ok {
return nil, err
}
v := view(s, nil)
return &v, nil
}
+36
View File
@@ -579,3 +579,39 @@ func nullJSON(v jsontext.Value) any {
}
return []byte(v)
}
// Counts returns how many of an organisation's runs are in each state.
func (s *Store) Counts(ctx context.Context, orgID string) (map[string]int, error) {
var rows []struct {
State string `db:"state"`
N int `db:"n"`
}
if err := s.db.SelectContext(ctx, &rows, `
SELECT state, count(*) AS n FROM runs.runs
WHERE org_id = $1 AND NOT dry AND state IN ('queued', 'running', 'waiting', 'waiting_approval', 'parked')
GROUP BY state`, orgID); err != nil {
return nil, fmt.Errorf("count runs: %w", err)
}
out := make(map[string]int, len(rows))
for _, r := range rows {
out[r.State] = r.N
}
return out, nil
}
// LastFailed returns an organisation's most recently failed run; ok is false
// when none has failed.
func (s *Store) LastFailed(ctx context.Context, orgID string) (Summary, bool, error) {
var out Summary
err := s.db.GetContext(ctx, &out, `
SELECT id, workflow, workflow_version, state, attempts, error, detail, created_at, finished_at, dry FROM runs.runs
WHERE org_id = $1 AND NOT dry AND state = 'failed'
ORDER BY coalesce(finished_at, created_at) DESC LIMIT 1`, orgID)
if errors.Is(err, sql.ErrNoRows) {
return Summary{}, false, nil
}
if err != nil {
return Summary{}, false, fmt.Errorf("read last failed run: %w", err)
}
return out, true, nil
}
+7
View File
@@ -12,6 +12,7 @@ import (
"log/slog"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
@@ -72,6 +73,9 @@ type Engine struct {
opts Options
mu sync.RWMutex
deps *Deps
// claimFailure is the last failed attempt to claim a run, cleared by the
// next attempt that succeeds.
claimFailure atomic.Pointer[claimFailure]
// load turns a claimed run into the code that executes it.
load func(ctx context.Context, run store.Run) (Workflow, error)
}
@@ -331,6 +335,9 @@ func (e *Engine) Work(ctx context.Context) {
func (e *Engine) workerLoop(ctx context.Context) {
for ctx.Err() == nil {
claimed, err := e.claimAndExecute(ctx)
if ctx.Err() == nil {
e.noteClaim(err)
}
if err != nil {
e.log.ErrorContext(ctx, "run worker", "error", err)
}
+181
View File
@@ -0,0 +1,181 @@
package system
import (
"context"
"net/http"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/platform/health"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/runs"
)
// healthTimeout bounds the checks, so a dependency that hangs shows as
// unhealthy instead of stalling the report.
const healthTimeout = 5 * time.Second
// Sources are the facts the health report is assembled from. Each comes from
// the module that owns it, and the composition root supplies them. A source
// that is nil, or that fails, is reported as unhealthy.
type Sources struct {
// Checks runs the readiness checks, one per module or dependency.
Checks func(context.Context) []health.Result
// Connectors is the state of each accepted package.
Connectors func() []connectors.Health
// Runner is the state of the run workers.
Runner func() runs.RunnerHealth
// Waiting counts what waits on a person.
Waiting func(context.Context) (Waiting, error)
// LastFailedRun is the most recent run that failed; nil when none has.
LastFailedRun func(context.Context) (*runs.RunView, error)
}
// UseHealth sets where the health report reads from.
func (m *Module) UseHealth(s Sources) {
m.mu.Lock()
defer m.mu.Unlock()
m.sources = s
}
// Waiting is what waits on a person.
type Waiting struct {
Approvals int `json:"approvals" doc:"Approval requests waiting for an answer"`
// Runs are in the states that need work or a person, by state.
Runs runs.RunCounts `json:"runs"`
}
// Check is one module or dependency's readiness.
type Check struct {
Name string `json:"name"`
Healthy bool `json:"healthy"`
Error string `json:"error,omitzero"`
}
// Runner is the state of the run workers.
type Runner struct {
Healthy bool `json:"healthy"`
runs.RunnerHealth
}
// Failure is the most recent thing that went wrong and why.
type Failure struct {
Source string `json:"source" doc:"A connector name, the runner, or a workflow"`
Cause string `json:"cause"`
At time.Time `json:"at"`
// RunID is set when the failure is a run.
RunID string `json:"run_id,omitzero"`
}
// Health is the binary's state at the time it was read.
type Health struct {
Version string `json:"version"`
Healthy bool `json:"healthy"`
CheckedAt time.Time `json:"checked_at"`
Checks []Check `json:"checks"`
Connectors []connectors.Health `json:"connectors"`
Runner Runner `json:"runner"`
// Waiting is absent when it could not be read; that is a failed check.
Waiting *Waiting `json:"waiting,omitzero"`
LastFailure *Failure `json:"last_failure,omitzero"`
}
func (m *Module) registerHealth(api huma.API) {
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindInstance), huma.Operation{
OperationID: "get-system-health", Method: http.MethodGet, Path: "/api/system/health",
Summary: "Report whether each module, connector and the runner is working, and what waits on a person", Tags: []string{"System"},
}, m.getHealth)
}
// getHealth never fails because a part is down: a failing part is reported as
// unhealthy with its cause.
func (m *Module) getHealth(ctx context.Context, _ *struct{}) (*httpapi.Reply[Health], error) {
m.mu.RLock()
src := m.sources
m.mu.RUnlock()
ctx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
h := Health{Version: buildinfo.Version, CheckedAt: time.Now().UTC(), Checks: []Check{}, Connectors: []connectors.Health{}}
if src.Checks != nil {
for _, r := range src.Checks(ctx) {
h.Checks = append(h.Checks, Check{Name: r.Name, Healthy: r.Error == "", Error: r.Error})
}
}
if src.Connectors != nil {
h.Connectors = src.Connectors()
}
h.Runner = Runner{Healthy: true}
if src.Runner != nil {
h.Runner.RunnerHealth = src.Runner()
h.Runner.Healthy = h.Runner.Error == ""
}
if src.Waiting != nil {
w, err := src.Waiting(ctx)
if err != nil {
m.log.ErrorContext(ctx, "count what waits on a person", "error", err)
h.Checks = append(h.Checks, Check{Name: "waiting", Error: "couldn't count approvals and runs: " + err.Error()})
} else {
h.Waiting = &w
}
}
var lastRun *runs.RunView
if src.LastFailedRun != nil {
var err error
if lastRun, err = src.LastFailedRun(ctx); err != nil {
m.log.ErrorContext(ctx, "read the last failed run", "error", err)
h.Checks = append(h.Checks, Check{Name: "runs", Error: "couldn't read recent runs: " + err.Error()})
}
}
h.Healthy = healthy(h)
h.LastFailure = lastFailure(h, lastRun)
return httpapi.OK(h), nil
}
func healthy(h Health) bool {
if !h.Runner.Healthy {
return false
}
for _, c := range h.Checks {
if !c.Healthy {
return false
}
}
for _, c := range h.Connectors {
if !c.Healthy {
return false
}
}
return true
}
// lastFailure picks the newest of the failures with a time: a connector that
// failed to start, the runner's last claim error, and the last failed run.
func lastFailure(h Health, run *runs.RunView) *Failure {
var out *Failure
consider := func(f Failure) {
if out == nil || f.At.After(out.At) {
out = &f
}
}
for _, c := range h.Connectors {
if !c.Healthy && c.FailedAt != nil {
consider(Failure{Source: c.Name, Cause: c.Error, At: *c.FailedAt})
}
}
if h.Runner.ErrorAt != nil {
consider(Failure{Source: "runner", Cause: h.Runner.Error, At: *h.Runner.ErrorAt})
}
if run != nil {
at := run.CreatedAt
if run.FinishedAt != nil {
at = *run.FinishedAt
}
consider(Failure{Source: run.Workflow, Cause: run.Error, At: at, RunID: run.ID.String()})
}
return out
}
+2
View File
@@ -35,6 +35,7 @@ type Module struct {
mu sync.RWMutex
bootstrap Bootstrap
rotate func(hash string) error
sources Sources
log *slog.Logger
}
@@ -55,6 +56,7 @@ func (m *Module) Register(api huma.API) {
OperationID: "rotate-admin-token", Method: http.MethodPost, Path: "/api/admin/token",
Summary: "Rotate the admin token; the new one is shown once", Tags: tags,
}, m.rotateAdminToken)
m.registerHealth(api)
}
// adminToken is a new admin token, returned once.
+2 -2
View File
@@ -7,7 +7,7 @@
{
"path": "/",
"label": "Dashboard",
"description": "Running work, work needing attention, token spend."
"description": "Health, what waits on a person, and recent runs."
},
{
"path": "/tasks",
@@ -57,7 +57,7 @@
{
"path": "/settings/status",
"label": "Status",
"description": "Service presence, dependency health, and build versions."
"description": "Module, connector and runner health, and the running version."
},
{
"path": "/logs",
+13 -1
View File
@@ -8,6 +8,7 @@ import { ReadError } from "@/components/ui/read-error";
import { Button } from "@/components/ui/button";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Input } from "@/components/ui/input";
import { Section } from "@/components/ui/section";
import { Spinner } from "@/components/ui/spinner";
import { Timestamp } from "@/components/ui/timestamp";
@@ -15,7 +16,7 @@ import { useDenialsStore } from "@/stores/denials";
import { denialResource, type Denial } from "./denials";
const store = useDenialsStore();
const { denials, loading, error } = storeToRefs(store);
const { denials, filters, more, loading, error } = storeToRefs(store);
const columns: DataTableColumn<Denial>[] = [
{ id: "at", label: "When", fixed: true, width: "9rem", sort: (d) => d.at },
@@ -39,6 +40,15 @@ onMounted(() => void store.load());
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="store.load()" />
<Section>
<form class="mb-3 flex flex-wrap items-center gap-2" @submit.prevent="store.load()">
<Input v-model="filters.principal" class="w-48" aria-label="Principal" placeholder="Principal" />
<Input v-model="filters.action" class="w-36" aria-label="Action" placeholder="Action" />
<Input v-model="filters.kind" class="w-36" aria-label="Resource kind" placeholder="Resource kind" />
<Input v-model="filters.since" type="datetime-local" class="w-52" aria-label="From" />
<Input v-model="filters.until" type="datetime-local" class="w-52" aria-label="Until" />
<Button type="submit" size="sm" :disabled="loading">Filter</Button>
<Button type="button" variant="outline" size="sm" :disabled="loading" @click="store.clear()">Clear</Button>
</form>
<div class="flex flex-col gap-3">
<p v-if="loading && !denials.length" class="flex items-center gap-2 text-label text-fg-muted">
<Spinner />Loading denials…
@@ -57,6 +67,7 @@ onMounted(() => void store.load());
<Empty><EmptyHeader><EmptyTitle>No denials recorded</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-at="{ row }"><Timestamp :at="row.at" /></template>
<template #cell-action="{ row }"><span class="font-mono text-caption">{{ row.action }}</span></template>
<template #cell-principal="{ row }"><span class="font-mono text-caption">{{ row.principal }}</span></template>
<template #cell-resource="{ row }"><span class="font-mono text-caption">{{ denialResource(row) }}</span></template>
<template #cell-level="{ row }"><Badge variant="idle">{{ row.level }}</Badge></template>
@@ -65,6 +76,7 @@ onMounted(() => void store.load());
</template>
<template #cell-count="{ row }"><span class="tabular-nums">{{ row.count }}</span></template>
</DataTable>
<Button v-if="more" variant="outline" size="sm" class="self-start" :disabled="loading" @click="store.load(true)">Load older</Button>
</div>
</Section>
</div>
+2
View File
@@ -14,6 +14,8 @@ export interface Denial {
export interface DenialsResponse {
denials: Denial[];
/** Whether older refusals match beyond this page. */
more: boolean;
}
/** The resource column: the kind and id together, or just the kind. */
+10 -26
View File
@@ -1,35 +1,19 @@
<script setup lang="ts">
import { Alert, AlertDescription } from "@/components/ui/alert";
import { errors } from "@/copy/errors";
import { storeToRefs } from "pinia";
import { computed } from "vue";
import { useSystemHealth } from "@/lib/system-health";
import { useTasksStore } from "@/stores/tasks";
import LiveActivityCard from "./LiveActivityCard.vue";
import HealthCard from "./HealthCard.vue";
import NeedsAttentionCard from "./NeedsAttentionCard.vue";
import QuickTask from "./QuickTask.vue";
import SetupPanel from "./SetupPanel.vue";
import { setupPanelState } from "./setup-preference";
import { useDashboard } from "./state";
import RecentRunsCard from "./RecentRunsCard.vue";
/** Start work, then see what needs you and what is happening. Setup takes a
* rail only while it is incomplete. */
const { error, setup, summary } = storeToRefs(useTasksStore());
useDashboard();
const setupIncomplete = computed(() => setupPanelState(setup.value).kind === "incomplete");
/** What is wrong, what waits on you, then what ran. */
const { data: health, error, reload } = useSystemHealth();
</script>
<template>
<h1 class="sr-only">Dashboard</h1>
<Alert v-if="error" variant="destructive" class="mb-4"><AlertDescription>{{ errors.dashboard.page.unresponsive(error) }}</AlertDescription></Alert>
<div :class="setupIncomplete ? 'grid items-start gap-4 @3xl:grid-cols-[minmax(0,1fr)_320px]' : 'mx-auto max-w-5xl'">
<div class="grid min-w-0 gap-4">
<QuickTask class="mb-2" />
<NeedsAttentionCard />
<LiveActivityCard v-if="summary" />
</div>
<SetupPanel v-if="setupIncomplete" class="@max-3xl:-order-1" />
<h1 class="sr-only">Home</h1>
<div class="mx-auto grid max-w-5xl min-w-0 gap-4">
<HealthCard :health="health" :error="error" @retry="reload" />
<NeedsAttentionCard />
<RecentRunsCard />
</div>
</template>
+60
View File
@@ -0,0 +1,60 @@
<script setup lang="ts">
import { computed } from "vue";
import { RouterLink } from "vue-router";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Item, ItemGroup } from "@/components/ui/item";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { StatusDot } from "@/components/ui/status-dot";
import { Timestamp } from "@/components/ui/timestamp";
import type { SystemHealth } from "@/lib/api";
const props = defineProps<{ health?: SystemHealth; error: string }>();
defineEmits<{ retry: [] }>();
interface Problem {
key: string;
label: string;
cause: string;
at?: string;
}
// Only what is wrong is listed; a healthy part is the absence of a row.
const problems = computed<Problem[]>(() => {
const h = props.health;
if (!h) return [];
return [
...h.checks.filter((c) => !c.healthy).map((c) => ({ key: `check:${c.name}`, label: c.name, cause: c.error ?? "" })),
...h.connectors
.filter((c) => !c.healthy)
.map((c) => ({ key: `connector:${c.name}`, label: c.name, cause: c.error ?? "", at: c.failed_at })),
...(h.runner.healthy ? [] : [{ key: "runner", label: "runner", cause: h.runner.error ?? "", at: h.runner.error_at }]),
];
});
</script>
<template>
<ReadError v-if="error && !health" :message="error" retry-label="Retry" @retry="$emit('retry')" />
<Section v-else-if="health" title="Health" class="mb-0">
<template #action>
<Button variant="ghost" size="sm" as-child><RouterLink to="/settings/status">Status</RouterLink></Button>
</template>
<p v-if="!problems.length" class="flex items-center gap-2 text-label">
<StatusDot tone="ok" />Everything is working
<span class="font-mono text-caption text-fg-muted">{{ health.version }}</span>
</p>
<ItemGroup v-else>
<Item v-for="problem in problems" :key="problem.key" as="li" variant="outline" size="xs">
<Badge variant="danger">unhealthy</Badge>
<div class="min-w-0 flex-1">
<p class="truncate font-medium font-mono">{{ problem.label }}</p>
<p class="break-words text-caption text-fg-muted" :title="problem.cause">
{{ problem.cause.split("\n")[0] }}<template v-if="problem.at"> · <Timestamp :at="problem.at" /></template>
</p>
</div>
</Item>
</ItemGroup>
</Section>
</template>
-243
View File
@@ -1,243 +0,0 @@
<script setup lang="ts">
import { storeToRefs } from "pinia";
import { computed, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ChevronDown, ChevronRight } from "@lucide/vue";
import { Badge } from "@/components/ui/badge";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { Section } from "@/components/ui/section";
import { StatusDot } from "@/components/ui/status-dot";
import { Timestamp } from "@/components/ui/timestamp";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { useSessionStore } from "@/stores/session";
import { useTasksStore } from "@/stores/tasks";
import { useAlerts } from "@/alerts/use-alerts";
import ActivityRow from "./ActivityRow.vue";
import { useOrgStore } from "@/stores/org";
import { activityDetail } from "./activity-detail";
import {
ACTIVITY_FILTERS,
activityEmptyTitle,
filterActivity,
type ActivityFilter,
} from "./activity-filter";
import { groupActivity, type ActivityGroup } from "./activity-group";
import { describeTimelineEvent } from "@/tasks/timeline-event";
const identities = useOrgStore();
identities.watchIdentities();
/**
* The last 50 events, newest first, with the stream's own state on the card and
* the All / Tasks / System filter over it. The filter and the grouping rules
* live in activity-filter.ts and activity-group.ts and are tested there; this
* card only renders their result.
*/
const router = useRouter();
const { summary } = storeToRefs(useTasksStore());
const running = computed(() => summary.value?.statuses?.running || 0);
// Open alerts lead the feed until resolved: a stream event scrolls away, the
// condition it reported does not.
const { alerts } = useAlerts();
const { activity } = storeToRefs(
useSessionStore(),
);
/** Which view of the feed is showing: everything, task runs, or the daemon's
* own background work. */
const filter = ref<ActivityFilter>("tasks");
const shown = computed(() => filterActivity(activity.value, filter.value));
// A blocked task already leads the page under Needs attention.
const pinned = computed(() =>
filter.value === "tasks"
? []
: alerts.value.filter((alert) => alert.kind !== "task.needs_decision"),
);
const emptyTitle = computed(() =>
activityEmptyTitle(shown.value.length, filter.value),
);
// Expansion is keyed on label:taskID, so a live run stays open as its events
// arrive, and a collapsed group re-collapses only when the run breaks.
const expanded = reactive(new Set<string>());
const groups = computed(() => groupActivity(shown.value));
function toggle(key: string) {
if (expanded.has(key)) expanded.delete(key);
else expanded.add(key);
}
function isOpen(group: ActivityGroup): boolean {
return expanded.has(group.key);
}
// The collapsed group row shows what its newest event would have shown, so
// expanding and collapsing moves no information.
function groupDetail(group: ActivityGroup) {
return activityDetail(group.representative, identities.identityName);
}
function openTask(taskID: number) {
void router.push(`/tasks?task=${encodeURIComponent(taskID)}`);
}
</script>
<template>
<Section :title="running ? `Activity · ${running} running` : 'Activity'" class="mb-0">
<template #action>
<ToggleGroup type="single" variant="outline" size="sm" :model-value="filter" aria-label="Filter activity" @update:model-value="(value) => { if (value) filter = value as typeof filter; }">
<ToggleGroupItem v-for="option in ACTIVITY_FILTERS" :key="option.value" :value="option.value">{{ option.label }}</ToggleGroupItem>
</ToggleGroup>
</template>
<!--
role="region" names the table for landmark navigation; aria-live is
polite, not assertive, because rows arrive constantly while the stream
is live and an assertive region would talk over the operator. The live
region is the table's wrapper, so new rows are announced as they
appear without a second hidden summary to keep in sync.
-->
<div role="region" aria-label="Activity" aria-live="polite">
<!--
The feed's height is bounded to the card, not the page: group rows
multiply with the number of interleaved tasks, so an unbounded table
would push the rest of the dashboard off screen. The newest events
stay visible at the top; the rest scroll inside the card. The header
stays put via sticky so the columns keep their labels while scrolling.
-->
<div class="max-h-[28rem] overflow-y-auto">
<Table>
<TableHeader class="sticky top-0 z-10 bg-card">
<TableRow>
<TableHead>Event</TableHead>
<TableHead>Task</TableHead>
<TableHead>Detail</TableHead>
<TableHead>When</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="alert in pinned" :key="`alert:${alert.id}`">
<TableCell class="font-medium">
<span class="inline-flex items-center gap-1.5">
<StatusDot tone="warn" />Alert
<Badge v-if="alert.count > 1" variant="idle">×{{ alert.count }}</Badge>
</span>
</TableCell>
<TableCell class="font-mono">–</TableCell>
<TableCell class="w-[55%] max-w-0">
<span class="flex items-center gap-3">
<Tooltip>
<TooltipTrigger as-child>
<span tabindex="0" class="min-w-0 flex-1 truncate">{{ alert.title }}</span>
</TooltipTrigger>
<TooltipContent v-if="alert.body">{{ alert.body }}</TooltipContent>
</Tooltip>
<RouterLink v-if="alert.link" :to="alert.link" class="shrink-0 text-warn hover:underline">Fix →</RouterLink>
</span>
</TableCell>
<TableCell><Timestamp :at="alert.last_seen" /></TableCell>
</TableRow>
<TableRow v-if="!shown.length && !pinned.length">
<TableCell colspan="4">
<Empty>
<EmptyHeader>
<EmptyTitle>{{ emptyTitle }}</EmptyTitle>
</EmptyHeader>
</Empty>
</TableCell>
</TableRow>
<template v-if="shown.length">
<template v-for="group in groups" :key="group.key">
<!-- A singleton is exactly the row the raw feed would show. -->
<ActivityRow
v-if="group.count === 1"
:event="group.representative"
@open="openTask"
/>
<template v-else>
<!--
The row stays a row: mouse clicks open the task via the
row handler, and the first cell holds the two real focus
targets -- the disclosure button and, when the group
resolves to a task, an open button carrying its title.
-->
<TableRow
:class="group.taskID > 0 ? 'cursor-pointer' : ''"
@click="group.taskID > 0 && openTask(group.taskID)"
>
<TableCell class="font-medium">
<div class="flex items-center gap-1">
<button
type="button"
class="inline-flex items-center rounded hover:opacity-80 focus-visible:outline-2 focus-visible:outline-ring"
:aria-expanded="isOpen(group)"
:aria-label="`${isOpen(group) ? 'Hide' : 'Show'} ${group.count} ${group.label} events`"
@click.stop="toggle(group.key)"
>
<ChevronDown
v-if="isOpen(group)"
class="size-3.5 shrink-0 text-fg-subtle"
/>
<ChevronRight
v-else
class="size-3.5 shrink-0 text-fg-subtle"
/>
</button>
<button
v-if="group.taskID > 0"
type="button"
class="inline-flex items-center gap-1 rounded hover:opacity-80 focus-visible:outline-2 focus-visible:outline-ring"
aria-label="Open task details"
@click.stop="openTask(group.taskID)"
>
{{ describeTimelineEvent({ ...group.representative, at: undefined, detail: undefined }).title }}
<Badge variant="idle">×{{ group.count }}</Badge>
</button>
<template v-else>
{{ describeTimelineEvent({ ...group.representative, at: undefined, detail: undefined }).title }}
<Badge variant="idle">×{{ group.count }}</Badge>
</template>
</div>
</TableCell>
<TableCell class="font-mono">{{
group.taskID > 0 ? `#${group.taskID}` : "–"
}}</TableCell>
<TableCell class="w-[55%] max-w-0">
<Tooltip>
<TooltipTrigger as-child>
<span tabindex="0" class="block truncate">{{ groupDetail(group).text }}</span>
</TooltipTrigger>
<TooltipContent v-if="groupDetail(group).truncated">{{ groupDetail(group).full }}</TooltipContent>
</Tooltip>
</TableCell>
<TableCell><Timestamp :at="group.representative.at" /></TableCell>
</TableRow>
<template v-if="isOpen(group)">
<ActivityRow
v-for="(event, i) in group.events"
:key="`${group.key}:${i}`"
:event="event"
@open="openTask"
/>
</template>
</template>
</template>
</template>
</TableBody>
</Table>
</div>
</div>
</Section>
</template>
+65 -46
View File
@@ -1,64 +1,83 @@
<script setup lang="ts">
import { Item, ItemGroup } from "@/components/ui/item";
import { storeToRefs } from "pinia";
import { computed } from "vue";
import { computed, onMounted } from "vue";
import { RouterLink } from "vue-router";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Item, ItemGroup } from "@/components/ui/item";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { attentionStatusIds, statusLabel } from "@/lib/task-meta";
import TaskRowActions from "@/tasks/TaskRowActions.vue";
import type { Task } from "@/tasks/task";
import { useTasksStore } from "@/stores/tasks";
import { Timestamp } from "@/components/ui/timestamp";
import { api } from "@/lib/api";
import { usePoll, useRead } from "@/lib/use-read";
import { useLiveResource } from "@/stores/session";
const { tasks } = storeToRefs(useTasksStore());
const loadDashboard = useTasksStore().load;
/** What waits on a person: approval requests, and runs a step parked. Each
* links to the run that needs the answer. */
const approvals = useRead(() => api.approvals());
const runs = useRead(() => api.runs());
function reload(): void {
void approvals.reload();
void runs.reload();
}
onMounted(reload);
useLiveResource(null, reload);
usePoll(reload);
/** Tasks needing attention, each with the reason and the next action. */
type AttentionTask = Task & { park_reason?: string; plan?: string };
interface Waiting {
key: string;
badge: string;
title: string;
caption: string;
run: string;
at?: string;
}
const waiting = computed(() => {
const ids = attentionStatusIds();
return ((tasks.value ?? []) as AttentionTask[]).filter((t) => t.status && ids.has(t.status));
});
const waiting = computed<Waiting[]>(() => [
...(approvals.data.value ?? [])
.filter((a) => a.state === "pending")
.map((a) => ({
key: `approval:${a.id}`,
badge: "Approval",
title: a.summary || a.action,
caption: [a.action, a.target].filter(Boolean).join(" on "),
run: a.run_id,
at: a.requested_at,
})),
...(runs.data.value ?? [])
.filter((r) => r.state === "parked")
.map((r) => ({
key: `run:${r.id}`,
badge: "Parked",
title: r.workflow,
caption: r.detail || r.error || "",
run: r.id,
at: r.created_at,
})),
]);
// Each row mounts its own action menu, so an unbounded list freezes the page
// when many tasks park at once; the rest are one click away on the tasks page.
// Each row is cheap, but the full list is one click away on the runs page.
const shown = computed(() => waiting.value.slice(0, 5));
function meta(task: AttentionTask): string {
const where = task.owner && task.repo ? `${task.owner}/${task.repo}${task.issue_url && task.issue_number ? ` #${task.issue_number}` : ""}` : "";
return [where, task.workflow].filter(Boolean).join(" · ");
}
// A blocker's summary says what is missing; the raw reason stays on hover and
// on the task page.
function reason(task: AttentionTask): string {
return task.recovery?.summary || task.park_reason || task.plan || "";
}
const error = computed(() => approvals.error.value || runs.error.value);
</script>
<template>
<Section v-if="waiting.length" title="Needs attention" class="mb-0">
<ReadError v-if="error" :message="error" retry-label="Retry" @retry="reload" />
<Section v-else-if="waiting.length" title="Needs attention" class="mb-0">
<template #action>
<Button variant="ghost" size="sm" as-child><RouterLink to="/tasks?status=needs_attention">{{ waiting.length > shown.length ? `All ${waiting.length}` : "All tasks" }}</RouterLink></Button>
<Button variant="ghost" size="sm" as-child><RouterLink to="/runs">{{ waiting.length > shown.length ? `All ${waiting.length}` : "All runs" }}</RouterLink></Button>
</template>
<ItemGroup>
<Item v-for="task in shown" :key="task.id" as="li" variant="outline" size="xs">
<Badge variant="warn">{{ statusLabel(task.status ?? "") }}</Badge>
<div class="min-w-0 flex-1">
<RouterLink :to="`/tasks/${task.id}`" class="block truncate font-medium hover:underline">{{
task.title || `Task ${task.id}`
}}</RouterLink>
<p class="truncate text-caption text-fg-muted" :title="task.park_reason || undefined">
{{ [meta(task), reason(task)].filter(Boolean).join(" · ") }}
</p>
</div>
<Button v-if="task.recovery" as-child size="sm">
<RouterLink :to="task.recovery.link">{{ task.recovery.label }}</RouterLink>
</Button>
<TaskRowActions :task="task" :secondary="!!task.recovery" @done="loadDashboard" />
</Item>
<Item v-for="item in shown" :key="item.key" as="li" variant="outline" size="xs">
<Badge variant="warn">{{ item.badge }}</Badge>
<div class="min-w-0 flex-1">
<RouterLink :to="`/runs/${item.run}`" class="block truncate font-medium hover:underline">{{ item.title }}</RouterLink>
<p class="truncate text-caption text-fg-muted">
{{ item.caption }}<template v-if="item.at"> · <Timestamp :at="item.at" /></template>
</p>
</div>
<Button as-child size="sm"><RouterLink :to="`/runs/${item.run}`">Review</RouterLink></Button>
</Item>
</ItemGroup>
</Section>
</template>
+80
View File
@@ -0,0 +1,80 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { Badge } from "@/components/ui/badge";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { Skeleton } from "@/components/ui/skeleton";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Timestamp } from "@/components/ui/timestamp";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { api } from "@/lib/api";
import { usePoll, useRead } from "@/lib/use-read";
import { ended, stateTone } from "@/runs/state";
import { useLiveResource } from "@/stores/session";
/** The latest runs, newest first, filtered to all, those still going, or
* those that failed. */
const router = useRouter();
const { data, error, reload } = useRead(() => api.runs(50));
onMounted(reload);
useLiveResource(null, () => void reload());
usePoll(reload);
const FILTERS = [
{ value: "all", label: "All" },
{ value: "active", label: "Active" },
{ value: "failed", label: "Failed" },
] as const;
const filter = ref<(typeof FILTERS)[number]["value"]>("all");
const shown = computed(() =>
(data.value ?? []).filter((r) => {
if (filter.value === "active") return !ended(r.state);
if (filter.value === "failed") return r.state === "failed";
return true;
}),
);
const running = computed(() => (data.value ?? []).filter((r) => r.state === "running").length);
</script>
<template>
<ReadError v-if="error && !data" :message="error" retry-label="Retry" @retry="reload" />
<Section v-else :title="running ? `Runs · ${running} running` : 'Runs'" class="mb-0">
<template #action>
<ToggleGroup type="single" variant="outline" size="sm" :model-value="filter" aria-label="Filter runs" @update:model-value="(value) => { if (value) filter = value as typeof filter; }">
<ToggleGroupItem v-for="option in FILTERS" :key="option.value" :value="option.value">{{ option.label }}</ToggleGroupItem>
</ToggleGroup>
</template>
<div v-if="!data" role="status" aria-label="Loading runs" class="space-y-2"><Skeleton class="h-8 w-full" /><Skeleton class="h-8 w-full" /></div>
<div v-else class="max-h-[28rem] overflow-y-auto">
<Table>
<TableHeader class="sticky top-0 z-10 bg-card">
<TableRow>
<TableHead>Workflow</TableHead>
<TableHead>State</TableHead>
<TableHead>Detail</TableHead>
<TableHead>Started</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-if="!shown.length">
<TableCell colspan="4">
<Empty><EmptyHeader><EmptyTitle>{{ filter === "all" ? "No runs yet" : `No ${filter} runs` }}</EmptyTitle></EmptyHeader></Empty>
</TableCell>
</TableRow>
<TableRow v-for="run in shown" :key="run.id" class="cursor-pointer" @click="router.push(`/runs/${run.id}`)">
<TableCell class="font-medium">
<RouterLink :to="`/runs/${run.id}`" class="rounded-sm hover:underline focus-visible:outline-2 focus-visible:outline-ring" @click.stop>{{ run.workflow }}</RouterLink>
</TableCell>
<TableCell><Badge :variant="stateTone(run.state)">{{ run.state }}</Badge></TableCell>
<TableCell class="w-[55%] max-w-0"><span class="block truncate" :title="run.error || run.detail || undefined">{{ run.error || run.detail }}</span></TableCell>
<TableCell><Timestamp :at="run.created_at" /></TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</Section>
</template>
+60 -3
View File
@@ -279,10 +279,11 @@ export const api = {
publishProposal: (id: string) =>
request<MemoryNote>(`/api/memory/proposals/${encodeURIComponent(id)}/publish`, { method: "POST", body: {} }),
health: <T = unknown>() => request<T>("/health/detailed"),
services: <T = unknown>() => request<T>("/api/services"),
systemHealth: () => request<SystemHealth>("/api/system/health"),
bootstrap: () => request<Bootstrap>("/api/system/bootstrap"),
policies: <T = unknown>() => request<T>("/api/access/policies"),
denials: <T = unknown>(limit?: number) =>
request<T>(`/api/access/denials${limit ? `?limit=${limit}` : ""}`),
denials: <T = unknown>(params: QueryParams = {}) =>
request<T>("/api/access/denials" + qs(params)),
putPolicy: (policy: unknown) =>
request<{ version: number }>("/api/access/policies", { method: "PUT", body: policy }),
deletePolicy: (policy: unknown) =>
@@ -359,6 +360,17 @@ export const api = {
qs({ workspace }),
{ method: "DELETE", parse: false },
),
/** Suspending stops a member acting at once; their memberships stay. */
suspendOrgMember: (id: string, identity: string) =>
request<void>(
`/api/orgs/${encodeURIComponent(id)}/members/${encodeURIComponent(identity)}/suspend`,
{ method: "POST", parse: false },
),
reactivateOrgMember: (id: string, identity: string) =>
request<void>(
`/api/orgs/${encodeURIComponent(id)}/members/${encodeURIComponent(identity)}/reactivate`,
{ method: "POST", parse: false },
),
assignOrgAgent: <T = unknown>(id: string, identity: string) =>
request<T>(
`/api/orgs/${encodeURIComponent(id)}/agents/${encodeURIComponent(identity)}`,
@@ -715,6 +727,51 @@ export interface ApprovalAssertion {
credential: unknown;
}
/** One module's or dependency's readiness. */
export interface HealthCheck {
name: string;
healthy: boolean;
error?: string;
}
/** One accepted package: a connector, secret engine, guard or MCP server. */
export interface ConnectorHealth {
name: string;
contract: string;
healthy: boolean;
error?: string;
failed_at?: string;
}
export interface RunCounts {
queued: number;
running: number;
waiting: number;
waiting_approval: number;
parked: number;
}
/** What the binary reports about itself: GET /api/system/health. */
export interface SystemHealth {
version: string;
healthy: boolean;
checked_at: string;
checks: HealthCheck[];
connectors: ConnectorHealth[];
runner: { healthy: boolean; workers: number; error?: string; error_at?: string };
waiting?: { approvals: number; runs: RunCounts };
last_failure?: { source: string; cause: string; at: string; run_id?: string };
}
/** The values only the config file sets: GET /api/system/bootstrap. */
export interface Bootstrap {
listen: string;
database_host: string;
schemas: number;
admin_token_set: boolean;
sealing_identity_set: boolean;
}
export interface ApprovalConfirmation {
token: string;
options: { publicKey: Record<string, unknown> };
+13
View File
@@ -0,0 +1,13 @@
import { onMounted } from "vue";
import { api } from "@/lib/api";
import { usePoll, useRead } from "@/lib/use-read";
/** The binary's health, read on mount and every 15 seconds. The Home page and
* the Status page share it. */
export function useSystemHealth() {
const read = useRead(() => api.systemHealth());
onMounted(read.reload);
usePoll(read.reload, 15_000);
return read;
}
+37 -7
View File
@@ -7,7 +7,10 @@ import CreateAccount from "@/accounts/CreateAccount.vue";
import { Section } from "@/components/ui/section";
import { storeToRefs } from "pinia";
import { computed, onMounted, ref } from "vue";
import { Plus, Trash2 } from "@lucide/vue";
import { Pause, Play, Plus, Trash2 } from "@lucide/vue";
import { Badge } from "@/components/ui/badge";
import { Timestamp } from "@/components/ui/timestamp";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
@@ -128,6 +131,16 @@ const setRole = (member: Member, role: unknown) =>
await load();
});
const toggleStatus = (member: Member) =>
run(member.identity_id + "status", async () => {
if (!org.value) return;
await (member.status === "suspended" ? api.reactivateOrgMember : api.suspendOrgMember)(
org.value.id,
member.identity_id,
);
await load();
});
const pending = ref<Member | null>(null);
const remove = (member: Member) =>
run(member.identity_id + (member.workspace_id ?? ""), async () => {
@@ -151,7 +164,9 @@ const remove = (member: Member) =>
<TableHead class="w-28">Kind</TableHead>
<TableHead class="w-44">Workspace</TableHead>
<TableHead class="w-44">Role</TableHead>
<TableHead class="w-24 text-right">Actions</TableHead>
<TableHead class="w-28">Status</TableHead>
<TableHead class="w-40">Last sign-in</TableHead>
<TableHead class="w-28 text-right">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
@@ -169,7 +184,22 @@ const remove = (member: Member) =>
</SelectContent>
</Select>
</TableCell>
<TableCell><Badge :variant="member.status === 'active' ? 'ok' : 'idle'">{{ member.status }}</Badge></TableCell>
<TableCell class="text-label text-fg-subtle"><Timestamp v-if="member.last_sign_in" :at="member.last_sign_in" /><template v-else>–</template></TableCell>
<TableCell class="text-right">
<Tooltip v-if="!member.workspace_id && member.kind !== 'system'">
<TooltipTrigger as-child>
<Button
variant="ghost"
size="icon"
:aria-label="`${member.status === 'suspended' ? 'Reactivate' : 'Suspend'} ${member.display_name}`"
:disabled="busy !== ''"
@click="toggleStatus(member)"
><Play v-if="member.status === 'suspended'" /><Pause v-else
/></Button>
</TooltipTrigger>
<TooltipContent>{{ member.status === "suspended" ? "Reactivate" : "Suspend" }}</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger as-child>
<Button
@@ -177,7 +207,7 @@ const remove = (member: Member) =>
size="icon"
:aria-label="`Remove ${member.display_name}`"
:disabled="busy !== ''"
@click="remove(member)"
@click="pending = member"
><Trash2
/></Button>
</TooltipTrigger>
@@ -186,10 +216,10 @@ const remove = (member: Member) =>
</TableCell>
</TableRow>
<TableRow v-if="loading">
<TableCell colspan="5" class="text-label text-fg-muted" role="status">Loading members…</TableCell>
<TableCell colspan="7" class="text-label text-fg-muted" role="status">Loading members…</TableCell>
</TableRow>
<TableRow v-else-if="!members.length && !error">
<TableCell colspan="5">
<TableCell colspan="7">
<Empty class="py-6">
<EmptyHeader>
<EmptyTitle>No members yet</EmptyTitle>
@@ -202,8 +232,8 @@ const remove = (member: Member) =>
</Section>
<CreateAccount v-if="org?.id===identities.homeOrg" @created="load" />
<div class="grid gap-4 lg:grid-cols-2">
<Section title="Add an identity" class="mb-0">
<div class="grid gap-4" :class="candidates.length ? 'lg:grid-cols-2' : ''">
<Section v-if="candidates.length" title="Add an identity" class="mb-0">
<form class="flex flex-wrap items-center gap-2" @submit.prevent="add">
<Select v-model="picked">
<SelectTrigger class="w-64" aria-label="Identity">
+3
View File
@@ -47,4 +47,7 @@ export interface Member {
display_name: string;
workspace_id?: string;
role: Role;
/** The identity's lifecycle; a suspended member keeps the role but cannot act. */
status: "active" | "suspended" | "retired";
last_sign_in?: string;
}
+2 -2
View File
@@ -52,7 +52,7 @@ export const routes: RouteRecordRaw[] = [
meta: {
label: "Dashboard",
description:
"Running work, work needing attention, token spend.",
"Health, what waits on a person, and recent runs.",
section: "home",
},
},
@@ -185,7 +185,7 @@ export const routes: RouteRecordRaw[] = [
settings: true,
label: "Status",
description:
"Service presence, dependency health, and build versions.",
"Module, connector and runner health, and the running version.",
section: "system_status",
},
},
+70
View File
@@ -0,0 +1,70 @@
<script setup lang="ts">
import { ChevronDown } from "@lucide/vue";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp";
import type { ConnectorHealth, HealthCheck } from "@/lib/api";
defineProps<{ checks: HealthCheck[]; connectors: ConnectorHealth[] }>();
const contract: Record<string, string> = {
connector: "Connector",
secretengine: "Secret engine",
guard: "Guard",
mcp: "MCP server",
};
const moduleColumns: DataTableColumn<HealthCheck>[] = [
{ id: "name", label: "Module", fixed: true },
{ id: "state", label: "State" },
];
const connectorColumns: DataTableColumn<ConnectorHealth>[] = [
{ id: "name", label: "Connector", fixed: true },
{ id: "kind", label: "Kind" },
{ id: "failed", label: "Failed", hidden: true },
{ id: "state", label: "State" },
];
</script>
<template>
<Section title="Modules">
<DataTable :columns="moduleColumns" :rows="checks" :row-key="(row) => row.name" storage-key="status.modules">
<template #cell-name="{ row }"><span class="font-mono">{{ row.name }}</span></template>
<template #cell-state="{ row }">
<Popover v-if="!row.healthy">
<PopoverTrigger as-child>
<Button type="button" variant="ghost" class="h-auto rounded-sm p-0" :aria-label="`Why ${row.name} is unhealthy`">
<Badge variant="danger">unhealthy<ChevronDown data-icon="inline-end" /></Badge>
</Button>
</PopoverTrigger>
<PopoverContent align="start" class="w-96 max-w-[calc(100vw-2rem)] space-y-1 text-label">
<p class="whitespace-pre-line break-words">{{ row.error }}</p>
</PopoverContent>
</Popover>
</template>
</DataTable>
</Section>
<Section v-if="connectors.length" title="Connectors">
<DataTable :columns="connectorColumns" :rows="connectors" :row-key="(row) => row.name" storage-key="status.connectors">
<template #cell-name="{ row }"><span class="font-mono">{{ row.name }}</span></template>
<template #cell-kind="{ row }"><span class="text-fg-muted">{{ contract[row.contract] ?? row.contract }}</span></template>
<template #cell-failed="{ row }"><Timestamp v-if="row.failed_at" :at="row.failed_at" class="text-fg-muted" /></template>
<template #cell-state="{ row }">
<Popover v-if="!row.healthy">
<PopoverTrigger as-child>
<Button type="button" variant="ghost" class="h-auto rounded-sm p-0" :aria-label="`Why ${row.name} failed`">
<Badge variant="danger">failed<ChevronDown data-icon="inline-end" /></Badge>
</Button>
</PopoverTrigger>
<PopoverContent align="start" class="w-96 max-w-[calc(100vw-2rem)] space-y-1 text-label">
<p class="whitespace-pre-line break-words">{{ row.error }}</p>
</PopoverContent>
</Popover>
</template>
</DataTable>
</Section>
</template>
+44
View File
@@ -0,0 +1,44 @@
<script setup lang="ts">
import { computed } from "vue";
import { RouterLink } from "vue-router";
import { MetadataList } from "@/components/ui/metadata-list";
import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp";
import type { SystemHealth } from "@/lib/api";
const props = defineProps<{ health: SystemHealth }>();
const rows = computed(() => {
const h = props.health;
const runs = h.waiting?.runs;
return [
{ label: "Version", value: h.version, mono: true },
{ label: "Workers", value: String(h.runner.workers) },
...(h.waiting
? [
{ label: "Approvals waiting", value: String(h.waiting.approvals) },
{ label: "Runs parked", value: String(runs?.parked ?? 0) },
{ label: "Runs waiting", value: String((runs?.waiting ?? 0) + (runs?.waiting_approval ?? 0)) },
{ label: "Runs queued", value: String(runs?.queued ?? 0) },
{ label: "Runs running", value: String(runs?.running ?? 0) },
]
: []),
];
});
</script>
<template>
<Section title="Instance">
<p v-if="health.runner.error" role="alert" class="mb-3 flex flex-wrap items-center gap-x-2 text-label text-danger">
Runner: {{ health.runner.error }}
<Timestamp v-if="health.runner.error_at" :at="health.runner.error_at" />
</p>
<p v-if="health.last_failure" class="mb-3 flex flex-wrap items-center gap-x-2 text-label text-danger">
<Timestamp :at="health.last_failure.at" />
<span class="font-mono">{{ health.last_failure.source }}</span><span class="min-w-0 break-words" :title="health.last_failure.cause">{{ health.last_failure.cause.split("\n")[0] }}</span>
<RouterLink v-if="health.last_failure.run_id" :to="`/runs/${health.last_failure.run_id}`" class="underline">Run</RouterLink>
</p>
<MetadataList :rows="rows" />
</Section>
</template>
-144
View File
@@ -1,144 +0,0 @@
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { ReadError } from "@/components/ui/read-error";
import { computed, onMounted, onUnmounted, ref } from "vue";
import { ChevronDown } from "@lucide/vue";
import { Badge } from "@/components/ui/badge";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { api, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
interface AppliedRecord {
kind: string;
applied_version: number;
error?: string;
state: "current" | "failed" | "unknown";
}
interface Service {
service: string;
instance_id: string;
state: "up" | "degraded" | "down" | "unknown";
version: string;
install_type: string;
started_at: string;
reported_at: string;
detail?: string;
applied: AppliedRecord[];
checks?: { name: string; status: "ok" | "degraded"; detail?: string }[];
}
const services = ref<Service[]>([]);
const error = ref<string | null>(null);
let timer: ReturnType<typeof setInterval> | undefined;
const tone = { up: "ok", degraded: "warn", down: "danger", unknown: "idle" } as const;
const columns = (agent: boolean): DataTableColumn<Service>[] => [
{ id: "name", label: agent ? "Task" : "Service", fixed: true },
{ id: "version", label: "Version" },
{ id: "install", label: "Runs as", hidden: true },
{ id: "uptime", label: "Uptime" },
{ id: "seen", label: "Last seen", hidden: true },
{ id: "state", label: "State" },
];
// An agent reports for one task, so agents get their own table keyed by task.
const tables = computed(() => {
const agents = services.value.filter((s) => s.service === "archie-agent");
return [
{ title: "Services", key: "services", agent: false, rows: services.value.filter((s) => s.service !== "archie-agent") },
...(agents.length ? [{ title: "Agents", key: "agents", agent: true, rows: agents }] : []),
];
});
const rowKey = (service: Service) => `${service.service}/${service.instance_id}`;
function uptime(service: Service): string {
if (!service.started_at || service.state === "down") return "";
const seconds = Math.max(
0,
Math.round((Date.now() - Date.parse(service.started_at)) / 1000),
);
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days) return `${days}d ${hours}h`;
if (hours) return `${hours}h ${minutes}m`;
return `${minutes}m`;
}
// A row names what is wrong with a service and nothing else: passing checks
// and applied versions that match are noise here.
function problems(service: Service): { key: string; text: string }[] {
return [
...(service.checks ?? [])
.filter((check) => check.status !== "ok")
.map((check) => ({
key: `check:${check.name}`,
text: [check.name, check.detail].filter(Boolean).join(": "),
})),
...service.applied
.filter((record) => record.error)
.map((record) => ({ key: `applied:${record.kind}`, text: `${record.kind}: ${record.error}` })),
];
}
const reasons = (service: Service) => {
const found = problems(service).map((problem) => problem.text);
return found.length || !service.detail ? found : [service.detail];
};
let loadGeneration = 0;
async function load(): Promise<void> {
const current = ++loadGeneration;
try {
services.value = await api.services<Service[]>();
if (current !== loadGeneration) return;
error.value = null;
} catch (err) {
if (current !== loadGeneration) return;
error.value = describeError(err, errors.common.unknown);
}
}
onMounted(() => {
void load();
timer = setInterval(() => void load(), 15_000);
});
onUnmounted(() => clearInterval(timer));
</script>
<template>
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="load" />
<Section v-for="table in tables" v-else :key="table.key" :title="table.title">
<DataTable :columns="columns(table.agent)" :rows="table.rows" :row-key="rowKey" :storage-key="`status.${table.key}`">
<template #cell-name="{ row }">
<RouterLink v-if="table.agent" :to="`/tasks/${row.instance_id}`" class="hover:underline">#{{ row.instance_id }}</RouterLink>
<span v-else class="font-mono">{{ row.service }}</span>
</template>
<template #cell-version="{ row }"><span class="font-mono text-fg-muted">{{ row.version }}</span></template>
<template #cell-install="{ row }"><span class="text-fg-muted">{{ row.install_type }}</span></template>
<template #cell-uptime="{ row }"><span class="tabular-nums">{{ uptime(row) }}</span></template>
<template #cell-seen="{ row }"><Timestamp :at="row.reported_at" class="text-fg-muted" /></template>
<template #cell-state="{ row }">
<template v-if="row.state !== 'up'">
<Popover v-if="reasons(row).length">
<PopoverTrigger as-child>
<Button type="button" variant="ghost" class="h-auto rounded-sm p-0" :aria-label="`Why ${row.service} is ${row.state}`">
<Badge :variant="tone[row.state]">{{ row.state }}<ChevronDown data-icon="inline-end" /></Badge>
</Button>
</PopoverTrigger>
<PopoverContent align="start" class="w-96 max-w-[calc(100vw-2rem)] space-y-1 text-label">
<p v-for="reason in reasons(row)" :key="reason" class="break-words">{{ reason }}</p>
</PopoverContent>
</Popover>
<Badge v-else :variant="tone[row.state]">{{ row.state }}</Badge>
</template>
</template>
</DataTable>
</Section>
</template>
+22 -29
View File
@@ -1,45 +1,38 @@
<script setup lang="ts">
import { storeToRefs } from "pinia";
import { errors } from "@/copy/errors";
import { computed, onMounted, onUnmounted } from "vue";
import { computed, onMounted } from "vue";
import PageHeader from "@/base/PageHeader.vue";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { ReadError } from "@/components/ui/read-error";
import { MetadataList } from "@/components/ui/metadata-list";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import ServicesCard from "./ServicesCard.vue";
import UpdateSettingsCard from "./UpdateSettingsCard.vue";
import VersionsCard from "./VersionsCard.vue";
import { useSettingsStore } from "@/stores/settings";
import { Skeleton } from "@/components/ui/skeleton";
import { api } from "@/lib/api";
import { useSystemHealth } from "@/lib/system-health";
import { useRead } from "@/lib/use-read";
import HealthTables from "./HealthTables.vue";
import InstanceCard from "./InstanceCard.vue";
const { data: health, error, reload } = useSystemHealth();
const startup = useRead(() => api.bootstrap());
onMounted(startup.reload);
const store = useSettingsStore();
const { config, catalogError } = storeToRefs(store);
const bootstrapRows = computed(() =>
Object.entries(config.value?.bootstrap ?? {}).map(([label, value]) => ({ label, value, mono: true })),
Object.entries(startup.data.value ?? {}).map(([label, value]) => ({ label, value: String(value), mono: true })),
);
let timer: ReturnType<typeof setInterval>;
onMounted(() => {
void store.loadConfig();
timer = setInterval(() => void store.loadConfig(), 15_000);
});
onUnmounted(() => clearInterval(timer));
</script>
<template>
<PageHeader title="Status" />
<div>
<ReadError v-if="catalogError" :message="catalogError" retry-label="Retry" class="mb-4" @retry="store.load()" />
<Alert v-if="config?.reload?.last_error" variant="destructive" class="mb-4">
<AlertTitle>{{ errors.settings.status.reload }}</AlertTitle>
<AlertDescription>{{ config.reload.last_error }}</AlertDescription>
</Alert>
<ServicesCard />
<VersionsCard />
<UpdateSettingsCard />
<Section v-if="config?.bootstrap && Object.keys(config.bootstrap).length" title="Startup settings">
<MetadataList :rows="bootstrapRows" />
<ReadError v-if="error && !health" :message="error" retry-label="Retry" class="mb-4" @retry="reload" />
<div v-else-if="!health" role="status" aria-label="Loading status" class="space-y-2"><Skeleton class="h-8 w-full" /><Skeleton class="h-8 w-full" /></div>
<template v-else>
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="reload" />
<InstanceCard :health="health" />
<HealthTables :checks="health.checks" :connectors="health.connectors" />
</template>
<Section v-if="bootstrapRows.length" title="Startup settings">
<MetadataList :rows="bootstrapRows" />
</Section>
</div>
</template>
-70
View File
@@ -1,70 +0,0 @@
<script setup lang="ts">
import { Alert, AlertDescription } from "@/components/ui/alert";
import { computed, onMounted } from "vue";
import { Field, FieldContent, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Section } from "@/components/ui/section";
import { Input } from "@/components/ui/input";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { useSettingsStore } from "@/stores/settings";
import { fieldInvalid } from "./settings-form";
import KindGate from "./KindGate.vue";
const KIND = "update-settings";
type UpdateSettings = { channel: string; pin: string };
const store = useSettingsStore();
onMounted(store.load);
const form = computed(() => store.formFor(KIND));
const channel = computed(() => (store.draftValue(KIND) as UpdateSettings | undefined)?.channel || "stable");
const error = computed(() => store.pageErrorFor(KIND));
const channels = [
{ value: "stable", label: "Stable" },
{ value: "next", label: "Prereleases" },
{ value: "exact-pin", label: "Pinned" },
];
</script>
<template>
<KindGate v-if="!form" :kind="KIND" />
<Section v-if="form" title="Updates">
<Alert v-if="error" variant="destructive" class="mb-2"><AlertDescription>{{ error }}</AlertDescription></Alert>
<FieldGroup class="@container/field-group">
<form.Field v-slot="{ field }" name="channel">
<Field orientation="responsive">
<FieldContent><FieldLabel>Release channel</FieldLabel></FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<ToggleGroup
type="single"
variant="outline"
size="sm"
:model-value="channel"
aria-label="Release channel"
@update:model-value="(value) => { if (value) field.handleChange(value); }"
>
<ToggleGroupItem v-for="option in channels" :key="option.value" :value="option.value">{{ option.label }}</ToggleGroupItem>
</ToggleGroup>
</div>
</Field>
</form.Field>
<form.Field v-if="channel === 'exact-pin'" v-slot="{ field }" name="pin">
<Field orientation="responsive" :data-invalid="fieldInvalid(field)">
<FieldContent><FieldLabel for="update-pin">Version</FieldLabel></FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<Input
id="update-pin"
:model-value="field.state.value"
class="max-w-40 font-mono"
placeholder="1.48.0"
:aria-invalid="fieldInvalid(field)"
@blur="field.handleBlur"
@update:model-value="field.handleChange"
/>
</div>
</Field>
</form.Field>
</FieldGroup>
</Section>
</template>
-214
View File
@@ -1,214 +0,0 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { Badge } from "@/components/ui/badge";
import { ReadError } from "@/components/ui/read-error";
import { Button } from "@/components/ui/button";
import CodeView from "@/base/CodeView.vue";
import { Section } from "@/components/ui/section";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Timestamp } from "@/components/ui/timestamp";
import { ApiError, api, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
import { useLiveResource } from "@/stores/session";
/**
* Per-component versions and the update that applies them: /api/version reports
* what is running, what the check command claims, what is available and the
* status between those (ok, update_available, drift, unknown).
*/
interface ComponentVersion {
id: string;
label: string;
install_type?: string;
reference?: string;
running_version?: string;
installed_claim?: string;
latest_available?: string;
status: "ok" | "update_available" | "drift" | "unknown";
}
const components = ref<ComponentVersion[]>([]);
const snapshot = ref<unknown>(null);
const available = ref<unknown[]>([]);
const canInstall = ref(false);
const notConfigured = ref(false);
const loading = ref(true);
const loadError = ref<string | null>(null);
const installing = ref(false);
const installProgress = ref<string[]>([]);
const installResult = ref<string | null>(null);
const installError = ref<string | null>(null);
const outcome = ref<{ at: string; summary: string; failed: boolean } | null>(null);
const outcomeError = ref<string | null>(null);
const updateable = computed(() =>
components.value.filter((c) => c.status === "update_available"),
);
const statusKind: Record<
ComponentVersion["status"],
"ok" | "warn" | "danger" | "idle"
> = {
ok: "ok",
update_available: "warn",
drift: "danger",
unknown: "idle",
};
const statusLabel: Record<ComponentVersion["status"], string> = {
ok: "",
update_available: "Update available",
drift: "Version drift",
unknown: "Unknown",
};
async function load(): Promise<void> {
loading.value = true;
loadError.value = null;
try {
const [report, update] = await Promise.all([
api.version<{ components: ComponentVersion[] }>(),
api
.updateSnapshot<{ snapshot: unknown; available: unknown[] | null; can_install: boolean }>()
.catch((err) => {
if (err instanceof ApiError && err.status === 501) {
notConfigured.value = true;
return null;
}
throw err;
}),
]);
components.value = report.components;
if (update) {
snapshot.value = update.snapshot;
available.value = update.available ?? [];
canInstall.value = update.can_install;
}
} catch (err) {
loadError.value = describeError(err, errors.common.unknown);
} finally {
loading.value = false;
}
}
// The last update's outcome is the update_report event the watchdog records
// after it restarts and health-checks the daemon.
async function loadOutcome(): Promise<void> {
try {
const page = await api.audit<{
records: {
at: string;
summary: string;
detail: { data: { health_check?: string; rolled_back?: boolean } | null };
}[];
}>({ actions: "update_report", limit: 1 });
const last = page.records[0];
const data = last?.detail.data;
outcome.value = last
? {
at: last.at,
summary: last.summary,
failed: !!data?.rolled_back || data?.health_check !== "passed",
}
: null;
outcomeError.value = null;
} catch (err) {
outcomeError.value = describeError(err, errors.common.unknown);
}
}
// The snapshot the install endpoint demands is the one the operator saw: the
// endpoint re-checks freshness and answers 409 when releases moved.
function installableSnapshot(): unknown {
return snapshot.value && available.value.length ? snapshot.value : null;
}
async function install(): Promise<void> {
const snap = installableSnapshot();
if (!snap || installing.value) return;
installing.value = true;
installError.value = null;
installProgress.value = [];
installResult.value = null;
try {
const result = await api.updateInstall<{
progress?: string[];
result?: { installed?: Record<string, string> };
}>(snap);
installProgress.value = result.progress ?? [];
const installed = result.result?.installed;
installResult.value = installed
? Object.entries(installed)
.map(([id, version]) => `${id} → ${version}`)
.join(", ")
: "update finished";
installResult.value = `Staged: ${installResult.value}. The outcome appears below after the restart.`;
await load();
} catch (err) {
installError.value = describeError(err, errors.common.unknown);
} finally {
installing.value = false;
}
}
onMounted(() => {
void load();
void loadOutcome();
});
useLiveResource("updates", () => void loadOutcome());
</script>
<template>
<p v-if="notConfigured" class="text-label text-fg-muted">Update checks not configured.</p>
<Section v-else title="Versions">
<ReadError v-if="loadError" :message="loadError" retry-label="Retry" class="mb-3" @retry="load" />
<p v-else-if="loading" class="text-label text-fg-muted">Checking…</p>
<template v-else>
<Table>
<TableHeader>
<TableRow>
<TableHead>Component</TableHead>
<TableHead>Running</TableHead>
<TableHead>Available</TableHead>
<TableHead class="w-full"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="component in components" :key="component.id">
<TableCell>{{ component.label || component.id }}</TableCell>
<TableCell class="font-mono text-fg-muted">{{
component.running_version || component.installed_claim
}}</TableCell>
<TableCell class="font-mono text-fg-muted">{{ component.latest_available }}</TableCell>
<TableCell>
<Badge v-if="component.status !== 'ok'" :variant="statusKind[component.status]">{{
statusLabel[component.status]
}}</Badge>
</TableCell>
</TableRow>
</TableBody>
</Table>
<Button class="mt-4 self-start" :disabled="installing || !installableSnapshot()" @click="install">{{ installing ? "Installing…" : `Install ${updateable.map((c) => c.label || c.id).join(", ")}`.trim() }}</Button>
<CodeView v-if="installProgress.length" :code="installProgress.join('\n')" class="mt-3" />
<p v-if="installResult" class="mt-2 text-label text-ok">
{{ installResult }}
</p>
<p v-if="outcome" class="mt-2 text-label" :class="outcome.failed ? 'text-danger' : 'text-ok'">
<Timestamp :at="outcome.at" /> {{ outcome.summary }}
</p>
<ReadError v-if="outcomeError" :message="outcomeError" class="mt-2" />
<ReadError v-if="installError" :message="installError" class="mt-2" />
</template>
</Section>
</template>
+48 -11
View File
@@ -1,31 +1,68 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { reactive, ref } from "vue";
import type { Denial, DenialsResponse } from "@/audit/denials";
import { errors } from "@/copy/errors";
import { ApiError, api, describeError } from "@/lib/api";
/** The caller's org's recorded access denials, newest first. */
const PAGE = 100;
/** What narrows the list; an empty field matches everything. `since` and
* `until` are datetime-local values, read in the browser's time zone. */
export interface DenialFilters {
principal: string;
action: string;
kind: string;
since: string;
until: string;
}
const noFilters = (): DenialFilters => ({ principal: "", action: "", kind: "", since: "", until: "" });
const toISO = (local: string): string => (local ? new Date(local).toISOString() : "");
/** The caller's org's recorded access denials, newest first, a page at a time. */
export const useDenialsStore = defineStore("denials", () => {
const denials = ref<Denial[]>([]);
const filters = reactive<DenialFilters>(noFilters());
const more = ref(false);
const loading = ref(false);
const error = ref("");
// A response for filters or a page the operator has since replaced is dropped.
let attempt = 0;
async function load(): Promise<void> {
/** load reads the first page, or the next one after those already shown. */
async function load(next = false): Promise<void> {
const mine = ++attempt;
loading.value = true;
try {
denials.value = (await api.denials<DenialsResponse>()).denials ?? [];
const response = await api.denials<DenialsResponse>({
principal: filters.principal.trim(),
action: filters.action.trim(),
kind: filters.kind.trim(),
since: toISO(filters.since),
until: toISO(filters.until),
limit: PAGE,
offset: next ? denials.value.length : 0,
});
if (mine !== attempt) return;
denials.value = next ? [...denials.value, ...(response.denials ?? [])] : (response.denials ?? []);
more.value = response.more;
error.value = "";
} catch (err) {
if (err instanceof ApiError && err.status === 503) {
error.value = errors.stores.denials.unavailable;
} else {
error.value = describeError(err, errors.common.unknown);
}
if (mine !== attempt) return;
error.value = err instanceof ApiError && err.status === 503
? errors.stores.denials.unavailable
: describeError(err, errors.common.unknown);
} finally {
loading.value = false;
if (mine === attempt) loading.value = false;
}
}
return { denials, loading, error, load };
function clear(): void {
Object.assign(filters, noFilters());
void load();
}
return { denials, filters, more, loading, error, load, clear };
});